Skip to content

Add missing Flow typing for 'print' function #1702

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

Merged
merged 1 commit into from
Feb 1, 2019
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
4 changes: 3 additions & 1 deletion src/language/__tests__/printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @flow strict
*/

import { expect } from 'chai';
Expand All @@ -29,6 +29,7 @@ describe('Printer: Query document', () => {

it('produces helpful error messages', () => {
const badAst1 = { random: 'Data' };
// $DisableFlowOnNegativeTest
expect(() => print(badAst1)).to.throw(
'Invalid AST Node: { random: "Data" }',
);
Expand Down Expand Up @@ -166,6 +167,7 @@ describe('Printer: Query document', () => {
const printed = print(parse(kitchenSinkQuery));

expect(printed).to.equal(
// $FlowFixMe
dedent(String.raw`
query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery {
whoever123is: node(id: [123, 456]) {
Expand Down
1 change: 1 addition & 0 deletions src/language/__tests__/schema-printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Printer: SDL document', () => {

it('produces helpful error messages', () => {
const badAst1 = { random: 'Data' };
// $DisableFlowOnNegativeTest
expect(() => print(badAst1)).to.throw(
'Invalid AST Node: { random: "Data" }',
);
Expand Down
8 changes: 5 additions & 3 deletions src/language/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @flow strict
*/

import type { ASTNode } from './ast';
import { visit } from './visitor';

/**
* Converts an AST into a string, using one set of reasonable
* formatting rules.
*/
export function print(ast) {
export function print(ast: ASTNode): string {
return visit(ast, { leave: printDocASTReducer });
}

const printDocASTReducer = {
// TODO: provide better type coverage in future
const printDocASTReducer: any = {
Name: node => node.value,
Variable: node => '$' + node.name,

Expand Down
9 changes: 4 additions & 5 deletions src/type/introspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import objectValues from '../polyfills/objectValues';
import isInvalid from '../jsutils/isInvalid';
import { astFromValue } from '../utilities/astFromValue';
import { print } from '../language/printer';
import {
Expand Down Expand Up @@ -348,10 +347,10 @@ export const __InputValue = new GraphQLObjectType({
description:
'A GraphQL-formatted string representing the default value for this ' +
'input value.',
resolve: inputVal =>
isInvalid(inputVal.defaultValue)
? null
: print(astFromValue(inputVal.defaultValue, inputVal.type)),
resolve(inputVal) {
const valueAST = astFromValue(inputVal.defaultValue, inputVal.type);
return valueAST ? print(valueAST) : null;
},
},
}),
});
Expand Down
29 changes: 19 additions & 10 deletions src/utilities/__tests__/buildASTSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ function cycleSDL(sdl, options = {}) {
return printSchema(schema, { commentDescriptions });
}

function printNode(node) {
invariant(node);
return print(node);
}

describe('Schema Builder', () => {
it('can use built schema for limited execution', () => {
const schema = buildASTSchema(
Expand Down Expand Up @@ -678,8 +683,9 @@ describe('Schema Builder', () => {

directive @test(arg: TestScalar) on FIELD
`;
const ast = parse(sdl, { noLocation: true });

const schema = buildSchema(sdl);
const schema = buildASTSchema(ast);
const query = assertObjectType(schema.getType('Query'));
const testInput = assertInputObjectType(schema.getType('TestInput'));
const testEnum = assertEnumType(schema.getType('TestEnum'));
Expand All @@ -702,28 +708,31 @@ describe('Schema Builder', () => {
testScalar.astNode,
testDirective.astNode,
],
loc: undefined,
};
expect(print(restoredSchemaAST)).to.be.equal(sdl);
expect(restoredSchemaAST).to.be.deep.equal(ast);

const testField = query.getFields().testField;
expect(print(testField.astNode)).to.equal(
expect(printNode(testField.astNode)).to.equal(
'testField(testArg: TestInput): TestUnion',
);
expect(print(testField.args[0].astNode)).to.equal('testArg: TestInput');
expect(print(testInput.getFields().testInputField.astNode)).to.equal(
expect(printNode(testField.args[0].astNode)).to.equal('testArg: TestInput');
expect(printNode(testInput.getFields().testInputField.astNode)).to.equal(
'testInputField: TestEnum',
);
const testEnumValue = testEnum.getValue('TEST_VALUE');
invariant(testEnumValue);
expect(print(testEnumValue.astNode)).to.equal('TEST_VALUE');
expect(printNode(testEnumValue.astNode)).to.equal('TEST_VALUE');

expect(print(testInterface.getFields().interfaceField.astNode)).to.equal(
expect(
printNode(testInterface.getFields().interfaceField.astNode),
).to.equal('interfaceField: String');
expect(printNode(testType.getFields().interfaceField.astNode)).to.equal(
'interfaceField: String',
);
expect(print(testType.getFields().interfaceField.astNode)).to.equal(
'interfaceField: String',
expect(printNode(testDirective.args[0].astNode)).to.equal(
'arg: TestScalar',
);
expect(print(testDirective.args[0].astNode)).to.equal('arg: TestScalar');
});

it('Root operation types with custom names', () => {
Expand Down
45 changes: 25 additions & 20 deletions src/utilities/__tests__/extendSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,18 @@ const testSchemaDefinitions = testSchemaAST.definitions.map(print);
function printTestSchemaChanges(extendedSchema) {
const ast = parse(printSchema(extendedSchema));
return print({
...ast,
kind: Kind.DOCUMENT,
definitions: ast.definitions.filter(
node => !testSchemaDefinitions.includes(print(node)),
),
});
}

function printNode(node) {
invariant(node);
return print(node);
}

describe('extendSchema', () => {
it('returns the original schema when there are no type definitions', () => {
const extendedSchema = extendTestSchema('{ field }');
Expand Down Expand Up @@ -476,49 +481,49 @@ describe('extendSchema', () => {
).to.be.equal(printSchema(extendedTwiceSchema));

const newField = query.getFields().newField;
expect(print(newField.astNode)).to.equal(
expect(printNode(newField.astNode)).to.equal(
'newField(testArg: TestInput): TestEnum',
);
expect(print(newField.args[0].astNode)).to.equal('testArg: TestInput');
expect(print(query.getFields().oneMoreNewField.astNode)).to.equal(
expect(printNode(newField.args[0].astNode)).to.equal('testArg: TestInput');
expect(printNode(query.getFields().oneMoreNewField.astNode)).to.equal(
'oneMoreNewField: TestUnion',
);

const newValue = someEnum.getValue('NEW_VALUE');
invariant(newValue);
expect(print(newValue.astNode)).to.equal('NEW_VALUE');
expect(printNode(newValue.astNode)).to.equal('NEW_VALUE');

const oneMoreNewValue = someEnum.getValue('ONE_MORE_NEW_VALUE');
invariant(oneMoreNewValue);
expect(print(oneMoreNewValue.astNode)).to.equal('ONE_MORE_NEW_VALUE');
expect(print(someInput.getFields().newField.astNode)).to.equal(
expect(printNode(oneMoreNewValue.astNode)).to.equal('ONE_MORE_NEW_VALUE');
expect(printNode(someInput.getFields().newField.astNode)).to.equal(
'newField: String',
);
expect(print(someInput.getFields().oneMoreNewField.astNode)).to.equal(
expect(printNode(someInput.getFields().oneMoreNewField.astNode)).to.equal(
'oneMoreNewField: String',
);
expect(print(someInterface.getFields().newField.astNode)).to.equal(
expect(printNode(someInterface.getFields().newField.astNode)).to.equal(
'newField: String',
);
expect(print(someInterface.getFields().oneMoreNewField.astNode)).to.equal(
'oneMoreNewField: String',
);
expect(
printNode(someInterface.getFields().oneMoreNewField.astNode),
).to.equal('oneMoreNewField: String');

expect(print(testInput.getFields().testInputField.astNode)).to.equal(
expect(printNode(testInput.getFields().testInputField.astNode)).to.equal(
'testInputField: TestEnum',
);

const testValue = testEnum.getValue('TEST_VALUE');
invariant(testValue);
expect(print(testValue.astNode)).to.equal('TEST_VALUE');
expect(printNode(testValue.astNode)).to.equal('TEST_VALUE');

expect(print(testInterface.getFields().interfaceField.astNode)).to.equal(
'interfaceField: String',
);
expect(print(testType.getFields().interfaceField.astNode)).to.equal(
expect(
printNode(testInterface.getFields().interfaceField.astNode),
).to.equal('interfaceField: String');
expect(printNode(testType.getFields().interfaceField.astNode)).to.equal(
'interfaceField: String',
);
expect(print(testDirective.args[0].astNode)).to.equal('arg: Int');
expect(printNode(testDirective.args[0].astNode)).to.equal('arg: Int');
});

it('builds types with deprecated fields/values', () => {
Expand Down Expand Up @@ -1173,7 +1178,7 @@ describe('extendSchema', () => {

const queryType = schema.getQueryType();
expect(queryType).to.include({ name: 'Foo' });
expect(print(schema.astNode)).to.equal(extensionSDL);
expect(printNode(schema.astNode)).to.equal(extensionSDL);
});

it('adds new root types via schema extension', () => {
Expand Down
20 changes: 7 additions & 13 deletions src/utilities/schemaPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import flatMap from '../polyfills/flatMap';
import objectValues from '../polyfills/objectValues';
import isNullish from '../jsutils/isNullish';
import isInvalid from '../jsutils/isInvalid';
import { astFromValue } from '../utilities/astFromValue';
import { print } from '../language/printer';
import type { GraphQLSchema } from '../type/schema';
Expand Down Expand Up @@ -297,9 +295,10 @@ function printArgs(options, args, indentation = '') {
}

function printInputValue(arg) {
const defaultAST = astFromValue(arg.defaultValue, arg.type);
let argDecl = arg.name + ': ' + String(arg.type);
if (!isInvalid(arg.defaultValue)) {
argDecl += ` = ${print(astFromValue(arg.defaultValue, arg.type))}`;
if (defaultAST) {
argDecl += ` = ${print(defaultAST)}`;
}
return argDecl;
}
Expand All @@ -320,16 +319,11 @@ function printDeprecated(fieldOrEnumVal) {
return '';
}
const reason = fieldOrEnumVal.deprecationReason;
if (
isNullish(reason) ||
reason === '' ||
reason === DEFAULT_DEPRECATION_REASON
) {
return ' @deprecated';
const reasonAST = astFromValue(reason, GraphQLString);
if (reasonAST && reason !== '' && reason !== DEFAULT_DEPRECATION_REASON) {
return ' @deprecated(reason: ' + print(reasonAST) + ')';
}
return (
' @deprecated(reason: ' + print(astFromValue(reason, GraphQLString)) + ')'
);
return ' @deprecated';
}

function printDescription(
Expand Down