Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-codegen-modules-gen

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-codegen-modules-gen

## 💡 Motivation

  • 1.1.3
  • npm
  • Socket score

Version published
Weekly downloads
15
increased by1400%
Maintainers
1
Weekly downloads
 
Created
Source

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

▶ Add it at the end of your usual @graphql-codegen

At the end of your codegen.yml file, add:

hooks:
  afterAllFileWrite:
    - yarn gen-mg

✨ Result

A new folder named _gen with resolvers.ts and typedefs.ts containing everything needed in 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! 🔥

FAQs

Package last updated on 26 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc