graphql-codegen-modules-gen
v1.8.1
Published
## π‘ Motivation
Downloads
5
Readme
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)