graphql-codegen-on-operations
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

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

A plugin for https://the-guild.dev/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

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;

Readme

Keywords

none

Package Sidebar

Install

npm i graphql-codegen-on-operations

Weekly Downloads

34

Version

0.0.1

License

MIT

Unpacked Size

19.4 kB

Total Files

12

Last publish

Collaborators

  • ilijanl