npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan HefnerΒ  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

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)