Hey guys, I currently have a project in which I have split the Query object in multiple files. I then import those separate Query objects and merge them into one main Query object which I pass to the server. It is working but I am just wondering if this is the way to do it or is there a better way?
Thank you
import Query1 from ...
import Query2 from ...
import Query3 from ...
import Query4 from ...
export const Query = { ...Query1, ...Query2, ...Query3, ...Query4};
// ----------------------- index.js file
import Query from ...
const startServer = async () => {
const schema = applyMiddleware(
makeExecutableSchema({
typeDefs: [....],
resolvers: {
Query
},
})
);
}
Lets say you have Query.js, Query2.js, Query3.js etc that looks similar to this:
queries/Query1.js
export const Query1 = gql`{...}`;
queries/Query2.js
export const Query1 = gql`{...}`;
You can create index.js in the queries directory that looks like this:
export * from './Query1.js';
export * from `./Query2.js`;
And in your main file you just simply call:
import * as typeDefs from './queries';
This basically achieves the same result without the need to combine them like you are doing it. If you want to exclude a query, just delete it from index.ts. And when you need add one, just add it to the list there.
Why does this work? If a module system, you don’t need to specify every single variable so “*” works with both exports and imports. And if your import will point to a directory, the module system will look for index.js inside that directory.