Skip to content

acomagu/graphql-codegen-typescript-schema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL Codegen TypeScript Schema Plugin

This plugin for GraphQL Code Generator allows you to import the schema from generated file(like typescript-document-nodes plugin, but for schema).

Traditionally, working with .graphql files required configuring a bundler loader and adjusting tsconfig settings. With this plugin, you can bypass those steps. You can just import schema object from the generated TypeScript file.

Installation

npm i -D graphql-codegen-typescript-schema

Usage

Add the plugin to your GraphQL Codegen configuration file:

schema: schema.graphql
generates:
  schema.ts:
    plugins:
      - typescript-schema # <-- this

It can be used with other typescript-* plugins;

schema: schema.graphql
generates:
  generated.ts:
    plugins:
      - typescript
      - typescript-resolvers
      - typescript-schema # <-- this

This will add an named export as schema to generated.ts.

import { schema } from './generated.ts';

Options

schemaOutput

This option specifies the format of the generated output. It accepts two values:

  • schemaObject (default): Exports a GraphQL schema object using buildSchema.
  • dslString: Exports the schema as a raw DSL string.

For example,

- typescript-schema:
    schemaOutput: schemaObject

This configuration will generate a file that looks like:

import { buildSchema } from 'graphql';

export const schema = buildSchema(`
  type Query {
    hello: String
  }
`);

Also,

- typescript-schema:
    schemaOutput: dslString

This configuration will generate a file that looks like:

export const schema = `
  type Query {
    hello: String
  }
`;

The schema object format is ready to use with GraphQL libraries, while the DSL string format provides more flexibility for custom processing.

About

graphql-codegen plugin exports the GraphQLSchema object

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •