@graphql-ts/extend

Search for an npm package
    "@graphql-ts/extend"
    • extend
    • Extension
    • BaseSchemaMeta

@graphql-ts/extend

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.

more
less

See extend for more details.

module "@graphql-ts/extend" {

extend allows you to extend a GraphQLSchema with @graphql-ts/schema.

more
less
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
}
export function extend(extension: Extension | readonly Extension[] | ((base: BaseSchemaMeta) => Extension | readonly Extension[])): (schema: import("graphql").GraphQLSchema) => import("graphql").GraphQLSchema

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.

more
less
const extension: Extension = {
query: {
isLoggedIn: g.field({
type: g.Boolean,
resolve(source, args, context, info) {
// ...
},
}),
},
};
query?: FieldsOnAnything;

Extra fields to be added to the mutation type.

more
less
const extension: Extension = {
mutation: {
createPost: g.field({
type: g.Boolean,
resolve(source, args, context, info) {
// ...
},
}),
},
};
mutation?: FieldsOnAnything;
}
Referenced by
  • extend
Unexported symbols referenced here

Any

more
less

Note the distinct usages of any vs unknown is intentional.

  • The 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 types
  • The first any 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
  • The 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.
type FieldsOnAnything = {
[key: string]: import("@graphql-ts/schema").GField<unknown, any, import("@graphql-ts/schema").GOutputType<any>, unknown, any>;
}
Referenced by

This object contains the schema being extended and functions to get GraphQL types from the schema.

export type BaseSchemaMeta = {
schema: import("graphql").GraphQLSchema;

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.

more
less
const originalSchema = new GraphQLSchema({ ...etc });
const extendedSchema = extend((base) => ({
query: {
something: g.field({
type: base.object("Something"),
resolve() {
return { something: true };
},
}),
},
}))(originalSchema);
object(name: string): import("@graphql-ts/schema").GObjectType<unknown, unknown>;

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.

more
less
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);
inputObject(name: string): import("@graphql-ts/schema").GInputObjectType<{
[key: string]: import("@graphql-ts/schema").GArg<any, boolean>;
}, boolean>;

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.

more
less
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);
enum(name: string): import("@graphql-ts/schema").GEnumType<Record<string, unknown>>;

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.

more
less
const originalSchema = new GraphQLSchema({ ...etc });
const extendedSchema = extend((base) => ({
query: {
something: g.field({
type: base.union("Something"),
resolve() {
return { something: true };
},
}),
},
}))(originalSchema);
union(name: string): import("@graphql-ts/schema").GUnionType<unknown, unknown>;

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.

more
less
const originalSchema = new GraphQLSchema({ ...etc });
const extendedSchema = extend((base) => ({
query: {
something: g.field({
type: base.interface("Something"),
resolve() {
return { something: true };
},
}),
},
}))(originalSchema);
interface(name: string): import("@graphql-ts/schema").GInterfaceType<unknown, any, unknown>;

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.

more
less

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);
scalar(name: string): import("@graphql-ts/schema").GScalarType<unknown>;
}
Referenced by
}