UNPKG

@omnigraph/json-schema

Version:

This package generates GraphQL Schema from JSON Schema and sample JSON request and responses. You can define your root field endpoints like below in your GraphQL Config for example;

36 lines (35 loc) 1.71 kB
import { specifiedDirectives } from 'graphql'; import { SchemaComposer } from 'graphql-compose'; import { addExecutionDirectivesToComposer, } from './addExecutionLogicToComposer.js'; import { getComposerFromJSONSchema } from './getComposerFromJSONSchema.js'; export async function getGraphQLSchemaFromDereferencedJSONSchema(name, opts) { const { fullyDeferencedSchema, logger, operations, operationHeaders, endpoint, queryParams, queryStringOptions, } = opts; logger.debug(`Generating GraphQL Schema from the bundled JSON Schema`); const visitorResult = await getComposerFromJSONSchema(fullyDeferencedSchema, logger.child('getComposerFromJSONSchema')); const schemaComposerWithoutExecutionLogic = visitorResult.output; if (!(schemaComposerWithoutExecutionLogic instanceof SchemaComposer)) { throw new Error('The visitor result should be a SchemaComposer instance.'); } // graphql-compose doesn't add @defer and @stream to the schema for (const directive of specifiedDirectives) { schemaComposerWithoutExecutionLogic.addDirective(directive); } const schemaComposerWithExecutionLogic = await addExecutionDirectivesToComposer(name, { schemaComposer: schemaComposerWithoutExecutionLogic, logger, operations, operationHeaders, endpoint, queryParams, queryStringOptions, }); if (schemaComposerWithExecutionLogic.Query.getFieldNames().length === 0) { schemaComposerWithExecutionLogic.Query.addFields({ dummy: { type: 'String', resolve: () => 'dummy', }, }); } return schemaComposerWithExecutionLogic.buildSchema(); }