diff --git a/src/utilities/__tests__/buildASTSchema-test.js b/src/utilities/__tests__/buildASTSchema-test.js index 25a353a8691..666ff7b9522 100644 --- a/src/utilities/__tests__/buildASTSchema-test.js +++ b/src/utilities/__tests__/buildASTSchema-test.js @@ -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, @@ -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 { diff --git a/src/validation/__tests__/KnownTypeNamesRule-test.js b/src/validation/__tests__/KnownTypeNamesRule-test.js index adfe354a281..584ecab1198 100644 --- a/src/validation/__tests__/KnownTypeNamesRule-test.js +++ b/src/validation/__tests__/KnownTypeNamesRule-test.js @@ -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 } @@ -97,7 +102,7 @@ describe('Validate: Known type names', () => { }); describe('within SDL', () => { - it('use standard scalars', () => { + it('use standard types', () => { expectValidSDL(` type Query { string: String @@ -105,6 +110,7 @@ describe('Validate: Known type names', () => { float: Float boolean: Boolean id: ID + introspectionType: __EnumValue } `); }); @@ -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 { @@ -248,6 +254,7 @@ describe('Validate: Known type names', () => { float: Float boolean: Boolean id: ID + introspectionType: __EnumValue } `; diff --git a/src/validation/rules/KnownTypeNamesRule.js b/src/validation/rules/KnownTypeNamesRule.js index 80f44c38668..357a0533d72 100644 --- a/src/validation/rules/KnownTypeNamesRule.js +++ b/src/validation/rules/KnownTypeNamesRule.js @@ -14,6 +14,7 @@ import { } from '../../language/predicates'; import { specifiedScalarTypes } from '../../type/scalars'; +import { introspectionTypes } from '../../type/introspection'; import { type ValidationContext, @@ -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( @@ -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): boolean {