GiraphQL
Search…
Printing Schema
Sometimes it's useful to have an SDL version of your schema. To do this, you can use some tools from the graphql package to write your schema out as SDL to a file.
1
import { writeFileSync } from 'fs';
2
import { printSchema, lexicographicSortSchema } from 'graphql';
3
import SchemaBuilder from '@giraphql/core';
4
5
const builder = new SchemaBuilder({});
6
7
builder.queryType({
8
fields: (t) => ({
9
hello: t.string({
10
args: {
11
name: t.arg.string(),
12
},
13
resolve: (parent, { name }) => `hello, ${name || 'World'}`,
14
}),
15
}),
16
});
17
18
const schema = builder.toSchema({});
19
const schemaAsString = printSchema(lexicographicSortSchema(schema));
20
21
writeFileSync('/path/to/schema.graphql', schemaAsString);
Copied!

Using graphql-code-generator

An alternative to printing your schema directly is to generate your schema file using graphql-code-generator. See Generating Client Types for more details
Last modified 2mo ago
Copy link
Edit on GitHub