-
Notifications
You must be signed in to change notification settings - Fork 242
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
Explicitly mention in the docs that @requires fields cant depend on fields with arguments #2276
Comments
Hi @rickbijkerk, thanks for the report. This behaviour was actually changed "recently" with #2120 and from federation 2.1.2 onward, you should be able to In any case, I agree that we should better document all of this. And in fact, we should document this for And just to summarise the status quo, if you have field with argument, say
|
Hi @pcmanus Thanks for the elaborate response this really helps a lot! 👍 |
@pcmanus We should also document what would happen if the user explicitly requests for the field inside the @requires directive with a different argument value than what's defined inside @requires directive selection set. // selection set when client requests for both fieldA & fieldB with different arguments Here, behaviour is ambiguous and it's not defined if sub-graph which provides fieldA will receive request for first 100 or first 50 . |
@agarwal-b-ankit That's a very good callout, and the honest answer is that I failed to properly think about this case, and I'll have to double check this more closely. But the intent in my mind is that the subgraph with the I'm just not sure it quite works at the moment and need to do some more testing. |
Yes, I agree that the arguments defined inside the Also, I tried to test this out by setting up federation on my local and found that only one call is made and it seemed that the explicitly provided arguments are favored. However, when I try to generate query plan in apollo-workbench, I see the opposite behavior. |
Hi @pcmanus on a similar note, have we thought about supporting dynamic variables in the @requires fields? For example, this is the query: query ProductWithAvailability($postalCode: String!) {
getProduct {
productId
availability(postalCode: $postalCode)
}
} On the schema side, type Query {
getProduct: Product
}
type ProductInventory {
quantitiesAvailable: Int!
}
type ProductAvailabilityMessage {
message: String!
}
type Product @key(fields: "productId") {
productId: Int!
inventory(postalCode: String!): ProductInventory
availabilityMessage: ProductAvailabilityMessage @requires(fields: "inventory($postalCode) { quantitiesAvailable }")
} It would be really nice here if we could use a dynamic variable when using @requires. Another thought is, what if postalCode was an actual field that we could pass into @requires? type Product {
productId: Int!
postalCode: String!
inventory: ProductInventory
availabilityMessage: ProductAvailabilityMessage @requires(fields: "inventory(postalCode) { quantitiesAvailable }")
} |
To clarify for those following along later. The requires directive does support arguments, but there is still an issue as documented in the above comment that if the client adds the field to the operation and overrides the arguments, the query planner will default to the client arguments, not the ones specified in the schema Schema 1type Query {
locations: [Location!]!
}
type Location @key(fields: "id"){
id: ID!
name: String!
photo(smallVersion: Boolean): String!
} Schema 2type Location @key(fields: "id") {
id: ID!
photo(smallVersion: Boolean): String! @external
overallRating: Float @requires(fields: "photo(smallVersion: true)") # Force the smallVersion here
} This does compose but at runtime if the operation is so query Locations {
locations {
id
photo
overallRating
}
} Then the query plan looks like this
VS not having the client select the field query Locations {
locations {
id
overallRating
}
} and the query plan looks like this QueryPlan {
Sequence {
Fetch(service: "locations") {
{
locations {
__typename
id
photo(smallVersion: true)
}
}
},
Flatten(path: "locations.@") {
Fetch(service: "reviews") {
{
... on Location {
__typename
id
photo
}
} =>
{
... on Location {
overallRating
}
}
},
},
},
} |
We should update the title here @apollographql/engineering |
I've run into this same issue. Whatever arguments the client specifies squashes whatever is expressed in @ requires, resulting in the incorrect value being passed to the subgraph that has the @ requires statement. I think that in the case of mismatching arguments, the router should probably use aliases or a similar strategy to request both. This may have some caching implications for some use cases, but I think that's preferable to providing incorrect inputs to subgraphs. |
While it makes sense the way it works, I had to find out myself that you can only depend on fields which dont have arguments.
A good place to add would be here: https://www.apollographql.com/docs/federation/federated-types/federated-directives/#requires
The text was updated successfully, but these errors were encountered: