Skip to content

Commit

Permalink
buildSchema: allow to reference introspection types
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 4, 2020
1 parent fe4f8eb commit b6c792f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
15 changes: 14 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,19 @@ describe('Schema Builder', () => {
expect(schema.getType('__Schema')).to.equal(__Schema);
});

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

expect(
schema.getType('Query').getFields()['introspectionField'].type,
).to.equal(__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
15 changes: 10 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,13 @@ 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

0 comments on commit b6c792f

Please sign in to comment.