Skip to content

Commit bd5dddf

Browse files
committed
docs: begin new documentation
1 parent ebdb3f5 commit bd5dddf

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

DOCUMENTATION_NEXT.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# GraphQL Request Documentation
2+
3+
# Return Mode
4+
5+
GraphQL execution has this general pattern:
6+
7+
```ts
8+
interface GraphQLExecutionResult {
9+
data?: object
10+
errors?: GraphQLError[]
11+
extensions?: []
12+
}
13+
```
14+
15+
You can change the output of client methods by configuring its return mode. This allows you to tailor the client better to your specific use-case.
16+
17+
The only client method that is not affected by return mode is `raw` which will _always_ return a standard GraphQL result type.
18+
19+
## `graphql`
20+
21+
Return the standard graphql execution output.
22+
23+
## `data`
24+
25+
Return just the data including [schema errors](#schema-errors) (if using). Other errors are thrown as an `AggregateError`.
26+
27+
**This mode is the default.**
28+
29+
## `successData`
30+
31+
Return just the data excluding [schema errors](#schema-errors). Errors are thrown as an `AggregateError`. This mode acts like you were using [`OrThrow`](#orthrow) method variants all the time.
32+
33+
This mode is only available when using [schema errors](#schema-errors).
34+
35+
## `dataAndErrors`
36+
37+
Return data and errors. This is the most type-safe mode. It never throws.
38+
39+
# Schema Errors
40+
41+
There is a GraphQL schema design pattern that advocates for encoding errors into your schema. It generally has two parts: One, objects that represent errors; Two, root fields that return unions of one success object and multiple error objects. The benefit of this approach is letting users know about error states and enabling clients to receive them in a type safe way. The general net positive is higher quality and easier to develop software.
42+
43+
Tools that support this pattern:
44+
45+
- https://pothos-graphql.dev/docs/plugins/errors
46+
47+
Articles about this pattern:
48+
49+
- ...
50+
51+
## Generation
52+
53+
This pattern has first class support. By default all objects whose name begin with `Error` will be considered to be "error objects". You can customize this at generate time with your own regular expression.
54+
55+
## Toggle
56+
57+
You can disable schema errors by disabling the generation in the first place or adjusting your constructor.
58+
59+
```ts
60+
const client = Client.create({ schemaErrors: false })
61+
```
62+
63+
## `isError`
64+
65+
You can use a helper function `isError` that will narrow objects to just error objects. For example:
66+
67+
```ts
68+
const result = await client.mutation.foo()
69+
if (isError(result)) {
70+
result // type is narrowed to just errors
71+
} else {
72+
result // type is narrowed to just success
73+
}
74+
```
75+
76+
## `orThrow`
77+
78+
You can use `orThrow` method variants to only return the success.
79+
80+
This is achieved by automatically adding `__typename` field to each object-like root field's selection set. Then the returned object name can be analyzed. Example:
81+
82+
```graphql
83+
mutation {
84+
foo {
85+
... on Bar {
86+
id
87+
}
88+
}
89+
}
90+
```
91+
92+
becomes:
93+
94+
```graphql
95+
mutation {
96+
foo {
97+
__typename
98+
... on Bar {
99+
id
100+
}
101+
}
102+
}
103+
```
104+
105+
The error that gets thrown is an instance of `Error` and its message is generic. However if all your error objects share an interface that contains a `message` field of `String` type then that message will be automatically be queried for used in the thrown Error. Example:
106+
107+
```graphql
108+
mutation {
109+
foo {
110+
... on Bar {
111+
id
112+
}
113+
}
114+
}
115+
```
116+
117+
becomes:
118+
119+
```graphql
120+
mutation {
121+
foo {
122+
__typename
123+
... on Error { # You could name your interface anything.
124+
message # Your interface must have a message field of String type.
125+
}
126+
... on Bar {
127+
id
128+
}
129+
}
130+
}
131+
```
132+
133+
Example:
134+
135+
```ts
136+
const result = await client.mutation.fooOrThrow({ onBar: { id } })
137+
result // type is narrowed to just Bar case.
138+
```

0 commit comments

Comments
 (0)