File tree Expand file tree Collapse file tree
packages/apollo-graphql/src/schema Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717- ` apollo-env `
1818 - <First ` apollo-env ` related entry goes here>
1919- ` apollo-graphql `
20- - <First ` apollo-graphql ` related entry goes here>
20+ - buildSchemaFromSDL - support meta fields on abstract types [ # 1330 ] ( https://github.com/apollographql/apollo-tooling/pull/1330 )
2121- ` apollo-language-server `
2222 - <First ` apollo-codegen-core ` related entry goes here>
2323- ` apollo-tools `
Original file line number Diff line number Diff line change 44 GraphQLSchema ,
55 GraphQLDirective ,
66 DirectiveLocation ,
7- GraphQLObjectType
7+ GraphQLObjectType ,
8+ GraphQLAbstractType
89} from "graphql" ;
910
1011import astSerializer from "./snapshotSerializers/astSerializer" ;
@@ -345,6 +346,7 @@ type MutationRoot {
345346 ) ;
346347 } ) ;
347348 } ) ;
349+
348350 describe ( `resolvers` , ( ) => {
349351 it ( `should add a resolver for a field` , ( ) => {
350352 const name = ( ) => { } ;
@@ -372,5 +374,53 @@ type MutationRoot {
372374
373375 expect ( nameField . resolve ) . toEqual ( name ) ;
374376 } ) ;
377+
378+ it ( `should add meta fields to abstract types` , ( ) => {
379+ const typeDefs = gql `
380+ union Animal = Dog
381+
382+ interface Creature {
383+ name: String!
384+ legs: Int!
385+ }
386+
387+ type Dog {
388+ id: ID!
389+ }
390+
391+ type Cat implements Creature {
392+ meow: Boolean!
393+ }
394+ ` ;
395+
396+ const resolveTypeUnion = ( obj : any ) => {
397+ return "Dog" ;
398+ } ;
399+
400+ const resolveTypeInterface = ( obj : any ) => {
401+ if ( obj . meow ) {
402+ return "Cat" ;
403+ }
404+ throw Error ( "Couldn't resolve interface" ) ;
405+ } ;
406+
407+ const resolvers = {
408+ Animal : {
409+ __resolveType : resolveTypeUnion
410+ } ,
411+ Creature : {
412+ __resolveType : resolveTypeInterface
413+ }
414+ } ;
415+
416+ const schema = buildSchemaFromSDL ( [ { typeDefs, resolvers } ] ) ;
417+ const animalUnion = schema . getType ( "Animal" ) as GraphQLAbstractType ;
418+ const creatureInterface = schema . getType (
419+ "Creature"
420+ ) as GraphQLAbstractType ;
421+
422+ expect ( animalUnion . resolveType ) . toBe ( resolveTypeUnion ) ;
423+ expect ( creatureInterface . resolveType ) . toBe ( resolveTypeInterface ) ;
424+ } ) ;
375425 } ) ;
376426} ) ;
Original file line number Diff line number Diff line change @@ -13,7 +13,8 @@ import {
1313 SchemaDefinitionNode ,
1414 SchemaExtensionNode ,
1515 OperationTypeNode ,
16- GraphQLObjectType
16+ GraphQLObjectType ,
17+ isAbstractType
1718} from "graphql" ;
1819import { validateSDL } from "graphql/validation/validate" ;
1920import { isDocumentNode , isNode } from "../utilities/graphql" ;
@@ -206,6 +207,15 @@ export function addResolversToSchema(
206207) {
207208 for ( const [ typeName , fieldConfigs ] of Object . entries ( resolvers ) ) {
208209 const type = schema . getType ( typeName ) ;
210+
211+ if ( isAbstractType ( type ) ) {
212+ for ( const [ fieldName , fieldConfig ] of Object . entries ( fieldConfigs ) ) {
213+ if ( fieldName . startsWith ( "__" ) ) {
214+ ( type as any ) [ fieldName . substring ( 2 ) ] = fieldConfig ;
215+ }
216+ }
217+ }
218+
209219 if ( ! isObjectType ( type ) ) continue ;
210220
211221 const fieldMap = type . getFields ( ) ;
You can’t perform that action at this time.
0 commit comments