Inconsistent type for IDs in GraphQL schema
Description
We're looking at adding middleware between the GraphQL API and our frontend. That resulted in an error due to a type mismatch.
The IDs for entries are returned as strings. However, when then using those IDs in a query, for example in relatedTo, an integer is expected. This requires a conversion from string to integer every time the results from a GraphQL query are used to make another one. Not a huge problem, but not super pretty either.
When directly connecting to the GraphQL endpoint, this actually works fine. However, once you start using something like Apollo Federation or even GraphiQL, the middleware does get strict about this type mismatch and throws an error.
This could be solved by returning the ID as an integer or tweaking the type expected for fields like relatedTo (maybe something like QueryArgument?).
Steps to reproduce
- Request
idfrom entry in GraphiQL:
query GetFirstEntry {
entries(limit: 1) {
id
}
}
- Use the exact response for
idto try and make a request in GraphiQL for related entries:
query GetRelatedEntry {
entries(relatedTo: "276") {
id
}
}
- GraphiQL will not let you make the request: it expects type
[int]forrelatedTo.
Additional info
- Craft version: 3.5.15.1
- PHP version: 7.4.8
- Database driver & version: MySQL 8.0.15
- Plugins & versions: lots, but none that are related to this
Thanks for reporting that! For the next release, I changed the type of relatedTo and relatedToAll to be [QueryArgument].
It's more consistent, as per the GraphQL Specification, the ID should serialize the same way a string does.
Craft 3.5.16 is out now with that fix.