graphql-codegen-modules-gen v1.8.1
Welcome to graphql-codegen-modules-gen š„
š” Motivation
To use Graphql-Modules well, you need to organize your code by modules (that's the all point š).
Something like:
...
āāā modules
ā ā
ā āāā user
ā ā āāā graphql <-- client graphql operations
ā ā ā āāā FRAG.user.gql
ā ā ā āāā MUTATION.CreateUser.gql
ā ā ā āāā QUERY.GetUser.gql
ā ā ā āāā QUERY.GetUsers.gql
ā ā āāā resolvers <-- server graphql resolvers
ā ā ā āāā _Mutation.ts
ā ā ā āāā _Query.ts
ā ā ā āāā User.ts
ā ā āāā typedefs <-- server graphql type definitions
ā ā ā āāā _Mutation.graphql
ā ā ā āāā _Query.graphql
ā ā ā āāā TYPE.User.graphql
ā ā āāā index.ts <-- server module creation / orchestration
ā ā
ā āāā module2
ā ā āāā graphql
ā ā ā āāā ...
ā ā āāā resolvers
ā ā ā āāā ...
ā ā āāā typedefs
ā ā ā āāā ...
ā ā āāā index.ts
ā ā
ā āāā module3
ā ā āāā ...
...
In index.ts
of each module, you will bring all your resolvers and type definitions together. To do this, you can use GraphQL Tools - Merging resolvers and the merge will be done at runtime. Unfortunately, it will not work well with bundlers (check this)
That's why graphql-codegen-modules-gen
exist. The cli, will generate you resolvers.ts
and typedefs.ts
per module at build time. Then, you will compose your index.ts
with these files.
š Usage
ā¶ Install
yarn add graphql-codegen-modules-gen -D
ā¶ Add in your package.json
"gen-mg": "yarn graphql-codegen-modules-gen ./src/lib/modules",
replacing ./src/lib/modules
by the location of your modules
ā¶ Run it
yarn gen-mg
ā¶ (Bonus) Execute it before your usual @graphql-codegen
with a pre hook.
āØ Result
In each module, a new folder named _gen
with resolvers.ts
and typedefs.ts
containing everything needed for your index.ts
file.
...
āāā modules
ā ā
ā āāā user
ā ā āāā _gen <-- āØ new folder (I put it in the .gitignore)
ā ā ā āāā resolvers.ts <-- āØ new file, combine resolvers, export resolvers
ā ā ā āāā typedefs.ts <-- āØ new file, merged typedefs, export typedefs
ā ā āāā graphql
ā ā ā āāā FRAG.user.gql
ā ā ā āāā MUTATION.CreateUser.gql
ā ā ā āāā QUERY.GetUser.gql
ā ā ā āāā QUERY.GetUsers.gql
ā ā āāā resolvers
ā ā ā āāā _Mutation.ts
ā ā ā āāā _Query.ts
ā ā ā āāā User.ts
ā ā āāā typedefs
ā ā ā āāā _Mutation.graphql
ā ā ā āāā _Query.graphql
ā ā ā āāā TYPE.User.graphql
ā ā āāā index.ts
ā ā
ā āāā module2
ā ā āāā graphql
ā ā ā āāā ...
ā ā āāā resolvers
ā ā ā āāā ...
ā ā āāā typedefs
ā ā ā āāā ...
ā ā āāā index.ts
ā ā
ā āāā module3
ā ā āāā ...
...
And here is how my index.ts
file looks like:
import { createModule } from 'graphql-modules';
import { resolvers } from './_gen/resolvers';
import { typeDefs } from './_gen/typedefs';
export const userModule = createModule({
id: 'user-module',
typeDefs,
resolvers,
providers: [],
middlewares: {
'*': {
'*': []
}
}
});
Now, enjoy! š„
In Addition
Enums 0/ Creates a dedicated module for all Enums with *.graphql
and List*.ts
to get valid graphql types & ready to use list in the UI.
Merge 1/ Generate your resolvers.ts
and typedefs.ts
files per module
Merge 2/ Generate global _ctxModules.ts
(merge all _ctxXXX.ts
of each modules)
Merge 3/ Generate global _appModules.ts
(merge all index.ts
of each modules)
Merge 4/ Generate global _urqlCache.ts
(merge all _urqlCacheXXX.ts
of each modules)