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

Using argument :after in a field causes DuplicateNamesError when fetching the whole schema in Graphiql #5162

Open
tomwang57 opened this issue Nov 14, 2024 · 1 comment

Comments

@tomwang57
Copy link

tomwang57 commented Nov 14, 2024

Describe the bug

Using argument :after in a field causes DuplicateNamesError when fetching the whole schema in Graphiql:

GraphQL::Schema::DuplicateNamesError - Found two visible definitions for `Me.events_connection.after`:
 #<GraphQL::Schema::Argument Me.events_connection.after: String @description="Returns the elements in the list that come after the specified cursor.">, 
#<GraphQL::Schema::Argument Me.events_connection.after: String! @description="Fetch items after this cursor">

Versions

graphql: 1.13.23
rails: 6.1
graphql-batch: 0.6.0

GraphQL schema

Include relevant types and fields (in Ruby is best, in GraphQL IDL is ok). Any custom extensions, etc?

field :events_connection, Types::EventsConnectionType do
    argument :after, String, description: "Fetch items after this cursor"
    # other arguments
 end

GraphQL query

Fetch the whole shema in Graphiql

@rmosolgo
Copy link
Owner

Hi! Thanks for the bug report. This could probably be fixed by adding a check here to see if a field with the given name was already defined:

def apply
field.argument :after, "String", "Returns the elements in the list that come after the specified cursor.", required: false
field.argument :before, "String", "Returns the elements in the list that come before the specified cursor.", required: false
field.argument :first, "Int", "Returns the first _n_ elements from the list.", required: false
field.argument :last, "Int", "Returns the last _n_ elements from the list.", required: false
end

Another approach would be to delete the one you don't want:

# Define the field 
field :events_connection ... do 
end 

# Remove the argument that GraphQL-Ruby added automatically:
events_conn_field = get_field("eventsConnection")
duplicate_defn = events_conn_field.all_argument_definitions.find { |arg| arg.keyword == :after && arg.desciption != "Fetch items after this cursor" } 
events_conn_field.remove_argument(duplicate_defn) 

Another approach would be to pass field ... connection: false to opt out of GraphQL-Ruby's automatic connection handling. But if you did that, you'd have to also define :before, :first, and :last, and manually wrap the list in a connection wrapper object. So it's probably a bit heavy-handed.

Does one of those approaches 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

2 participants