Skip to content

Schema build fails when declaring same inputType in 2 different queries #90

@danielocampo2

Description

@danielocampo2

Context: it has been added in graphql-java 3.0.0 a method that checks that InputTypes have to be unique and will throw an exception if this requirement isn't met.
The exception thrown is the following:
Caused by: graphql.AssertException: All types within a GraphQL schema must have unique names. No two provided types may have the same name. No provided type may have a name which conflicts with any built in types (including Scalar and Introspection types). You have redefined the type 'inputCode' from being a 'GraphQLInputObjectType' to a 'GraphQLInputObjectType'

Given how graphql-java-annotations works, this will prevent developers from using the same InputType as a parameter for 2 different queries, when this usecase might be totally valid.
I believe this happens because when building the schema, annotations library iterates over the parameters defined for a GraphQLField. If it is an object type (not primitive), it will always attempt to create a new InputType, even if this already was created previously, thus throwin the aforementioned exception.

Example: A simple example to reproduce this is to create a new class that will be used by the queries, say Code, and then declare this as parameters for 2 different queries.

public class Code {
  @GraphQLField
  public String firstField;
  @GraphQLField
  public String secondField;
}

public class YourQueryClass {
  @GraphQLField
  public List<Something> something(Code code) {  }
  @GraphQLField
  public List<SomethingElse> somethingElse(Code code) { }
}

Possible solution: when building the schema in the annotations library, evaluate if a class is already defined as InputType. If it is, use that, otherwise create a new one.

Momentary Workaround: If the object that you defined (Code) it is not too complex, just flatten your query parameters to use primitive types (in this case, replace Code in your queries with two String parameters for firstField and secondField)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions