graphql-devise / graphql_devise

GraphQL interface on top devise_token_auth
MIT License
197 stars 36 forks source link

GraphQL::Schema::DuplicateNamesError #226

Closed snyt45 closed 6 months ago

snyt45 commented 2 years ago

Describe the bug

I have implemented Mutation to sign up users. Changing the content of the mutation argument raises a GraphQL::Schema::DuplicateNamesError.

If I restart rails, this problem goes away, but if I change the argument content of Mutation again, I get a GraphQL::Schema::DuplicateNamesError.

Perhaps it is caused by double registration in the following place (sorry if I am wrong). I am not familiar with graphql_devise and would like some help. https://github.com/graphql-devise/graphql_devise/blob/master/lib/graphql_devise/resource_loader.rb#L39

Environment

Steps to reproduce

I am implementing a Mutation that uses graphql_devise to sign up users. I am using GraphiQL to make sure GraphQL works.

module Mutations
  class Users::SignUp < GraphqlDevise::Mutations::SignUp
    argument :firstName, String, required: true
    argument :lastName, String, required: true

    def resolve(email:, **attrs)
      original_payload = super
      original_payload.merge(user: original_payload[:authenticatable])
    end
  end
end

The following mutations were performed in GraphiQL.

mutation {
  userRegister(
    email: "test@exaple.com",
    password: "pass1234",
    passwordConfirmation: "pass1234",
    confirmSuccessUrl: "test",
    firstName: "test",
    lastName: "test"
  ) {
    credentials {
      accessToken
      uid
      tokenType
    }
  }
}

The following error occurred

ActiveModel::UnknownAttributeError (unknown attribute 'firstName' for User.):

Because the argument was incorrect, the argument was corrected as follows.

module Mutations
  class Users::SignUp < GraphqlDevise::Mutations::SignUp
    argument :first_name, String, required: true # Corrections
    argument :last_name, String, required: true # Corrections

    def resolve(email:, **attrs)
      original_payload = super
      original_payload.merge(user: original_payload[:authenticatable])
    end
  end
end

If I run Mutation again, I get GraphQL::Schema::DuplicateNamesError.

GraphQL::Schema::DuplicateNamesError (Found two visible definitions for `Mutation.userRegister`: #<GraphQL::Schema::Field Mutation.userRegister(...): UserRegisterPayload>, #<GraphQL::Schema::Field Mutation.userRegister(...): UserRegisterPayload>):

Expected behavior

GraphQL::Schema::DuplicateNamesError should be able to register users without generating

Actual behavior

GraphQL::Schema::DuplicateNamesError (Found two visible definitions for `Mutation.userRegister`: #<GraphQL::Schema::Field Mutation.userRegister(...): UserRegisterPayload>, #<GraphQL::Schema::Field Mutation.userRegister(...): UserRegisterPayload>):

graphql (1.13.5) lib/graphql/schema/warden.rb:58:in `block in visible_entry?'
graphql (1.13.5) lib/graphql/schema/warden.rb:53:in `each'
graphql (1.13.5) lib/graphql/schema/warden.rb:53:in `visible_entry?'
graphql (1.13.5) lib/graphql/schema/member/has_fields.rb:46:in `block in get_field'
graphql (1.13.5) lib/graphql/schema/member/has_fields.rb:42:in `each'
graphql (1.13.5) lib/graphql/schema/member/has_fields.rb:42:in `get_field'
graphql (1.13.5) lib/graphql/schema.rb:1257:in `get_field'
graphql (1.13.5) lib/graphql/schema/warden.rb:139:in `block (2 levels) in get_field'
graphql (1.13.5) lib/graphql/schema/warden.rb:329:in `block in read_through'
graphql (1.13.5) lib/graphql/schema/warden.rb:148:in `get_field'
/usr/local/lib/ruby/3.0.0/forwardable.rb:238:in `get_field'
graphql (1.13.5) lib/graphql/static_validation/rules/fields_will_merge.rb:343:in `block in find_fields_and_fragments'
graphql (1.13.5) lib/graphql/static_validation/rules/fields_will_merge.rb:340:in `each'
graphql (1.13.5) lib/graphql/static_validation/rules/fields_will_merge.rb:340:in `find_fields_and_fragments'
graphql (1.13.5) lib/graphql/static_validation/rules/fields_will_merge.rb:333:in `fields_and_fragments_from_selection'
graphql (1.13.5) lib/graphql/static_validation/rules/fields_will_merge.rb:61:in `conflicts_within_selection_set'
graphql (1.13.5) lib/graphql/static_validation/rules/fields_will_merge.rb:25:in `block in on_operation_definition'
graphql (1.13.5) lib/graphql/static_validation/rules/fields_will_merge.rb:52:in `setting_errors'
graphql (1.13.5) lib/graphql/static_validation/rules/fields_will_merge.rb:25:in `on_operation_definition'
graphql (1.13.5) lib/graphql/static_validation/rules/operation_names_are_valid.rb:12:in `on_operation_definition'
graphql (1.13.5) lib/graphql/static_validation/rules/unique_directives_per_location.rb:27:in `block (2 levels) in <module:UniqueDirectivesPerLocation>'
graphql (1.13.5) lib/graphql/static_validation/base_visitor.rb:76:in `on_operation_definition'
graphql (1.13.5) lib/graphql/language/visitor.rb:79:in `public_send'
graphql (1.13.5) lib/graphql/language/visitor.rb:79:in `visit_node'
graphql (1.13.5) lib/graphql/language/visitor.rb:174:in `on_node_with_modifications'
graphql (1.13.5) lib/graphql/language/visitor.rb:102:in `block in on_abstract_node'
graphql (1.13.5) lib/graphql/language/visitor.rb:101:in `each'
graphql (1.13.5) lib/graphql/language/visitor.rb:101:in `on_abstract_node'
graphql (1.13.5) lib/graphql/language/visitor.rb:123:in `on_document'
graphql (1.13.5) lib/graphql/static_validation/definition_dependencies.rb:38:in `on_document'
graphql (1.13.5) lib/graphql/static_validation/rules/variables_are_used_and_defined.rb:79:in `on_document'
graphql (1.13.5) lib/graphql/static_validation/rules/fragment_spreads_are_possible.rb:26:in `on_document'
graphql (1.13.5) lib/graphql/static_validation/rules/fragments_are_used.rb:6:in `on_document'
graphql (1.13.5) lib/graphql/static_validation/rules/fragments_are_finite.rb:6:in `on_document'
graphql (1.13.5) lib/graphql/static_validation/rules/fragment_names_are_unique.rb:17:in `on_document'
graphql (1.13.5) lib/graphql/static_validation/rules/operation_names_are_valid.rb:16:in `on_document'
graphql (1.13.5) lib/graphql/static_validation/rules/no_definitions_are_present.rb:34:in `on_document'
graphql (1.13.5) lib/graphql/language/visitor.rb:79:in `public_send'
graphql (1.13.5) lib/graphql/language/visitor.rb:79:in `visit_node'
graphql (1.13.5) lib/graphql/language/visitor.rb:174:in `on_node_with_modifications'
graphql (1.13.5) lib/graphql/language/visitor.rb:68:in `visit'
graphql (1.13.5) lib/graphql/static_validation/validator.rb:57:in `block (3 levels) in validate'
graphql (1.13.5) lib/graphql/static_validation/validator.rb:44:in `catch'
graphql (1.13.5) lib/graphql/static_validation/validator.rb:44:in `block (2 levels) in validate'
/usr/local/lib/ruby/3.0.0/timeout.rb:80:in `timeout'
graphql (1.13.5) lib/graphql/static_validation/validator.rb:43:in `block in validate'
graphql (1.13.5) lib/graphql/tracing.rb:66:in `trace'
graphql (1.13.5) lib/graphql/static_validation/validator.rb:30:in `validate'
graphql (1.13.5) lib/graphql/query/validation_pipeline.rb:75:in `ensure_has_validated'
graphql (1.13.5) lib/graphql/query/validation_pipeline.rb:35:in `valid?'
graphql (1.13.5) lib/graphql/query.rb:316:in `valid?'
graphql (1.13.5) lib/graphql/analysis/ast.rb:36:in `block (2 levels) in analyze_multiplex'
graphql (1.13.5) lib/graphql/analysis/ast.rb:35:in `map'
graphql (1.13.5) lib/graphql/analysis/ast.rb:35:in `block in analyze_multiplex'
graphql (1.13.5) lib/graphql/tracing.rb:66:in `trace'
graphql (1.13.5) lib/graphql/analysis/ast.rb:34:in `analyze_multiplex'
graphql (1.13.5) lib/graphql/execution/multiplex.rb:210:in `block in instrument_and_analyze'
graphql (1.13.5) lib/graphql/execution/instrumentation.rb:29:in `block (2 levels) in apply_instrumenters'
graphql (1.13.5) lib/graphql/execution/instrumentation.rb:46:in `block (2 levels) in each_query_call_hooks'
graphql (1.13.5) lib/graphql/execution/instrumentation.rb:41:in `each_query_call_hooks'
graphql (1.13.5) lib/graphql/execution/instrumentation.rb:45:in `block in each_query_call_hooks'
graphql (1.13.5) lib/graphql/execution/instrumentation.rb:72:in `call_hooks'
graphql (1.13.5) lib/graphql/execution/instrumentation.rb:44:in `each_query_call_hooks'
graphql (1.13.5) lib/graphql/execution/instrumentation.rb:27:in `block in apply_instrumenters'
graphql (1.13.5) lib/graphql/execution/instrumentation.rb:72:in `call_hooks'
graphql (1.13.5) lib/graphql/execution/instrumentation.rb:26:in `apply_instrumenters'
graphql (1.13.5) lib/graphql/execution/multiplex.rb:190:in `instrument_and_analyze'
graphql (1.13.5) lib/graphql/execution/multiplex.rb:62:in `block in run_queries'
graphql (1.13.5) lib/graphql/tracing.rb:66:in `trace'
graphql (1.13.5) lib/graphql/execution/multiplex.rb:60:in `run_queries'
graphql (1.13.5) lib/graphql/execution/multiplex.rb:50:in `run_all'
graphql (1.13.5) lib/graphql/schema.rb:1760:in `multiplex'
graphql (1.13.5) lib/graphql/schema.rb:1731:in `execute'
graphql_devise (0.18.2) app/controllers/graphql_devise/graphql_controller.rb:17:in `auth'
actionpack (6.1.4.4) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.1.4.4) lib/abstract_controller/base.rb:228:in `process_action'
actionpack (6.1.4.4) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.1.4.4) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.1.4.4) lib/active_support/callbacks.rb:106:in `run_callbacks'
actionpack (6.1.4.4) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.1.4.4) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.1.4.4) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (6.1.4.4) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (6.1.4.4) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.1.4.4) lib/active_support/notifications.rb:203:in `instrument'
actionpack (6.1.4.4) lib/action_controller/metal/instrumentation.rb:33:in `process_action'
actionpack (6.1.4.4) lib/action_controller/metal/params_wrapper.rb:249:in `process_action'
activerecord (6.1.4.4) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.1.4.4) lib/abstract_controller/base.rb:165:in `process'
actionpack (6.1.4.4) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (6.1.4.4) lib/action_controller/metal.rb:254:in `dispatch'
actionpack (6.1.4.4) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (6.1.4.4) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.1.4.4) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (6.1.4.4) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.1.4.4) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.1.4.4) lib/action_dispatch/routing/route_set.rb:842:in `call'
warden (1.2.9) lib/warden/manager.rb:36:in `block in call'
warden (1.2.9) lib/warden/manager.rb:34:in `catch'
warden (1.2.9) lib/warden/manager.rb:34:in `call'
rack (2.2.3) lib/rack/etag.rb:27:in `call'
rack (2.2.3) lib/rack/conditional_get.rb:40:in `call'
rack (2.2.3) lib/rack/head.rb:12:in `call'
activerecord (6.1.4.4) lib/active_record/migration.rb:601:in `call'
actionpack (6.1.4.4) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.1.4.4) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (6.1.4.4) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.1.4.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.4.4) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
actionpack (6.1.4.4) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'
actionpack (6.1.4.4) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.1.4.4) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.1.4.4) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.1.4.4) lib/active_support/tagged_logging.rb:99:in `block in tagged'
activesupport (6.1.4.4) lib/active_support/tagged_logging.rb:37:in `tagged'
activesupport (6.1.4.4) lib/active_support/tagged_logging.rb:99:in `tagged'
railties (6.1.4.4) lib/rails/rack/logger.rb:26:in `call'
actionpack (6.1.4.4) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.1.4.4) lib/action_dispatch/middleware/request_id.rb:26:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (6.1.4.4) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.1.4.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.4.4) lib/action_dispatch/middleware/static.rb:24:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.1.4.4) lib/action_dispatch/middleware/host_authorization.rb:119:in `call'
rack-cors (1.1.1) lib/rack/cors.rb:100:in `call'
railties (6.1.4.4) lib/rails/engine.rb:539:in `call'
puma (5.5.2) lib/puma/configuration.rb:249:in `call'
puma (5.5.2) lib/puma/request.rb:77:in `block in handle_request'
puma (5.5.2) lib/puma/thread_pool.rb:340:in `with_force_shutdown'
puma (5.5.2) lib/puma/request.rb:76:in `handle_request'
puma (5.5.2) lib/puma/server.rb:447:in `process_client'
puma (5.5.2) lib/puma/thread_pool.rb:147:in `block in spawn_thread'

Reproducible demo

Rails.application.routes.draw do
  mount_graphql_devise_for User, at: 'graphql_auth', operations: {
    register: Mutations::Users::SignUp,
  }
  post '/graphql', to: 'graphql#execute'

  if Rails.env.development?
    mount GraphiQL::Rails::Engine, at: '/graphiql_auth', graphql_path: '/graphql_auth', as: :graphiql_auth
    mount GraphiQL::Rails::Engine, at: '/graphiql', graphql_path: '/graphql'
  end
end
# frozen_string_literal: true

module Types
  class MutationType < Types::BaseObject
    # TODO: remove me
    field :test_field, String, null: false,
                               description: 'An example field added by the generator'
    def test_field
      'Hello World'
    end
    field :sign_up, mutation: Mutations::Users::SignUp
  end
end
module Mutations
  class Users::SignUp < GraphqlDevise::Mutations::SignUp
    argument :first_name, String, required: true # Correction. 
    argument :last_name, String, required: true # Correction. 

    def resolve(email:, **attrs)
      original_payload = super
      original_payload.merge(user: original_payload[:authenticatable])
    end
  end
end

I also confirmed that the same GraphQL::Schema::DuplicateNamesError occurs when mutation_type.rb is set as follows, regardless of graphql_devise.

module Types
  class MutationType < Types::BaseObject
    # TODO: remove me
    field :test_field, String, null: false,
                               description: 'An example field added by the generator'
    def test_field
      'Hello World'
    end
    field :sign_up, mutation: Mutations::Users::SignUp
    field :sign_up, mutation: Mutations::Users::SignUp # Add
  end
end
Etx17 commented 11 months ago

Hi, did you resolve that ? I'm facing a same kind of issue, 'Found two visible definitions for UserRegistrationInput: Types::UserRegistrationInputType,'

dawidof commented 11 months ago

@Etx17 I’ve solved this by adding graphql_name in mutation

module Mutations
  class Users::SignUp < GraphqlDevise::Mutations::SignUp
    graphql_name ’SignUpUser’ # <- HERE

    argument :first_name, String, required: true
    argument :last_name, String, required: true

    def resolve(email:, **attrs)
      ...
    end
  end
end
mcelicalderon commented 7 months ago

@snyt45 could you please confirm if this is still a problem? I've seen this in the past, but it used to be related to the actual GraphQL gem and I think that was fixed a long time ago. Does it only happen when you change the code and Rails automatically reloads the changes in development?

snyt45 commented 6 months ago

@mcelicalderon I believe this problem occurred when I used GraphiQL to verify the operation. Now that I am using Altair GraphQL Client and not using GraphiQL, I have not encountered this problem.

mcelicalderon commented 6 months ago

Thank you, @snyt45. I'll close this one then. It can be reopened if someone runs into this again