question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GraphQLFieldDefinition should implement equals/hashcode

See original GitHub issue

When defining class object types the GraphQLObjectType checks for duplicate field definitions by field name and throws an exception if multiple fields are defined with the same name.

However, the GraphQLFieldDefinition object doesn’t implement the equals/hashcode method such that when programmatically constructing schema definitions we can build a Set of field definitions for a class and detect duplicates.

Simple example is:

public abstract class BaseA {
  protected String field1;

  public abstract String getField1();
}

public class A extends BaseA {

  public String getField1() {  return field1; }
}

A programmatic approach of building the schema definition would be to introspect the class A to determine it’s exposed fields which would generate a GraphQLFieldDefinition for Field1. Then you would need to introspect the class(s) it inherits from to determine the fields it inherits. The parent class introspection would detect the same Field1 definition. The fix to the programatic approach would be to use an ImmutableSet/Set to build the GraphQLFieldDefinitions to prevent this.

However, if you build a Set<GraphQLFieldDefinition> and append to it the definitions from class A. Then traverse it’s parent class and add the definitions it’s discovers, which in this case would be the same exact definition, when you added the new definitions to the set it would appear distinct. You wouldn’t detect the error until you called add the fieldsto the GraphQLObjectType.Builder and called build on it. At which time it would enforce field name uniqueness constraint.

I suggest that we make the following changes:

  1. Implement .equals/hashcode for GraphQLFieldDefinition to enforce field name uniqueness constraint.
  2. (Breaking change) separate PR/release since it’d require major revision change we change the GraphQLObject.Builder class method fields to accept a Set<GraphQLFieldDefinition> instead of a List<GraphQLFieldDefinition>

The only other solution to this is for callers to keep a separate name->GraphQLFieldDefinition map to detect duplicates.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dminkovskycommented, Sep 14, 2016

Thank you for opening this issue. Yes, implementing #equals() would be very nice for several reasons, including the one you mention.

We are exploring replacing the schema definition classes and their builders with code generated by immutables. The generated code would include #equals() and #hashCode() etc.

0reactions
andimarekcommented, May 13, 2017

closed: see #28

Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQLFieldDefinition (graphql-java 18.0 API) - javadoc.io
No GraphQLSchemaElement implements `equals/hashCode` because we need object identity to treat a GraphQLSchema as an abstract graph. boolean, isDeprecated().
Read more >
Generating hashCode() and equals() when creating Java ...
The code I'm working on is using jaxb2-maven-plugin from org.codehaus.mojo to generate Java classes from XSD schema. I'm looking for a way to...
Read more >
Schema | GraphQL Java
Your GraphQL API has a schema which defines each field that can be queried or mutated and what types those fields are.
Read more >
GraphQLFieldDefinition.newFieldDefinition - Java - Tabnine
@param builderConsumer the consumer code that will be given a builder to transform * * @return a new field based on calling build...
Read more >
Directives - GraphQL Java Kickstart
And the actual implementation is the following: public class UppercaseDirective implements SchemaDirectiveWiring { @Override public GraphQLFieldDefinition ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found