Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 50d4e9b

Browse files
authored
Merge pull request #768 from apollographql/feature/namespacingcaveats
Added caveats section to TN0012
2 parents 8d12101 + e1424f6 commit 50d4e9b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/content/technotes/TN0012-namespacing-by-separation-of-concern.mdx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ tags: [schema-design]
99

1010
Most GraphQL APIs provide their capabilities as root-level fields of the `Query` and `Mutation` types, resulting in a flat structure. For example, the GitHub GraphQL API has approximately 200 of these root-level fields! Even with tools like the Apollo Explorer, navigating and understanding larger "flat" graphs can be difficult.
1111

12+
<Note>
13+
14+
Make sure to read the [Caveats](#caveats) section below. While the name spacing pattern works well for queries, mutations can have side-effects that may not be inline with the GraphQL spec.
15+
16+
</Note>
17+
1218
To improve the logical organization of our graph's capabilities, we can define _namespaces_ for our root-level operation fields. These are object types that in turn define query and mutation fields that are all related to a particular concern.
1319

1420
For example, we can define all the mutation fields related to `User` objects in a `UsersMutations` namespace object:
@@ -108,10 +114,7 @@ mutation DoTwoThings {
108114
With namespaces, your mutation fields that actually modify data are no longer root-level fields (instead, your namespace objects are). Because of this, the mutation fields are resolved in parallel. In many systems, this doesn't present an issue (for example, you probably want to use another mechanism in your mutation resolvers to ensure transactional consistency, such as a saga orchestrator).
109115

110116
```graphql
111-
mutation DoTwoNestedThings(
112-
$createInput: CreateReviewInput!
113-
$deleteInput: DeleteReviewInput!
114-
) {
117+
mutation DoTwoNestedThings($createInput: CreateReviewInput!, $deleteInput: DeleteReviewInput!) {
115118
reviews {
116119
create(input: $createInput) {
117120
success
@@ -127,10 +130,7 @@ mutation DoTwoNestedThings(
127130
If you want to guarantee serial execution in a particular operation, you can use client-side aliases to create two root fields that are resolved serially:
128131

129132
```graphql
130-
mutation DoTwoNestedThingsInSerial(
131-
$createInput: CreateReviewInput!
132-
$deleteInput: DeleteReviewInput!
133-
) {
133+
mutation DoTwoNestedThingsInSerial($createInput: CreateReviewInput!, $deleteInput: DeleteReviewInput!) {
134134
a: reviews {
135135
create(input: $createInput) {
136136
success
@@ -144,3 +144,13 @@ mutation DoTwoNestedThingsInSerial(
144144
}
145145
}
146146
```
147+
148+
## Caveats
149+
150+
As pointed out by [members from the GraphQL Technical Steering Committee](https://benjie.dev/graphql/nested-mutations), while the above approach does execute in a GraphQL server, it does not satisfy the GraphQL spec requirement that:
151+
152+
> resolution of fields other than top-level mutation fields must always be side effect-free and idempotent.
153+
154+
Instead it is recommended that any GraphQL mutations by defined at the root level so they are executed serially and in accordance with the expectations of the specification.
155+
156+
At this time the only solution the GraphQL specification provides for grouping related mutations is field naming conventions and ordering these fields carefully - there's currently no spec-compliant solution to having an overwhelming number of fields on the root mutation type. However, there are some interesting proposals to address this issue that we encourage community members to review and provide feedback on, in particular the [proposal for GraphQL Namespaces](https://github.com/graphql/graphql-spec/issues/163) and the [proposal for `serial fields`](https://github.com/graphql/graphql-spec/issues/252).

0 commit comments

Comments
 (0)