Skip to content

Conversation

@tgriesser
Copy link
Contributor

@tgriesser tgriesser commented Oct 6, 2016

This change permits resolveType functions to return the "name" string of a type definition rather than the concrete GraphQLObjectType instance.

This is particularly useful when the resolveType function doesn't have access to the type instances in the immediate closure scope. While it's possible to accomplish the same with info.schema.getType in the resolve function, being able to return a string and have the executor figure it out felt a bit nicer.

Example:

const PetType = new GraphQLInterfaceType({
  name: 'Pet',
  resolveType(obj) {
    return obj instanceof Dog ? 'Dog' :
      obj instanceof Cat ? 'Cat' :
      null;
  },
  fields: {
    name: { type: GraphQLString }
  }
});

@leebyron leebyron merged commit c82ff68 into graphql:master Oct 10, 2016
@leebyron
Copy link
Contributor

This is cool - nice idea.

@istarkov
Copy link

istarkov commented Oct 13, 2016

@leebyron having this isn't it will be good to make default resolveType as

resolveType(obj) {  
   // checks on object ...
   return obj.constructor.name;
}

So it will work with es6 classes, and will allow to define interfaces using graphql schema language?
So examples like here http://graphql.org/graphql-js/object-types/ will also could use interfaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants