Skip to content

Commit 863fe2b

Browse files
IvanGoncharovleebyron
authored andcommitted
Add 'introspectionFromSchema' utility function (#1187)
Based on #1114 by @mjmahone
1 parent 802e518 commit 863fe2b

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ export {
309309
introspectionQuery,
310310
// Gets the target Operation from a Document
311311
getOperationAST,
312+
// Convert a GraphQLSchema to an IntrospectionQuery
313+
introspectionFromSchema,
312314
// Build a GraphQLSchema from an introspection result.
313315
buildClientSchema,
314316
// Build a GraphQLSchema from a parsed GraphQL Schema language AST.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import { describe, it } from 'mocha';
9+
import { expect } from 'chai';
10+
11+
import dedent from '../../jsutils/dedent';
12+
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from '../../type';
13+
import { printSchema } from '../schemaPrinter';
14+
import { buildClientSchema } from '../buildClientSchema';
15+
import { introspectionFromSchema } from '../introspectionFromSchema';
16+
17+
function introspectionToSDL(introspection) {
18+
return printSchema(buildClientSchema(introspection));
19+
}
20+
21+
describe('introspectionFromSchema', () => {
22+
const schema = new GraphQLSchema({
23+
query: new GraphQLObjectType({
24+
name: 'Simple',
25+
description: 'This is a simple type',
26+
fields: {
27+
string: {
28+
type: GraphQLString,
29+
description: 'This is a string field',
30+
},
31+
},
32+
}),
33+
});
34+
35+
it('converts a simple schema', () => {
36+
const introspection = introspectionFromSchema(schema);
37+
38+
expect(introspectionToSDL(introspection)).to.deep.equal(dedent`
39+
schema {
40+
query: Simple
41+
}
42+
43+
"""This is a simple type"""
44+
type Simple {
45+
"""This is a string field"""
46+
string: String
47+
}
48+
`);
49+
});
50+
51+
it('converts a simple schema without descriptions', () => {
52+
const introspection = introspectionFromSchema(schema, {
53+
descriptions: false,
54+
});
55+
56+
expect(introspectionToSDL(introspection)).to.deep.equal(dedent`
57+
schema {
58+
query: Simple
59+
}
60+
61+
type Simple {
62+
string: String
63+
}
64+
`);
65+
});
66+
});

src/utilities/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export type {
4141
// Gets the target Operation from a Document
4242
export { getOperationAST } from './getOperationAST';
4343

44+
// Convert a GraphQLSchema to an IntrospectionQuery
45+
export { introspectionFromSchema } from './introspectionFromSchema';
46+
4447
// Build a GraphQLSchema from an introspection result.
4548
export { buildClientSchema } from './buildClientSchema';
4649

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import invariant from '../jsutils/invariant';
11+
import { getIntrospectionQuery } from './introspectionQuery';
12+
import { GraphQLSchema } from '../type/schema';
13+
import { execute } from '../execution/execute';
14+
import { parse } from '../language/parser';
15+
import type {
16+
IntrospectionQuery,
17+
IntrospectionOptions,
18+
} from './introspectionQuery';
19+
20+
/**
21+
* Build an IntrospectionQuery from a GraphQLSchema
22+
*
23+
* IntrospectionQuery is useful for utilities that care about type and field
24+
* relationships, but do not need to traverse through those relationships.
25+
*
26+
* This is the inverse of buildClientSchema. The primary use case is outside
27+
* of the server context, for instance when doing schema comparisons.
28+
*/
29+
export function introspectionFromSchema(
30+
schema: GraphQLSchema,
31+
options: IntrospectionOptions,
32+
): IntrospectionQuery {
33+
const queryAST = parse(getIntrospectionQuery(options));
34+
const result = execute(schema, queryAST);
35+
invariant(!result.then && !result.errors && result.data);
36+
return (result.data: any);
37+
}

0 commit comments

Comments
 (0)