@pothos/plugin-add-graphql
Version:
A Pothos plugin for adding existing GraphQL types to a Pothos schema
46 lines (41 loc) • 1.25 kB
text/typescript
import { createContextCache, type SchemaTypes } from '@pothos/core';
import {
type GraphQLNamedType,
isEnumType,
isInputObjectType,
isInterfaceType,
isObjectType,
isScalarType,
isUnionType,
} from 'graphql';
export const referencedTypes = createContextCache(() => new Set<GraphQLNamedType>());
export function addTypeToSchema<Types extends SchemaTypes>(
builder: PothosSchemaTypes.SchemaBuilder<Types>,
type: GraphQLNamedType,
) {
if (builder.configStore.hasConfig(type.name as never)) {
return;
}
if (isObjectType(type)) {
builder.addGraphQLObject(type);
} else if (isInterfaceType(type)) {
builder.addGraphQLInterface(type);
} else if (isUnionType(type)) {
builder.addGraphQLUnion(type);
} else if (isEnumType(type)) {
builder.addGraphQLEnum(type);
} else if (isInputObjectType(type)) {
builder.addGraphQLInput(type);
} else if (isScalarType(type)) {
builder.addScalarType(type.name as never, type);
}
}
export function addReferencedType<Types extends SchemaTypes>(
builder: PothosSchemaTypes.SchemaBuilder<Types>,
type: GraphQLNamedType,
) {
if (referencedTypes(builder).has(type)) {
return;
}
builder.configStore.onPrepare(() => addTypeToSchema(builder, type));
}