[AIC] Implement explicit fragments as feature #6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Example usage:
output:
Implicit Inline Fragments
The exported "*Selection" functions can be used as implicit inline fragments from anywhere.
It can be used and constructed anywhere outside the operation and then object-spread into the selection.
However, there is not typesafety for this right now. As one can see, the
ArticlesSelection
fragment also works on the Books type, because it also defines atitle
field.Explicitly defined query fragments
When used extensively in the Query, it may be useful to use real GraphQL fragments.
This can be done using the
$fragment
helper selection function and a separately defined fragment function.function titleOnly
is such a function. Please note, that it is defined asfunction
and not anarrow function
, because it needs to have it's own scope withthis
. Also, you need to pass thethis
viabind
to the*Selection
function, so that it get's registered as named fragment later on.Possible Usages
Apart from these requirements, you can do whatever you want in your custom fragment functions, opening possibilities to conditional and parameterized fragments (similar to what is being discussed here: graphql/graphql-spec#204 )
In case of named parameterized fragments, I have not yet tested what the generated query will look like when there're arguments in the selection. It should collect and hoist them to variables in the query but still reference them in the fragment. This might not be valid gql, but you can still use Implicit Inline Fragments, so you have the convenience of fragments being defined once as code and have them generate the wanted gql code based on your input.