Skip to content

Commit 9533fe0

Browse files
committed
TypeScript template feature: generate full schema type
This adds a new `schemaType` option to the TypeScript template. When used, this option will add a type to the generated TypeScript code that represents the entirety of the input GraphQL schema's type system. Each type from the schema will be placed as a property on the interface and given its declared type. The new interface is named `Schema`, optionally prefixed by the `interfacePrefix` option and placed within the optional `schemaNamespace` namespace. This type is not generated by default. It is useful when the context of the schema as a whole is needed, such as when determining whether a GraphQL type name used in code is properly referencing a type that exists in a target GraphQL schema.
1 parent 8e20bfd commit 9533fe0

4 files changed

Lines changed: 19 additions & 1 deletion

File tree

packages/templates/typescript/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ This will cause the codegen to output `readonly` properties and `ReadonlyArray`.
101101

102102
This will cause the codegen to output types for resolvers.
103103

104+
### `schemaType` (or `CODEGEN_SCHEMA_TYPE`, default value: `false`)
105+
106+
This will cause the codegen to output a single type that represents the entire schema.
107+
104108
### `noNamespaces` (or `CODEGEN_NO_NAMESPACES`, default value: `null`)
105109

106110
This will cause the codegen not to use `namespace` in typings

packages/templates/typescript/src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as schema from './schema.handlebars';
44
import * as resolver from './resolver.handlebars';
55
import * as documents from './documents.handlebars';
66
import * as selectionSet from './selection-set.handlebars';
7+
import * as typeProperty from './type-property.handlebars';
78
import * as fragments from './fragments.handlebars';
89
import * as enumTemplate from './enum.handlebars';
910
import { EInputType, GeneratorConfig } from 'graphql-codegen-core';
@@ -25,6 +26,7 @@ export const config: GeneratorConfig = {
2526
resolver,
2627
documents,
2728
selectionSet,
29+
typeProperty,
2830
fragments,
2931
enum: enumTemplate
3032
},

packages/templates/typescript/src/schema.handlebars

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,20 @@ export interface {{ toPascalCase name }}{{ toPascalCase ../name }}Args {
6363
export type {{ toPascalCase name }} = {{#each possibleTypes}}{{ toPascalCase this}}{{#unless @last}} | {{/unless}}{{/each}};
6464

6565
{{/each}}
66+
{{#if config.schemaType }}
67+
{{#if types}}
68+
export interface {{@root.config.interfacePrefix}}Schema {
69+
{{#each scalars}}{{~> typeProperty prefix=false }}{{/each}}
70+
{{#each types}}{{~> typeProperty prefix=true }}{{/each}}
71+
{{#each enums}}{{~> typeProperty prefix=true }}{{/each}}
72+
{{#each unions}}{{~> typeProperty prefix=true }}{{/each}}
73+
{{#each interfaces}}{{~> typeProperty prefix=true }}{{/each}}
74+
}
75+
{{/if}}
76+
{{/if}}
6677
{{#if @root.config.schemaNamespace ~}} } {{~/if}}
6778
{{#ifCond @root.config.resolvers "!==" false }}
6879
{{#each types}}
6980
{{~> resolver }}
7081
{{/each}}
71-
{{/ifCond}}
82+
{{/ifCond}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#if @root.config.immutableTypes }}readonly {{/if}}{{ name }}: {{#if prefix }}{{@root.config.interfacePrefix}}{{/if}}{{ toPascalCase name }} {{ toComment description }};

0 commit comments

Comments
 (0)