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

Created on 25 Dec 2017  路  9Comments  路  Source: rmosolgo/graphql-ruby

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

Most helpful comment

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

All 9 comments

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.

Yep, thanks for the link to those docs above!

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 aresolve_type -> (obj, ctx) { ... }function):

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?

Now I can use interfaces in my types files.

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.

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

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

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!

Was this page helpful?
0 / 5 - 0 ratings