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

NotImplementedError (schema contains Interfaces or Unions, so you must define a resolve_type -> (obj, ctx) { ... } function): #1205

Closed
ghost opened this issue Dec 24, 2017 · 9 comments

Comments

@ghost
Copy link

ghost commented Dec 24, 2017

If I follow this guide: http://tech.eshaiju.in/blog/2017/06/09/dry-graphql-definitions-using-interfaces/

I have this error:
NotImplementedError (schema contains Interfaces or Unions, so you must define a resolve_type -> (obj, ctx) { ... } function):

I'm using a simple player_type.rb:

Types::PlayerType = GraphQL::ObjectType.define do
  name 'Player'

  interfaces [Interfaces::ActiveRecordTimestamp]

  field :id, !types.ID
  field :team_id, !types.ID
end

and this:

Interfaces::ActiveRecordTimestamp = GraphQL::InterfaceType.define do
  name 'ActiveRecordTimestamp'

  field :createdAt, types.String, property: :created_at
  field :updatedAt, types.String, property: :updated_at
end
@jorroll
Copy link
Contributor

jorroll commented Dec 25, 2017

Sounds like a problem with the guide you're using. Check out graphql-ruby's website. Specifically, the Interface and Union types guide. It explains how to fix your problem.

@rmosolgo
Copy link
Owner

rmosolgo commented Jan 2, 2018

Yep, thanks for the link to those docs above!

@rmosolgo rmosolgo closed this as completed Jan 2, 2018
@ghost
Copy link
Author

ghost commented Jan 18, 2018

It doesn't work. I don't undestand how to do. I receive all the time this error:

NotImplementedError (schema contains Interfaces or Unions, so you must define a resolve_type -> (obj, ctx) { ... } function):

@ghost
Copy link
Author

ghost commented Jan 18, 2018

To fix this problem I used this in my project_schema.rb file:

ProjectSchema = GraphQL::Schema.define do
  query(Types::QueryType)
  mutation(Types::MutationType)

  resolve_type lambda { |_obj, _ctx|
  }
end

But I TOTALLY don't understand why!

Why?

@ghost
Copy link
Author

ghost commented Jan 18, 2018

Now I can use interfaces in my types files.

@ghost
Copy link
Author

ghost commented Jan 18, 2018

I ended up using this in project_schema.rb:

...
resolve_type ->(type, obj, ctx) {}

But please explain it to me what it does. And why it doesn't work here:

Interfaces::ActiveRecordTimestamp = GraphQL::InterfaceType.define do
  name 'ActiveRecordTimestamp'

  field :createdAt, types.String, property: :created_at
  field :updatedAt, types.String, property: :updated_at

  resolve_type ->(type, obj, ctx) {}
end

like it says here (http://graphql-ruby.org/types/abstract_types.html):

Type-Level Resolution Hooks
Instead of a single, top-level resolve_type function, you can provide type-level functions:

 MyUnion = GraphQL::UnionType.define do
   resolve_type ->(obj, ctx) {
     # resolve `obj` as a member of `MyUnion`
   }
 end
 
 MyInterface = GraphQL::InterfaceType.define do
   resolve_type ->(obj, ctx) {
     # resolve `obj` as a member of `MyInterface`
   }
 end

These functions take priority over the schema-level function.

@thefliik @rmosolgo please.

@wmaciejak
Copy link

I have exactly the same question. Why resolve_type need to be implemented or what kind of use cases it covers?
@rmosolgo

@NachoJusticia
Copy link

Any update on that? I have the same problem.. I have to add the resolve_type in the schema

@rmosolgo
Copy link
Owner

The schema requires a resolve_type implementation in order to determine the concrete type of objects that are returned as members of interface or union types. Here's an up-to-date doc link:

https://graphql-ruby.org/schema/definition.html#object-identification-hooks

Please let me know if it doesn't work for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants