GraphQLFieldDefinition should implement equals/hashcode
See original GitHub issueWhen 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:
- Implement .equals/hashcode for GraphQLFieldDefinition to enforce field name uniqueness constraint.
- (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:
- Created 7 years ago
- Comments:5 (4 by maintainers)
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.
closed: see #28