graphql-codegen-typescript-schema
v2.2.0
Published
This plugin for [GraphQL Code Generator](https://the-guild.dev/graphql/codegen) allows you to import the schema from generated file(like [typescript-document-nodes plugin](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-document-nodes)
Readme
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-schemaUsage
Add the plugin to your GraphQL Codegen configuration file:
schema: schema.graphql
generates:
schema.ts:
plugins:
- typescript-schema # <-- thisIt can be used with other typescript-* plugins;
schema: schema.graphql
generates:
generated.ts:
plugins:
- typescript
- typescript-resolvers
- typescript-schema # <-- thisThis 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 usingbuildSchema.dslString: Exports the schema as a raw DSL string.
For example,
- typescript-schema:
schemaOutput: schemaObjectThis configuration will generate a file that looks like:
import { buildSchema } from 'graphql';
export const schema = buildSchema(`
type Query {
hello: String
}
`);Also,
- typescript-schema:
schemaOutput: dslStringThis 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.
