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;

37 lines (36 loc) 1.81 kB
import { dereferenceObject, healJSONSchema } from 'json-machete'; import { process } from '@graphql-mesh/cross-helpers'; import { getInterpolatedHeadersFactory } from '@graphql-mesh/string-interpolation'; import { defaultImportFn, readFileOrUrl } from '@graphql-mesh/utils'; import { getReferencedJSONSchemaFromOperations } from './getReferencedJSONSchemaFromOperations.js'; export async function getDereferencedJSONSchemaFromOperations({ operations, cwd = process.cwd(), logger, fetchFn, schemaHeaders, ignoreErrorResponses, endpoint, operationHeaders, queryParams, }) { const referencedJSONSchema = await getReferencedJSONSchemaFromOperations({ operations, cwd, schemaHeaders, ignoreErrorResponses, fetchFn, endpoint, operationHeaders, queryParams, }); logger.debug(`Dereferencing JSON Schema to resolve all $refs`); const schemaHeadersFactory = getInterpolatedHeadersFactory(schemaHeaders); const dereferenceObjectLogger = logger.child('dereferenceObject'); const readFileOrUrlForJsonMachete = (path, opts) => readFileOrUrl(path, { cwd: opts.cwd, fetch: fetchFn, logger: dereferenceObjectLogger, headers: schemaHeadersFactory({ env: process.env }), importFn: defaultImportFn, }); const fullyDeferencedSchema = await dereferenceObject(referencedJSONSchema, { cwd, debugLogFn: dereferenceObjectLogger.debug.bind(dereferenceObjectLogger), readFileOrUrl: readFileOrUrlForJsonMachete, }); logger.debug(`Healing JSON Schema`); const healJSONSchemaLogger = logger.child('healJSONSchema'); const healedSchema = await healJSONSchema(fullyDeferencedSchema, healJSONSchemaLogger.debug.bind(healJSONSchemaLogger)); return healedSchema; }