From d98bae873f43b64838fe5d3b6123ba0657f1554b Mon Sep 17 00:00:00 2001 From: Yasser Kaddour Date: Sun, 23 Apr 2017 10:34:51 +0100 Subject: [PATCH] Add support for Json schema to relay-compiler Fix #1639 --- docs/modern/RelayCompiler.md | 2 +- packages/relay-compiler/bin/RelayCompilerBin.js | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/modern/RelayCompiler.md b/docs/modern/RelayCompiler.md index 8c20f12035e2b..b2d1c323180ab 100644 --- a/docs/modern/RelayCompiler.md +++ b/docs/modern/RelayCompiler.md @@ -63,7 +63,7 @@ Then after making edits to your application files, run `relay-compiler --src ./s ## GraphQL Schema -To use the Relay Compiler, you need a GraphQL schema file, describing your GraphQL server's API. Typically these files are local representations of a server source of truth and are not edited directly. For example, we might have a `schema.graphql` like: +To use the Relay Compiler, you need either a .graphql or .json GraphQL schema file, describing your GraphQL server's API. Typically these files are local representations of a server source of truth and are not edited directly. For example, we might have a `schema.graphql` like: ```graphql schema { diff --git a/packages/relay-compiler/bin/RelayCompilerBin.js b/packages/relay-compiler/bin/RelayCompilerBin.js index 8134d705181b8..5196966872e84 100644 --- a/packages/relay-compiler/bin/RelayCompilerBin.js +++ b/packages/relay-compiler/bin/RelayCompilerBin.js @@ -21,7 +21,13 @@ const fs = require('fs'); const path = require('path'); const yargs = require('yargs'); -const {buildASTSchema, parse} = require('graphql'); +const { + buildASTSchema, + buildClientSchema, + parse, + printSchema, +} = require('graphql'); + const { codegenTransforms, fragmentTransforms, @@ -122,6 +128,9 @@ function getRelayFileWriter(baseDir: string) { function getSchema(schemaPath: string): GraphQLSchema { try { let source = fs.readFileSync(schemaPath, 'utf8'); + if (path.extname(schemaPath) === '.json') { + source = printSchema(buildClientSchema(JSON.parse(source).data)); + } source = ` directive @include(if: Boolean) on FRAGMENT | FIELD directive @skip(if: Boolean) on FRAGMENT | FIELD @@ -132,8 +141,8 @@ function getSchema(schemaPath: string): GraphQLSchema { return buildASTSchema(parse(source)); } catch (error) { throw new Error(` -Error loading schema. Expected the schema to be a .graphql file using the -GraphQL schema definition language. Error detail: +Error loading schema. Expected the schema to be a .graphql or a .json +file, describing your GraphQL server's API. Error detail: ${error.stack} `.trim()); @@ -162,7 +171,7 @@ const argv = yargs '$0 --schema --src [--watch]') .options({ 'schema': { - describe: 'Path to schema.graphql', + describe: 'Path to schema.graphql or schema.json', demandOption: true, type: 'string', },