@@ -7,14 +7,21 @@ import { DirectiveLocation } from '../../language/directiveLocation';
77
88import { printSchema } from '../../utilities/printSchema' ;
99
10+ import type { GraphQLCompositeType } from '../definition' ;
1011import {
1112 GraphQLInputObjectType ,
1213 GraphQLInterfaceType ,
1314 GraphQLList ,
1415 GraphQLObjectType ,
1516 GraphQLScalarType ,
17+ GraphQLUnionType ,
1618} from '../definition' ;
1719import { GraphQLDirective } from '../directives' ;
20+ import {
21+ SchemaMetaFieldDef ,
22+ TypeMetaFieldDef ,
23+ TypeNameMetaFieldDef ,
24+ } from '../introspection' ;
1825import { GraphQLBoolean , GraphQLInt , GraphQLString } from '../scalars' ;
1926import { GraphQLSchema } from '../schema' ;
2027
@@ -319,6 +326,109 @@ describe('Type System: Schema', () => {
319326 ) ;
320327 } ) ;
321328
329+ describe ( 'getField' , ( ) => {
330+ const petType = new GraphQLInterfaceType ( {
331+ name : 'Pet' ,
332+ fields : {
333+ name : { type : GraphQLString } ,
334+ } ,
335+ } ) ;
336+
337+ const catType = new GraphQLObjectType ( {
338+ name : 'Cat' ,
339+ interfaces : [ petType ] ,
340+ fields : {
341+ name : { type : GraphQLString } ,
342+ } ,
343+ } ) ;
344+
345+ const dogType = new GraphQLObjectType ( {
346+ name : 'Dog' ,
347+ interfaces : [ petType ] ,
348+ fields : {
349+ name : { type : GraphQLString } ,
350+ } ,
351+ } ) ;
352+
353+ const catOrDog = new GraphQLUnionType ( {
354+ name : 'CatOrDog' ,
355+ types : [ catType , dogType ] ,
356+ } ) ;
357+
358+ const queryType = new GraphQLObjectType ( {
359+ name : 'Query' ,
360+ fields : {
361+ catOrDog : { type : catOrDog } ,
362+ } ,
363+ } ) ;
364+
365+ const mutationType = new GraphQLObjectType ( {
366+ name : 'Mutation' ,
367+ fields : { } ,
368+ } ) ;
369+
370+ const subscriptionType = new GraphQLObjectType ( {
371+ name : 'Subscription' ,
372+ fields : { } ,
373+ } ) ;
374+
375+ const schema = new GraphQLSchema ( {
376+ query : queryType ,
377+ mutation : mutationType ,
378+ subscription : subscriptionType ,
379+ } ) ;
380+
381+ function expectField ( parentType : GraphQLCompositeType , name : string ) {
382+ return expect ( schema . getField ( parentType , name ) ) ;
383+ }
384+
385+ it ( 'returns known fields' , ( ) => {
386+ expectField ( petType , 'name' ) . to . equal ( petType . getFields ( ) . name ) ;
387+ expectField ( catType , 'name' ) . to . equal ( catType . getFields ( ) . name ) ;
388+
389+ expectField ( queryType , 'catOrDog' ) . to . equal (
390+ queryType . getFields ( ) . catOrDog ,
391+ ) ;
392+ } ) ;
393+
394+ it ( 'returns `undefined` for unknown fields' , ( ) => {
395+ expectField ( catOrDog , 'name' ) . to . equal ( undefined ) ;
396+
397+ expectField ( queryType , 'unknown' ) . to . equal ( undefined ) ;
398+ expectField ( petType , 'unknown' ) . to . equal ( undefined ) ;
399+ expectField ( catType , 'unknown' ) . to . equal ( undefined ) ;
400+ expectField ( catOrDog , 'unknown' ) . to . equal ( undefined ) ;
401+ } ) ;
402+
403+ it ( 'handles introspection fields' , ( ) => {
404+ expectField ( queryType , '__typename' ) . to . equal ( TypeNameMetaFieldDef ) ;
405+ expectField ( mutationType , '__typename' ) . to . equal ( TypeNameMetaFieldDef ) ;
406+ expectField ( subscriptionType , '__typename' ) . to . equal (
407+ TypeNameMetaFieldDef ,
408+ ) ;
409+
410+ expectField ( petType , '__typename' ) . to . equal ( TypeNameMetaFieldDef ) ;
411+ expectField ( catType , '__typename' ) . to . equal ( TypeNameMetaFieldDef ) ;
412+ expectField ( dogType , '__typename' ) . to . equal ( TypeNameMetaFieldDef ) ;
413+ expectField ( catOrDog , '__typename' ) . to . equal ( TypeNameMetaFieldDef ) ;
414+
415+ expectField ( queryType , '__type' ) . to . equal ( TypeMetaFieldDef ) ;
416+ expectField ( queryType , '__schema' ) . to . equal ( SchemaMetaFieldDef ) ;
417+ } ) ;
418+
419+ it ( 'returns `undefined` for introspection fields in wrong location' , ( ) => {
420+ expect ( schema . getField ( petType , '__type' ) ) . to . equal ( undefined ) ;
421+ expect ( schema . getField ( dogType , '__type' ) ) . to . equal ( undefined ) ;
422+ expect ( schema . getField ( mutationType , '__type' ) ) . to . equal ( undefined ) ;
423+ expect ( schema . getField ( subscriptionType , '__type' ) ) . to . equal ( undefined ) ;
424+
425+ expect ( schema . getField ( petType , '__schema' ) ) . to . equal ( undefined ) ;
426+ expect ( schema . getField ( dogType , '__schema' ) ) . to . equal ( undefined ) ;
427+ expect ( schema . getField ( mutationType , '__schema' ) ) . to . equal ( undefined ) ;
428+ expect ( schema . getField ( subscriptionType , '__schema' ) ) . to . equal ( undefined ) ;
429+ } ) ;
430+ } ) ;
431+
322432 describe ( 'Validity' , ( ) => {
323433 describe ( 'when not assumed valid' , ( ) => {
324434 it ( 'configures the schema to still needing validation' , ( ) => {
0 commit comments