UNPKG

@magidoc/plugin-fuse-graphql

Version:

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

91 lines (88 loc) 2.81 kB
import { mergeMarkdownOptions } from '@magidoc/plugin-fuse-markdown'; import Fuse from 'fuse.js'; import { asQueryResult } from './graphql/queries.js'; import { asTypeSearchResult } from './graphql/types.js'; import { SearchResultType } from './result.js'; function defaultFuseOptions() { return { keys: [ { name: 'name', weight: 1.5, }, { name: 'description', weight: 1, }, // For queries { name: 'arguments.name', weight: 1.2, }, { name: 'arguments.description', weight: 1.0, }, // For enums { name: 'values.value', weight: 1.2, }, { name: 'values.description', weight: 1.0, }, // For objects, interfaces and input objects { name: 'fields.name', weight: 1.2, }, { name: 'fields.description', weight: 1.0, }, { name: 'fields.arguments.name', weight: 0.9, }, { name: 'fields.arguments.description', weight: 0.8, }, ], distance: 100, minMatchCharLength: 3, threshold: 0.2, includeMatches: true, includeScore: true, }; } function index(schema, options) { const fuse = options?.fuse ?? new Fuse([], defaultFuseOptions()); const markdownOptions = mergeMarkdownOptions(options?.markdown); indexAllFieldsOf(SearchResultType.QUERY, schema.getQueryType(), fuse, markdownOptions); indexAllFieldsOf(SearchResultType.MUTATION, schema.getMutationType(), fuse, markdownOptions); indexAllFieldsOf(SearchResultType.SUBSCRIPTION, schema.getSubscriptionType(), fuse, markdownOptions); indexAllTypes(schema, fuse, markdownOptions); return fuse; } function indexAllTypes(schema, fuse, markdownOptions) { Object.values(schema.getTypeMap()).forEach((type) => { if (type === schema.getQueryType() || type === schema.getMutationType() || type === schema.getSubscriptionType() || type.name.startsWith('__')) { return; } fuse.add(asTypeSearchResult(type, markdownOptions)); }); } function indexAllFieldsOf(type, target, fuse, options) { if (!target) return; Object.values(target.getFields()).forEach((field) => { fuse.add(asQueryResult(type, field, options)); }); } export { defaultFuseOptions, index }; //# sourceMappingURL=indexer.js.map