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

buildSchema: allow to reference introspection types #2608

Merged
merged 1 commit into from Jun 20, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/utilities/__tests__/buildASTSchema-test.js
Expand Up @@ -12,7 +12,7 @@ import { parse } from '../../language/parser';
import { print } from '../../language/printer';

import { validateSchema } from '../../type/validate';
import { __Schema } from '../../type/introspection';
import { __Schema, __EnumValue } from '../../type/introspection';
import {
assertDirective,
GraphQLSkipDirective,
Expand Down Expand Up @@ -1089,6 +1089,21 @@ describe('Schema Builder', () => {
expect(schema.getType('__Schema')).to.equal(__Schema);
});

it('Allows to reference introspection types', () => {
const schema = buildSchema(`
type Query {
introspectionField: __EnumValue
}
`);

const queryType = assertObjectType(schema.getType('Query'));
expect(queryType.getFields()).to.have.nested.property(
'introspectionField.type',
__EnumValue,
);
expect(schema.getType('__EnumValue')).to.equal(__EnumValue);
});

it('Rejects invalid SDL', () => {
const sdl = `
type Query {
Expand Down
13 changes: 10 additions & 3 deletions src/validation/__tests__/KnownTypeNamesRule-test.js
Expand Up @@ -35,11 +35,16 @@ function expectValidSDL(sdlStr, schema) {
describe('Validate: Known type names', () => {
it('known type names are valid', () => {
expectValid(`
query Foo($var: String, $required: [String!]!) {
query Foo(
$var: String
$required: [Int!]!
$introspectionType: __EnumValue
) {
user(id: 4) {
pets { ... on Pet { name }, ...PetFields, ... { name } }
}
}

fragment PetFields on Pet {
name
}
Expand Down Expand Up @@ -97,14 +102,15 @@ describe('Validate: Known type names', () => {
});

describe('within SDL', () => {
it('use standard scalars', () => {
it('use standard types', () => {
expectValidSDL(`
type Query {
string: String
int: Int
float: Float
boolean: Boolean
id: ID
introspectionType: __EnumValue
}
`);
});
Expand Down Expand Up @@ -239,7 +245,7 @@ describe('Validate: Known type names', () => {
]);
});

it('reference standard scalars inside extension document', () => {
it('reference standard types inside extension document', () => {
const schema = buildSchema('type Foo');
const sdl = `
type SomeType {
Expand All @@ -248,6 +254,7 @@ describe('Validate: Known type names', () => {
float: Float
boolean: Boolean
id: ID
introspectionType: __EnumValue
}
`;

Expand Down
14 changes: 9 additions & 5 deletions src/validation/rules/KnownTypeNamesRule.js
Expand Up @@ -14,6 +14,7 @@ import {
} from '../../language/predicates';

import { specifiedScalarTypes } from '../../type/scalars';
import { introspectionTypes } from '../../type/introspection';

import type {
ValidationContext,
Expand Down Expand Up @@ -49,13 +50,13 @@ export function KnownTypeNamesRule(
if (!existingTypesMap[typeName] && !definedTypes[typeName]) {
const definitionNode = ancestors[2] ?? parent;
const isSDL = definitionNode != null && isSDLNode(definitionNode);
if (isSDL && isSpecifiedScalarName(typeName)) {
if (isSDL && isStandardTypeName(typeName)) {
return;
}

const suggestedTypes = suggestionList(
typeName,
isSDL ? specifiedScalarsNames.concat(typeNames) : typeNames,
isSDL ? standardTypeNames.concat(typeNames) : typeNames,
);
context.reportError(
new GraphQLError(
Expand All @@ -68,9 +69,12 @@ export function KnownTypeNamesRule(
};
}

const specifiedScalarsNames = specifiedScalarTypes.map((type) => type.name);
function isSpecifiedScalarName(typeName) {
return specifiedScalarsNames.indexOf(typeName) !== -1;
const standardTypeNames = [...specifiedScalarTypes, ...introspectionTypes].map(
(type) => type.name,
);

function isStandardTypeName(typeName) {
return standardTypeNames.indexOf(typeName) !== -1;
}

function isSDLNode(value: ASTNode | $ReadOnlyArray<ASTNode>): boolean {
Expand Down