UNPKG

@magidoc/plugin-fuse-graphql

Version:

A plugin used to index a GraphQL schema into a Fuse.js search engine.

79 lines (76 loc) 2.8 kB
import { isEnumType, isObjectType, isInterfaceType, isScalarType, isUnionType, isInputObjectType } from 'graphql'; import { GraphQLType, SearchResultType } from '../result.js'; import { getDescription } from './description.js'; function asTypeSearchResult(type, options) { if (isEnumType(type)) return asEnumSearchResult(type, options); if (isObjectType(type)) return asObjectSearchResult(GraphQLType.OBJECT, type, options); if (isInterfaceType(type)) return asObjectSearchResult(GraphQLType.INTERFACE, type, options); if (isScalarType(type)) return asScalarSearchResult(type, options); if (isUnionType(type)) return asUnionSearchResult(type, options); if (isInputObjectType(type)) return asInputObjectType(type, options); throw new Error('This code block should be unreachable. If you ever receive this exception, it means you have an invalid setup using GraphQL.'); } function asInputObjectType(target, options) { return { type: SearchResultType.TYPE, graphqlType: GraphQLType.INPUT_OBJECT, name: target.name, description: getDescription(target, options), fields: Object.values(target.getFields()).map((field) => ({ name: field.name, description: getDescription(field, options), })), }; } function asUnionSearchResult(target, options) { return { type: SearchResultType.TYPE, graphqlType: GraphQLType.UNION, name: target.name, description: getDescription(target, options), }; } function asScalarSearchResult(target, options) { return { type: SearchResultType.TYPE, graphqlType: GraphQLType.SCALAR, name: target.name, description: getDescription(target, options), }; } function asObjectSearchResult(type, target, options) { return { type: SearchResultType.TYPE, graphqlType: type, name: target.name, description: getDescription(target, options), fields: Object.values(target.getFields()).map((field) => ({ name: field.name, description: getDescription(field, options), arguments: field.args.map((arg) => ({ name: arg.name, description: getDescription(arg, options), })), })), }; } function asEnumSearchResult(target, options) { return { name: target.name, description: getDescription(target, options), graphqlType: GraphQLType.ENUM, type: SearchResultType.TYPE, values: target.getValues().map((value) => ({ value: value.name, description: getDescription(value, options), })), }; } export { asTypeSearchResult }; //# sourceMappingURL=types.js.map