Skip to content

Commit 6d2fa48

Browse files
committed
Print Schema Tool
This tool accepts an input graphql schema definition file and outputs the corresponding introspection query result for that file
1 parent 697a6ca commit 6d2fa48

File tree

5 files changed

+99
-7
lines changed

5 files changed

+99
-7
lines changed

src/language/schema/__tests__/materializer.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
*
55
* This source code is licensed under the BSD-style license found in the
66
* LICENSE file in the root directory of this source tree. An additional grant
7-
* of patent rights can be found in the PATENTS file in the same directory.
7+
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

1010
import { expect } from 'chai';
1111
import { describe, it } from 'mocha';
1212
import { parseSchema } from '../parser';
1313
import { materializeSchema } from '../materializer';
1414
import { printSchema } from '../../../type/printer';
15-
import { introspectionQuery } from '../../../type/introspectionQuery';
16-
import { graphql } from '../../../';
15+
import { getIntrospectionResult } from '../printer';
1716

1817
// 80+ char lines are useful in describe/it, so ignore in this file.
1918
/*eslint-disable max-len */
@@ -23,9 +22,7 @@ function printForTest(result) {
2322
}
2423

2524
async function getOutput(body, queryType) {
26-
var doc = parseSchema(body);
27-
var schema = materializeSchema(doc, queryType);
28-
var result = await graphql(schema, introspectionQuery);
25+
var result = await getIntrospectionResult(body, queryType);
2926
return await printForTest(result);
3027
}
3128

@@ -272,6 +269,6 @@ type Hello {
272269
}
273270
`;
274271
var doc = parseSchema(body);
275-
expect(() => materializeSchema(doc, 'Wat')).to.throw('Type Wat not found in document');
272+
expect(() => materializeSchema(doc, 'Wat')).to.throw('Specified query type Wat not found in document');
276273
});
277274
});

src/language/schema/materializer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ export function materializeSchema(
111111

112112
var astMap = makeDict(ast.definitions, d => d.name.value);
113113

114+
if (nullish(astMap[queryTypeName])) {
115+
throw new Error('Specified query type ' + queryTypeName +
116+
' not found in document.');
117+
}
118+
114119
/**
115120
* This generates a function that allows you to produce
116121
* type definitions on demand. We produce the function

src/language/schema/printer.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
import { parseSchema } from './parser';
11+
import { materializeSchema } from './materializer';
12+
import { introspectionQuery } from './../../type/introspectionQuery';
13+
import { graphql } from '../../';
14+
15+
export async function getIntrospectionResult(body, queryType) {
16+
var doc = parseSchema(body);
17+
var schema = materializeSchema(doc, queryType);
18+
return await graphql(schema, introspectionQuery);
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
require('babel/register')({
11+
optional: ['runtime', 'es7.asyncFunctions']
12+
});
13+
14+
require('./src').executeTool();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
import { getIntrospectionResult }
11+
from '../../../../src/language/schema/printer';
12+
13+
var Promise = require('bluebird');
14+
var parseArgs = require('minimist');
15+
var fs = require('fs');
16+
17+
Promise.promisifyAll(fs);
18+
19+
export async function executeTool() {
20+
try {
21+
var argDict = parseArgs(process.argv.slice(2));
22+
23+
if (argDict['?'] !== undefined || argDict.help !== undefined) {
24+
console.log(helpString);
25+
process.exit(0);
26+
}
27+
28+
if (argDict.query === undefined) {
29+
console.log('--query is required');
30+
console.log(helpString);
31+
process.exit(0);
32+
}
33+
34+
if (argDict.file === undefined) {
35+
console.log('--file is required');
36+
console.log(helpString);
37+
process.exit(0);
38+
}
39+
40+
var body = await fs.readFileAsync(argDict.file);
41+
var result = await getIntrospectionResult(body, argDict.query);
42+
var out = await JSON.stringify(result, null, 2);
43+
console.log(out);
44+
} catch (error) {
45+
console.error(error);
46+
console.error(error.stack);
47+
}
48+
}
49+
50+
var helpString = `
51+
This tool consumes GraphQL schema definition files and outputs the
52+
introspection query result from querying that schema.
53+
54+
Required:
55+
56+
--file <path>: The path to the input schema definition file.
57+
--query <queryType>: The query type (root type) of the schema.`;

0 commit comments

Comments
 (0)