An API to extend an arbitrary GraphQLSchema with @graphql-ts/schema
.
Note if you're building a schema entirely with @graphql-ts/schema
, you
shouldn't use this package. This is useful when you have a
GraphQLSchema from somewhere else and you want to some fields to
various places in it.
See extend for more details.
extend
allows you to extend a GraphQLSchema with
@graphql-ts/schema
.
const originalSchema = new GraphQLSchema({ ...etc });const extendedSchema = extend({query: {hello: g.field({type: g.String,resolve() {return "Hello!";},}),},})(originalSchema);
To use existing types from the schema you're extending, you can provide a function and use the BaseSchemaMeta passed into the function to use existing types in the schema.
const originalSchema = new GraphQLSchema({ ...etc });const extendedSchema = extend((base) => ({query: {something: g.field({type: base.object("Something"),resolve() {return { something: true };},}),},}))(originalSchema);
See BaseSchemaMeta for how to get other types from the schema
extend
will currently throw an error if the query or mutation types are
used in other types like this. This will be allowed in a future version.
type Query { thing: Query }
An extension to a GraphQL schema. This currently only supports adding fields to the query and mutation types. Extending other types will be supported in the future.
export type Extension = {Extra fields to be added to the query type.
const extension: Extension = {query: {isLoggedIn: g.field({type: g.Boolean,resolve(source, args, context, info) {// ...},}),},};
Extra fields to be added to the mutation type.
const extension: Extension = {mutation: {createPost: g.field({type: g.Boolean,resolve(source, args, context, info) {// ...},}),},};
Any
Note the distinct usages of any
vs unknown
is intentional.
unknown
used for the source type is because the source isn't known and
it shouldn't generally be used here because these fields are on the query
and mutation typesany
used for the Args
type parameter is used because Args
is
invariant so only Record<string, Arg<InputType, boolean>>
would work with
it. The arguable unsafety here doesn't really matter because people will
always use g.field
any
in OutputType
and the last type argument mean that a field that
requires any context can be provided. This is unsafe, the only way this
could arguably be made more "safe" is by making this unknown which would
requiring casting or make extend
and etc. generic over a Context
but
given this is immediately used on an arbitrary GraphQLSchema so the
type would immediately be thrown away, it would be pretty much pointless.This object contains the schema being extended and functions to get GraphQL types from the schema.
export type BaseSchemaMeta = {Gets an object type from the existing GraphQL schema. If there is no object type in the existing schema with the name passed, an error will be thrown.
const originalSchema = new GraphQLSchema({ ...etc });const extendedSchema = extend((base) => ({query: {something: g.field({type: base.object("Something"),resolve() {return { something: true };},}),},}))(originalSchema);
Gets an input object type from the existing GraphQL schema. If there is no input object type in the existing schema with the name passed, an error will be thrown.
const originalSchema = new GraphQLSchema({ ...etc });const extendedSchema = extend((base) => ({query: {something: g.field({type: g.String,args: {something: g.field({type: base.inputObject("Something"),}),},resolve(source, { something }) {console.log(something);return "";},}),},}))(originalSchema);
Gets an enum type from the existing GraphQL schema. If there is no enum type in the existing schema with the name passed, an error will be thrown.
const originalSchema = new GraphQLSchema({ ...etc });const extendedSchema = extend((base) => ({query: {something: g.field({type: base.enum("Something"),args: {something: g.field({type: base.enum("Something"),}),},resolve(source, { something }) {return something;},}),},}))(originalSchema);
Gets a union type from the existing GraphQL schema. If there is no union type in the existing schema with the name passed, an error will be thrown.
const originalSchema = new GraphQLSchema({ ...etc });const extendedSchema = extend((base) => ({query: {something: g.field({type: base.union("Something"),resolve() {return { something: true };},}),},}))(originalSchema);
Gets an interface type from the existing GraphQL schema. If there is no interface type in the existing schema with the name passed, an error will be thrown.
const originalSchema = new GraphQLSchema({ ...etc });const extendedSchema = extend((base) => ({query: {something: g.field({type: base.interface("Something"),resolve() {return { something: true };},}),},}))(originalSchema);
Gets a scalar type from the existing GraphQL schema. If there is no scalar type in the existing schema with the name passed, an error will be thrown.
If the name of a built-in scalar type is passed, an error will also be thrown.
const originalSchema = new GraphQLSchema({ ...etc });const extendedSchema = extend((base) => ({query: {something: g.field({type: base.scalar("JSON"),args: {something: g.field({type: base.scalar("JSON"),}),},resolve(source, { something }) {return something;},}),},}))(originalSchema);