Description
I am unsure whether to put this under bugs or feature requests, because I would consider this to be both.
The directives @include
and @skip
are awesome concepts, but they miss the mark when a certain field is not queryable. That is, if the boolean variable on which the @include
directive is conditioned evaluates to false, then it would be reasonable to say that it is not relevant whether the field is queryable or not, because the field will simply be omitted in the result anyway.
Why is this important? Well, consider we have two APIs that are very similar and share a big part of their graphql schema definition. I work with django-graphene combined with relay, so I'll present my example use case with that context.
Suppose both APIs contain design logic for a fruit store. Both stores have a model they call Banana
. One store also has a model called BananaBox
, because the store has some kind of shipping system, whereas the other store does not define BananaBox
because it does not ship bananas in boxes anywhere. The store that has BananaBox
simply added a ForeignKey from Banana
to BananaBox
in order to keep track of which box a banana is in.
Both APIs define appropriate django-graphene Nodes for each model, and both APIs define a query endpoint for bananas.
Now, if we want to perform a query somewhat like the following:
query bananas {
banana {
id
banana_box {
id
size
}
}
}
That query will fail when executed on the store without the BananaBox
model. However, one elegant way of solving this problem would be to use the following query:
query bananas($fetch_box: Boolean!) {
banana {
id
banana_box @include(if: $fetch_box) {
id
size
}
}
}
And then specifying fetch_box: true
or fetch_box: false
in the store with and without box respectively.
However, this will result in the following error for the store without the BananaBox: Cannot query field "banana_box" on type "BananaNode".
I believe this error is irrelevant to answering the query, because having fetch_box: false
clearly indicates to the server that we are not interested in the banana_box
field.
The graphql documentation about directives mentions that the @include
and @skip
directives determine what fields are included in the result, and I would argue that excluding a field means that composing the result can be completed without knowledge of whether or not the field is queryable at all.
If this BananaNode has a lot of queryable attributes, and we are interested in all of them, defining the bananas
query twice just because the two APIs differ by one field is quite silly and repetitive, especially when working with more than two similar APIs. If this use case seems far-fetched, please understand that it is really relevant to the application I am developing.