Skip to content

Installation

To get started, you’ll need to install @graphql-ts/schema and graphql.

Terminal window
npm install graphql @graphql-ts/schema

Using @graphql-ts/schema starts with the g object which is a set of functions that mirror the GraphQL.js API, but with added type-safety.

To create g, you need to call gWithContext with a context type. The context type defines the type of the context that resolvers receive. You’ll generally want to export this from a module so you can use it in your schema definition. You’ll also want to export a type alias for g like type g<T> = gWithContext.infer<T>, you’ll use this later.

g.ts
import {
function gWithContext<Context>(): GWithContext<Context>
gWithContext
} from "@graphql-ts/schema";
export type
type Context = {}
Context
= {};
export const
const g: GWithContext<Context>
g
=
gWithContext<Context>(): GWithContext<Context>
gWithContext
<
type Context = {}
Context
>();
export type
type g<T> = T extends () => (args: any) => infer R ? R : T extends (args: any) => infer R ? R : never
g
<
function (type parameter) T in type g<T>
T
> =
(alias) namespace gWithContext
import gWithContext
gWithContext
.
type gWithContext.infer<T> = T extends () => (args: any) => infer R ? R : T extends (args: any) => infer R ? R : never
infer
<
function (type parameter) T in type g<T>
T
>;

Now you can use g to define your schema, you’ll probably start with a query type, like this:

schema.ts
import {
type g<T> = T extends () => (args: any) => infer R ? R : T extends (args: any) => infer R ? R : never
const g: GWithContext<Context>
g
} from "./g";
import {
class GraphQLSchema
GraphQLSchema
} from "graphql";
const
const Query: GObjectType<unknown, Context>
Query
=
const g: GWithContext<Context>
g
.
object: <unknown>(youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction?: {
youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction: true;
}) => <Fields, Interfaces>(config: {
...;
} & Omit<...>) => GObjectType<...>
object
()({
name: string
name
: "Query",
fields: {
hello: GField<unknown, any, GScalarType<string, string>, unknown, Context>;
} | (() => {
hello: GField<unknown, any, GScalarType<string, string>, unknown, Context>;
})
fields
: {
hello: GField<unknown, any, GScalarType<string, string>, unknown, Context>
hello
:
const g: GWithContext<Context>
g
.
field: <unknown, GScalarType<string, string>, () => string, any>(field: FieldFuncArgs<unknown, any, GScalarType<string, string>, Context> & {
resolve: ((source: unknown, args: {}, context: Context, info: GraphQLResolveInfo) => InferValueFromOutputType<...>) & (() => string);
}) => GField<...>
field
({
type: GScalarType<string, string>
type
:
const g: GWithContext<Context>
g
.
type String: GScalarType<string, string>
String
,
resolve: ((source: unknown, args: {}, context: Context, info: GraphQLResolveInfo) => InferValueFromOutputType<GScalarType<string, string>>) & (() => string)
resolve
: () => "world",
}),
},
});
const
const schema: GraphQLSchema
schema
= new
new GraphQLSchema(config: Readonly<GraphQLSchemaConfig>): GraphQLSchema
GraphQLSchema
({
query?: Maybe<GraphQLObjectType<any, any>>
query
:
const Query: GObjectType<unknown, Context>
Query
,
});

If we print that schema in GraphQL SDL, it looks like this:

schema.graphql
type Query {
hello: String
}

You’ve now created a GraphQL schema using @graphql-ts/schema! 🎉

This schema object can be used with any server that works with GraphQL.js schema, such as GraphQL Yoga, Apollo Server, graphql-http, etc.