Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Json schema to relay-compiler #1683

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/modern/RelayCompiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 13 additions & 4 deletions packages/relay-compiler/bin/RelayCompilerBin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems inefficient to build a client schema, print it, and then parse it back to schema again just a few lines later. Lets refactor to avoid this extra conversion, as it could slow down the compiler for apps with larger schemes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josephsavona could you give me some pointers on how to incorpore those directives in the GraphQLSchema. I am not familiar with the inner working of graphql-js, Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct fix to this is a much bigger refactoring. We shouldn't be doing this kind of directive patching in this file in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this current solution is reasonable until we do that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to leave a todo?
@leebyron What about my proposal to add support for GraphQL endpoint? Thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the compiler should directly rely on having network access, especially since many private company graphql schema endpoints require authentication which we shouldn't be building here.

However, I think it would be a great addition to have a simple script which allows you to provide a GraphQL endpoint and produce a schema.graphql file which you could later provide to the relay compiler

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/graphcool/get-graphql-schema does it

it can generate a .json or a .graphql schema from a graphql endpoint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I knew about graphql-config but not about get-graphql-schema. Thanks @sibelius

source = `
directive @include(if: Boolean) on FRAGMENT | FIELD
directive @skip(if: Boolean) on FRAGMENT | FIELD
Expand All @@ -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());
Expand Down Expand Up @@ -162,7 +171,7 @@ const argv = yargs
'$0 --schema <path> --src <path> [--watch]')
.options({
'schema': {
describe: 'Path to schema.graphql',
describe: 'Path to schema.graphql or schema.json',
demandOption: true,
type: 'string',
},
Expand Down