graphql-ruby icon indicating copy to clipboard operation
graphql-ruby copied to clipboard

DuplicateTypeNamesError when using Relay's scaffold code Types::NodeType

Open vmsp opened this issue 4 years ago • 3 comments

Describe the bug

The code generated by rails generate graphql:install doesn't seem to work.

# /app/graphql/types/node_type.rb
module Types
  module NodeType
    include Types::BaseInterface
    # Add the `id` field
    include GraphQL::Types::Relay::NodeBehaviors
  end
end

# /app/graphql/types/user_type.rb
class Types::UserType < Types::BaseObject
  implements Types::NodeType
  ...
end

Results in

GraphQL::Schema::DuplicateTypeNamesError: Multiple definitions for `Node`. Previously found GraphQL::Types::Relay::Node (Module), then found Types::NodeType (Module) at Query.user.implements

Versions

graphql version: 1.12.13 rails (or other framework): 6.1.4

vmsp avatar Jul 05 '21 12:07 vmsp

Ahh -- thanks for opening this. I see that the Query.id field uses GraphQL:Types::Relay::Node. It needs some way to accept a return type in the configuration. I'll see what I can find!

rmosolgo avatar Jul 09 '21 11:07 rmosolgo

Hey, any idea what the workaround for this could be? Just using GraphQL::Types::Relay::Node directly?

jesster2k10 avatar Aug 01 '21 13:08 jesster2k10

This seemed to do the trick for me.

module HasNodesFieldDecorator
  def field_options
    puts 'I was called'
    super.merge(type: [Types::NodeType, null: true])
  end
end

module HasNodeFieldDecorator
  def field_options
    puts 'I was called too'
    super.merge(type: Types::NodeType)
  end
end

GraphQL::Types::Relay::HasNodeField.singleton_class.prepend(HasNodeFieldDecorator)
GraphQL::Types::Relay::HasNodesField.singleton_class.prepend(HasNodesFieldDecorator)

skukx avatar Dec 17 '21 21:12 skukx