Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow resolveType to return a type name string #509

Merged
merged 1 commit into from
Oct 10, 2016

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.

None yet

4 participants