You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inline fragments don't work on fields declared with interface types (v16.2) #2336
Description
Specifying an inline fragment for a concrete (sub)type of a field declared with that same type seems to work. However, field fetchers aren't even called when the same is done for a field declared using an interface type, whether that declared interface type, concrete object type or any other type in between is used.
Set it up so that you can return/fetch some data. Perhaps return the exact same object/instance for both base and concrete fields of Test to make it more interesting (both need to be of type Concrete anyway). Issue the following query:
query {
test {
base {
__typename...onBase {
id
}
...onConcrete {
name
}
}
concrete {
__typename...onConcrete {
idname
}
}
}
}
Fields base and concrete should include exactly the same sub-fields: __typename, id and name. They do not.
... evaluates to true as I would have expected (inside skip is false and include is true so !skip && include is true). It is negated so that seems fine. The second condition:
... the type variable and the conditionType parameter are both GraphQLObjectTypes, both for Concrete but are different/separate instances. Not sure why this isn't normalized internally. Their toString() outputs are different. Equality check at the top of the method evaluates to false (not equal). It appears that equals method is not implemented on GraphQLObjectType so this falls back on the default implementation from Object that uses ==.
These are different internally. conditionType has replacedInterfaces non-null and type has it null.
Since conditionType isn't an interface or a union type in this case (but Concrete object type), this will fall through to that final return false;.
It appears that equals(Object that) method needs to be implemented on all types - object types to cover this case and interface types to cover exact matches too, which also fail. I am wondering if it can't be simply implemented to say that they are equal if they match the following conditions:
Are of the same Java class (this.class == that.class)
Belong to the same schema (although I don't see a readily available reference to it)
If wrapped, compare unwrapped (recursively), otherwise compare names.
I am willing to contribute this change if reasoning seems correct. Please respond.
This isn't going to be so easy. I cloned the repo and implemented the unit tests (commit 1) and then equals(Object) and hashCode() method (commit 2). The new unit tests are passing but 526 other fail with many occurrences of:
Internal error: should never happen: This schema is not forming an Directed Acyclic Graph : GraphQLObjectType{name='__Type', description='null', fieldDefinitionsByName=[kind, name, description, fields, interfaces, possibleTypes, enumValues, inputFields, ofType, specifiedByUrl], interfaces=[]} has already has a temporary mark
type and conditionType should really be == if they are for the same actual type - that should be fixed.
Not [1] but type.equals(another) must return true if the actual types are the same even when !=. This is my change. 526 unit test failures indicate incompatible expectations that would need to be fixed.
Neither [1] or [2]. FieldCollector.checkTypeCondition(FieldCollectorParameters, GraphQLType) must not rely (only) on equals(Object) but also account for this. My change could be modified to implement an alternate method instead of modifying equals(Object), then use that method instead.
Adds new isEquivalentTo(GraphQLType) to GraphQLType
Adds default implementation of isEquivalentTo(GraphQLType) for GraphQLNamedType
Adds default implementation of isEquivalentTo(GraphQLType) for GraphQLModifiedType
Adds the same for a nested class MyGraphQLObjectType inside SchemaPrinterTest
Modifies FieldCollector.checkTypeCondition(FieldCollectorParameters, GraphQLType) to use isEquivalentTo(GraphQLType) instead of equals(Object).
Wanted to test but didn't ... I need Long scalars and I made the changes to the newest code, not 16.2. They will be commonly needed in Java world. Note: GraphQL clients are neither solely JavaScript nor is JavaScript disadvantaged in this case any more - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt ... and custom scalars are supported.
GraphQLObjectTypes, both for Concrete but are different/separate instances
This sounds wrong: we are making sure that every Type is unique inside a Schema: you will never have two different types with the same name. Can you elaborate on that? Does that mean you have two GraphQLObjectType instances with the same name?
If so: how do you create you schema?
Programmatically .... using the schema builder. It is not reporting duplicate or redefined types.
I would recommend that you try to dig deeper into that: Have a look at GraphQLTypeCollectingVisitor.assertTypeUniqueness: this should normally throw an exception at schema build time if you have multiple types with the same name.
Put a breakpoint on the throw new ... (as shown below) ... and did NOT land on it.
privatevoidassertTypeUniqueness(GraphQLNamedTypetype, Map<String, GraphQLNamedType> result) {
GraphQLTypeexistingType = result.get(type.getName());
// do we have an existing definitionif (existingType != null) {
// type references are okif (!(existingTypeinstanceofGraphQLTypeReference || typeinstanceofGraphQLTypeReference))
// object comparison here is deliberateif (existingType != type) {
thrownewAssertException(format("All types within a GraphQL schema must have unique names. No two provided types may have the same name.\n" +
"No provided type may have a name which conflicts with any built in types (including Scalar and Introspection types).\n" +
"You have redefined the type '%s' from being a '%s' to a '%s'",
type.getName(), existingType.getClass().getSimpleName(), type.getClass().getSimpleName()));
}
}
}
I put a conditional breakpoint on it to trigger only when my type "in question" is being checked. Breakpoint is not triggered at all - like the method isn't called for it. I can see it being called for scalars (built-in and my custom ones) and enums (again, built-in and custom ones) but not once for either an object or interface type (didn't check others).
Same thing applies to visiting input and interface types. Union types are asserted always. You already have an extra comment there for that isTypeReference method:
// TODO: eh? Isn't it similar to assertTypeUniqueness?privatebooleanisTypeReference(Stringname) {
returnresult.containsKey(name) && !(result.get(name) instanceofGraphQLTypeReference);
}
However, this alone does not answer the main challenge. All calls to schemaBuilder.additionalType(type) in my code go through a single method of mine and I added double checking that names are distinct for all named output types. They are all distinct.
The second time it seems to be building another copy of it, as triggered by me setting up the GraphQL (kickstart servlet) configuration, from that line .with(getSchema()) in the code below:
I am strongly suspecting that something is messing with your Schema. Please remember that GraphQL Java is just the engine and we can't comment on libraries build on top of it (like the Kickstarter ones).
If this is really a GraphQL Java issue we need a standalone example in pure GraphQL Java code. Thanks
OK ... I found the issue... however, you should look at the calls to that assertion.
What was the issue? It was in my code and undetected by graphql-java. I invoked the build() method of the same GraphQLObjectType.Buildertwice accidentally. Only the result of the second one was passed on to the schema builder additionalType(). The result of the first invocation, however, may have found its way of being used directly / instead of a type reference from something else, such as the field type or as some kind of relation of another type. Schema (builder) accepted this just fine but got confused by it later...
After making sure that I build and reuse a single instance only things started working.
Activity
aleksandarsusnjar commentedon May 14, 2021
I've been tracing this... in
ExecutionStrategy, method:... line:
yields
subFieldswithout field from inline fragments.collectorParametershas:graphQLSchema: non-nullfragmentsByName: empty, size = 0variables: empty, size = 0objectType: correct object type and itsfieldDefinitionsByNameincludes all the fields.I can see that
fieldCollectorhas a non-nullconditionalNodesreference which has a non-nullvaluesResolverreference.aleksandarsusnjar commentedon May 14, 2021
SelectionSet for the field in question (for which subFields are being determined) prints as:
This causes execution to go to:
... specifically its case:
... that goes into:
Now, the first part of that condition, specifically:
... evaluates to
trueas I would have expected (insideskipisfalseandincludeistrueso!skip && includeistrue). It is negated so that seems fine. The second condition:Seems to figure out the correct
conditionType(Concrete) and goes into:This is where it gets interesting... I think. After the first line:
... the
typevariable and theconditionTypeparameter are bothGraphQLObjectTypes, both forConcretebut are different/separate instances. Not sure why this isn't normalized internally. Their toString() outputs are different. Equality check at the top of the method evaluates tofalse(not equal). It appears thatequalsmethod is not implemented onGraphQLObjectTypeso this falls back on the default implementation fromObjectthat uses==.These are different internally.
conditionTypehasreplacedInterfacesnon-null andtypehas itnull.In then comes to this code:
Since
conditionTypeisn't an interface or a union type in this case (butConcreteobject type), this will fall through to that finalreturn false;.It appears that
equals(Object that)method needs to be implemented on all types - object types to cover this case and interface types to cover exact matches too, which also fail. I am wondering if it can't be simply implemented to say that they are equal if they match the following conditions:this.class == that.class)I am willing to contribute this change if reasoning seems correct. Please respond.
aleksandarsusnjar commentedon May 14, 2021
This isn't going to be so easy. I cloned the repo and implemented the unit tests (commit 1) and then
equals(Object)andhashCode()method (commit 2). The new unit tests are passing but 526 other fail with many occurrences of:See: https://github.com/aleksandarsusnjar/graphql-java/pull/new/issue-2336-inline-fragments
aleksandarsusnjar commentedon May 14, 2021
Alternative is to make
doesFragmentConditionMatch()not useequals()(only).aleksandarsusnjar commentedon May 14, 2021
@andimarek:
What should be done here:
typeandconditionTypeshould really be==if they are for the same actual type - that should be fixed.type.equals(another)must returntrueif the actual types are the same even when!=. This is my change. 526 unit test failures indicate incompatible expectations that would need to be fixed.FieldCollector.checkTypeCondition(FieldCollectorParameters, GraphQLType)must not rely (only) onequals(Object)but also account for this. My change could be modified to implement an alternate method instead of modifyingequals(Object), then use that method instead.aleksandarsusnjar commentedon May 14, 2021
I have two sets of changes.
[2] https://github.com/aleksandarsusnjar/graphql-java/pull/new/issue-2336-inline-fragments
equals(Object)andhashCode()[3] https://github.com/aleksandarsusnjar/graphql-java/pull/new/issue-2336-alt-equality
isEquivalentTo(GraphQLType)toGraphQLTypedefaultimplementation ofisEquivalentTo(GraphQLType)forGraphQLNamedTypedefaultimplementation ofisEquivalentTo(GraphQLType)forGraphQLModifiedTypeMyGraphQLObjectTypeinsideSchemaPrinterTestFieldCollector.checkTypeCondition(FieldCollectorParameters, GraphQLType)to useisEquivalentTo(GraphQLType)instead ofequals(Object).Longscalars and I made the changes to the newest code, not 16.2. They will be commonly needed in Java world. Note: GraphQL clients are neither solely JavaScript nor is JavaScript disadvantaged in this case any more - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt ... and custom scalars are supported.aleksandarsusnjar commentedon May 14, 2021
I have tested a 16.2 variant of [3] inside what I am working on (see #2331) and can confirm that it works:
https://github.com/aleksandarsusnjar/graphql-java/pull/new/v16.2-issue-2336
I feel that [1] should be the right approach but I don't want to mess with that myself. @andimarek ?
andimarek commentedon May 14, 2021
This sounds wrong: we are making sure that every Type is unique inside a Schema: you will never have two different types with the same name. Can you elaborate on that? Does that mean you have two GraphQLObjectType instances with the same name?
If so: how do you create you schema?
aleksandarsusnjar commentedon May 14, 2021
Yes, exactly that:
Re:
Programmatically .... using the schema builder. It is not reporting duplicate or redefined types.
aleksandarsusnjar commentedon May 14, 2021
@andimarek: I cannot share that code but we can have a Zoom session if you're up to it... Would need to send you the details privately.
andimarek commentedon May 14, 2021
I would recommend that you try to dig deeper into that: Have a look at
GraphQLTypeCollectingVisitor.assertTypeUniqueness: this should normally throw an exception at schema build time if you have multiple types with the same name.aleksandarsusnjar commentedon May 14, 2021
OK ... will do now.
Breakpoint there.... restarting to regenerate schema.
aleksandarsusnjar commentedon May 14, 2021
Put a breakpoint on the
throw new ...(as shown below) ... and did NOT land on it.aleksandarsusnjar commentedon May 14, 2021
Not landing on:
either...
aleksandarsusnjar commentedon May 14, 2021
Method is getting called. It is landing on that first
if.aleksandarsusnjar commentedon May 14, 2021
I put a conditional breakpoint on it to trigger only when my type "in question" is being checked. Breakpoint is not triggered at all - like the method isn't called for it. I can see it being called for scalars (built-in and my custom ones) and enums (again, built-in and custom ones) but not once for either an object or interface type (didn't check others).
aleksandarsusnjar commentedon May 14, 2021
Notice this:
That will only call the assertion method for type references, which are ignored by the assertion. Is it meant to negate that condition:
aleksandarsusnjar commentedon May 14, 2021
Same thing applies to visiting input and interface types. Union types are asserted always. You already have an extra comment there for that
isTypeReferencemethod:aleksandarsusnjar commentedon May 14, 2021
However, this alone does not answer the main challenge. All calls to
schemaBuilder.additionalType(type)in my code go through a single method of mine and I added double checking that names are distinct for all named output types. They are all distinct.aleksandarsusnjar commentedon May 14, 2021
However, that
visitGraphQLObjectType()method IS called TWICE for my object type in question.The first time it is my code building it. Relevant part of the stack trace is:
The second time it seems to be building another copy of it, as triggered by me setting up the GraphQL (kickstart servlet) configuration, from that line
.with(getSchema())in the code below:Relevant part of the stack trace is:
andimarek commentedon May 14, 2021
I am strongly suspecting that something is messing with your Schema. Please remember that GraphQL Java is just the engine and we can't comment on libraries build on top of it (like the Kickstarter ones).
If this is really a GraphQL Java issue we need a standalone example in pure GraphQL Java code. Thanks
aleksandarsusnjar commentedon May 14, 2021
I get it. I am now puzzled as to why does kickstart configuration make a copy of the schema and also why would that be a problem either.
aleksandarsusnjar commentedon May 15, 2021
OK ... I found the issue... however, you should look at the calls to that assertion.
What was the issue? It was in my code and undetected by
graphql-java. I invoked thebuild()method of the sameGraphQLObjectType.Buildertwice accidentally. Only the result of the second one was passed on to the schema builderadditionalType(). The result of the first invocation, however, may have found its way of being used directly / instead of a type reference from something else, such as the field type or as some kind of relation of another type. Schema (builder) accepted this just fine but got confused by it later...After making sure that I build and reuse a single instance only things started working.