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 'introspectionFromSchema' utility function #1187

Merged
merged 1 commit into from
Jan 8, 2018
Merged
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: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ export {
introspectionQuery,
// Gets the target Operation from a Document
getOperationAST,
// Convert a GraphQLSchema to an IntrospectionQuery
introspectionFromSchema,
// Build a GraphQLSchema from an introspection result.
buildClientSchema,
// Build a GraphQLSchema from a parsed GraphQL Schema language AST.
Expand Down
66 changes: 66 additions & 0 deletions src/utilities/__tests__/introspectionFromSchema-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import { describe, it } from 'mocha';
import { expect } from 'chai';

import dedent from '../../jsutils/dedent';
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from '../../type';
import { printSchema } from '../schemaPrinter';
import { buildClientSchema } from '../buildClientSchema';
import { introspectionFromSchema } from '../introspectionFromSchema';

function introspectionToSDL(introspection) {
return printSchema(buildClientSchema(introspection));
}

describe('introspectionFromSchema', () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Simple',
description: 'This is a simple type',
fields: {
string: {
type: GraphQLString,
description: 'This is a string field',
},
},
}),
});

it('converts a simple schema', () => {
const introspection = introspectionFromSchema(schema);

expect(introspectionToSDL(introspection)).to.deep.equal(dedent`
schema {
query: Simple
}

"""This is a simple type"""
type Simple {
"""This is a string field"""
string: String
}
`);
});

it('converts a simple schema without descriptions', () => {
const introspection = introspectionFromSchema(schema, {
descriptions: false,
});

expect(introspectionToSDL(introspection)).to.deep.equal(dedent`
schema {
query: Simple
}

type Simple {
string: String
}
`);
});
});
3 changes: 3 additions & 0 deletions src/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export type {
// Gets the target Operation from a Document
export { getOperationAST } from './getOperationAST';

// Convert a GraphQLSchema to an IntrospectionQuery
export { introspectionFromSchema } from './introspectionFromSchema';

// Build a GraphQLSchema from an introspection result.
export { buildClientSchema } from './buildClientSchema';

Expand Down
37 changes: 37 additions & 0 deletions src/utilities/introspectionFromSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import invariant from '../jsutils/invariant';
import { getIntrospectionQuery } from './introspectionQuery';
import { GraphQLSchema } from '../type/schema';
import { execute } from '../execution/execute';
import { parse } from '../language/parser';
import type {
IntrospectionQuery,
IntrospectionOptions,
} from './introspectionQuery';

/**
* Build an IntrospectionQuery from a GraphQLSchema
*
* IntrospectionQuery is useful for utilities that care about type and field
* relationships, but do not need to traverse through those relationships.
*
* This is the inverse of buildClientSchema. The primary use case is outside
* of the server context, for instance when doing schema comparisons.
*/
export function introspectionFromSchema(
schema: GraphQLSchema,
options: IntrospectionOptions,
): IntrospectionQuery {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a note, it seems like the response type of this should be named IntrospectionResult

Copy link
Member Author

Choose a reason for hiding this comment

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

@leebyron It's pretty confusing name but it already defined here:

export type IntrospectionQuery = {|

Would be great to rename it to IntrospectionResult but I'm not sure how much code it will brake 😞

const queryAST = parse(getIntrospectionQuery(options));
const result = execute(schema, queryAST);
invariant(!result.then && !result.errors && result.data);
return (result.data: any);
}