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;

33 lines (32 loc) 1.52 kB
import { specifiedDirectives } from 'graphql'; import { SchemaComposer } from 'graphql-compose'; import { addExecutionDirectivesToComposer, } from './addExecutionLogicToComposer.js'; import { getComposerFromJSONSchema } from './getComposerFromJSONSchema.js'; export async function getGraphQLSchemaFromDereferencedJSONSchema(subgraphName, opts) { const { fullyDeferencedSchema, logger, operations, operationHeaders, endpoint, queryParams, queryStringOptions, handlerName = 'rest', } = opts; logger.debug(`Generating GraphQL Schema from the bundled JSON Schema`); const visitorResult = await getComposerFromJSONSchema({ subgraphName, schema: fullyDeferencedSchema, logger: logger.child('getComposerFromJSONSchema'), getScalarForFormat: opts.getScalarForFormat, }); 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); } return addExecutionDirectivesToComposer(subgraphName, { schemaComposer: schemaComposerWithoutExecutionLogic, logger, operations, operationHeaders, endpoint, queryParams, queryStringOptions, handlerName, }); }