An open API service indexing awesome lists of open source software.

https://github.com/ilijanl/graphql-codegen-on-operations

Generic graphql-codegen plugin to generate some code from operations/fragments
https://github.com/ilijanl/graphql-codegen-on-operations

graphql graphql-codegen typescript

Last synced: 10 months ago
JSON representation

Generic graphql-codegen plugin to generate some code from operations/fragments

Awesome Lists containing this project

README

          

# GraphQL codegen plugin to generate some code/configuration from operations/fragments

A plugin for [https://the-guild.dev/graphql/codegen](graphql-codegen) to create a signature from your documents which can be validated on the server

## Install

Install graphql-code-generator and this plugin

yarn add -D @graphql-codegen/cli @graphql-codegen/typescript @graphql-codegen/typescript-operations graphql-codegen-on-operations

## Usage

Create codegen.ts

```ts
import * as dotenv from 'dotenv'; // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
dotenv.config();
import type { CodegenConfig } from '@graphql-codegen/cli';
import createSignFn from 'graphql-codegen-on-operations/lib/use/signing';
import createListFn from 'graphql-codegen-on-operations/lib/use/listing';
import { GenerateFn } from 'graphql-codegen-on-operations';
import { print } from 'graphql';

const customGen: GenerateFn = (_schema, { documents }) => {
return JSON.stringify(documents.map((d) => print(d.node)).join('\n'), null, 2);
};

const config: CodegenConfig = {
schema: 'http://localhost:3000/api/graphql',
documents: ['gql/**/*.graphql'],
generates: {
'./__generated__/gql.ts': {
plugins: ['typescript', 'typescript-operations', 'typed-document-node'],
},
'./__generated__/signed-operations.json': {
plugins: ['graphql-codegen-on-operations'],
config: {
gen: createSignFn({
// should be long and not exposed to public
secret: process.env.SIGNING_SECRET,
}),
},
},
'./__generated__/list-operations.json': {
plugins: ['graphql-codegen-on-operations'],
config: {
gen: createListFn(),
},
},
'./__generated__/custom.json': {
plugins: ['graphql-codegen-on-operations'],
config: {
gen: customGen,
},
},
},
};
export default config;
```