graphql / graphql/graphql-spec

Execute fragments directly

Open
#866 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.6k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

It is not possible to execute a fragment directly against an executable schema. This can be useful for a variety of use cases while avoiding the need to generate/craft on-the-fly schemas that introduce a suitable top-level query. See https://github.com/graphql/graphql-js/issues/3060 for my initial description.

With the discussion around parameterized fragments it could be argued that every (root) operation can be interpreted as syntactic sugar for fragments:
```gql
query MyQuery($id = ID!) {
item(id: $id) { id }
}

fragment MyQuery($id = ID!) on Query {
item(id: $id) { id }
}
```
The only thing which may be less clear is the fact that just for mutations the top-level fields are resolved serially and not in parallel, but afaik the only requirement of the spec is that mutations are executed serial, so from an API user perspective this is a priori knowledge. To clarify: I do not want this as a replacement for query/mutation/subscription operation types, I just wanted to show the similarity between operation types and fragments.

### Use cases

Using graphql as a schema language for datastores:
The schema of (some of) our Kafka topics is expressed in terms of GQL fragments. Some mutations want to produce to these topics and may execute an "internal" (sub-)request instead of assembling the message values themselves. Most of the time the root value of these fragments is already available in the mutation and sometimes there isn't even a top-level query available to execute a "normal" operation against.

Distributed/federated query resolution:
Having had a quick look into how apollo federation forwards partial non-root-query subqueries (basically fragments), it seems to me that they basically expose a special top-level query `_entities` in federated services which gets special treatment to do a direct type-based resolution.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.