Skip to content

Commit bb031be

Browse files
author
Tasveer Singh
authored
Merge pull request #1 from lightsofapollo/add-introspection-query-json
Add handling for introspection query schema.
2 parents aa9571a + b951c21 commit bb031be

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

dist/getSchema.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,30 @@ var _fs = require('fs');
1111

1212
var _fs2 = _interopRequireDefault(_fs);
1313

14+
var _path = require('path');
15+
16+
var _path2 = _interopRequireDefault(_path);
17+
1418
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1519

1620
function getSchema(schemaPath) {
1721
try {
1822
let source = _fs2.default.readFileSync(schemaPath, 'utf8');
23+
if (_path2.default.extname(schemaPath) === '.json') {
24+
source = (0, _graphql.printSchema)((0, _graphql.buildClientSchema)(JSON.parse(source).data));
25+
}
1926
source = `
20-
directive @include(if: Boolean) on FRAGMENT | FIELD
21-
directive @skip(if: Boolean) on FRAGMENT | FIELD
22-
directive @relay(pattern: Boolean, plural: Boolean) on FRAGMENT | FIELD
23-
${source}
24-
`;
27+
directive @include(if: Boolean) on FRAGMENT | FIELD
28+
directive @skip(if: Boolean) on FRAGMENT | FIELD
29+
directive @relay(pattern: Boolean, plural: Boolean) on FRAGMENT | FIELD
30+
${source}
31+
`;
32+
2533
return (0, _graphql.buildASTSchema)((0, _graphql.parse)(source));
2634
} catch (error) {
2735
throw new Error(`
28-
Error loading schema. Expected the schema to be a .graphql file using the
29-
GraphQL schema definition language. Error detail:
30-
36+
Error loading schema. Expected the schema to be a .graphql or a .json
37+
file, describing your GraphQL server's API. Error detail:
3138
${error.stack}
3239
`.trim());
3340
}

src/getSchema.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
// @flow
22

3-
import { buildASTSchema, parse } from 'graphql'
3+
import {
4+
printSchema,
5+
buildClientSchema,
6+
buildASTSchema,
7+
parse
8+
} from 'graphql'
49
import fs from 'fs'
10+
import path from 'path'
511

612
import type { GraphQLSchema } from 'graphql'
713

8-
export default function getSchema (schemaPath: string): GraphQLSchema {
14+
export default function getSchema(schemaPath: string): GraphQLSchema {
915
try {
1016
let source = fs.readFileSync(schemaPath, 'utf8')
17+
if (path.extname(schemaPath) === '.json') {
18+
source = printSchema(buildClientSchema(JSON.parse(source).data))
19+
}
1120
source = `
12-
directive @include(if: Boolean) on FRAGMENT | FIELD
13-
directive @skip(if: Boolean) on FRAGMENT | FIELD
14-
directive @relay(pattern: Boolean, plural: Boolean) on FRAGMENT | FIELD
15-
${source}
16-
`
21+
directive @include(if: Boolean) on FRAGMENT | FIELD
22+
directive @skip(if: Boolean) on FRAGMENT | FIELD
23+
directive @relay(pattern: Boolean, plural: Boolean) on FRAGMENT | FIELD
24+
${source}
25+
`
26+
1727
return buildASTSchema(parse(source))
1828
} catch (error) {
1929
throw new Error(`
20-
Error loading schema. Expected the schema to be a .graphql file using the
21-
GraphQL schema definition language. Error detail:
22-
30+
Error loading schema. Expected the schema to be a .graphql or a .json
31+
file, describing your GraphQL server's API. Error detail:
2332
${error.stack}
2433
`.trim())
2534
}

0 commit comments

Comments
 (0)