1.8.1 ā€¢ Published 2 years ago

graphql-codegen-modules-gen v1.8.1

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

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)

1.8.1

2 years ago

1.8.0

2 years ago

1.7.0

2 years ago

1.6.1

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.0

2 years ago