|
1 | 1 | # GraphQL Request Documentation
|
2 | 2 |
|
3 |
| -# Return Mode |
| 3 | +# Output |
4 | 4 |
|
5 |
| -GraphQL execution has this general pattern: |
| 5 | +The standard GraphQL execution result type in the JavaScript ecosystem (from the `graphql` package) has roughly this type: |
6 | 6 |
|
7 | 7 | ```ts
|
8 | 8 | interface GraphQLExecutionResult {
|
9 | 9 | data?: object
|
10 | 10 | errors?: GraphQLError[]
|
11 |
| - extensions?: [] |
| 11 | + extensions?: unknown[] |
12 | 12 | }
|
13 | 13 | ```
|
14 | 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. |
| 15 | +Graffle can return this type but also many other types depending on your configuration. For example: |
16 | 16 |
|
17 |
| -The only client method that is not affected by return mode is `raw` which will _always_ return a standard GraphQL result type. |
| 17 | +1. Return the data directly without an envelope. |
| 18 | +1. Return all or some categories of errors (return type becomes a union). |
| 19 | +1. Return an envelope and place all or some categories of errors into the `errors` field. |
| 20 | +1. Throw all or some categories of errors. |
18 | 21 |
|
19 |
| -Here is a summary table of the modes: |
| 22 | +Configuration can be done at the constructor level. Method level will also be supported in the future. |
20 | 23 |
|
21 |
| -| Mode | Throw Sources (no type safety) | Returns (type safe) | |
22 |
| -| ---------------- | ------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | |
23 |
| -| `graphql` | Extensions, Fetch | `GraphQLExecutionResult` | |
24 |
| -| `graphqlSuccess` | Extensions, Fetch, GraphQLExecutionResult.errors | `GraphQLExecutionResult` with `.errors` always missing. | |
25 |
| -| `data` (default) | Extensions, Fetch, GraphQLExecutionResult.errors | `GraphQLExecutionResult.data` | |
26 |
| -| `dataSuccess` | Extensions, Fetch, GraphQLExecutionResult.errors, GraphQLExecutionResult.data Schema Errors | `GraphQLExecutionResult.data` without any schema errors | |
27 |
| -| `dataAndErrors` | | `GraphQLExecutionResult.data`, errors from: Extensions, Fetch, GraphQLExecutionResult.errors | |
| 24 | +```ts |
| 25 | +// Constructor Level |
| 26 | + |
| 27 | +const graffle = Graffle.create({ |
| 28 | + output: { |
| 29 | + errors: { |
| 30 | + execution: 'throw', |
| 31 | + other: 'return', |
| 32 | + }, |
| 33 | + }, |
| 34 | +}) |
| 35 | + |
| 36 | +// Method Level (planned, not implemented yet) |
| 37 | + |
| 38 | +await graffle.query.foo({}, { |
| 39 | + output: { |
| 40 | + envelope: true, |
| 41 | + }, |
| 42 | +}) |
| 43 | +``` |
28 | 44 |
|
29 |
| -## `graphql` |
| 45 | +## Errors |
30 | 46 |
|
31 |
| -Return the standard graphql execution output. |
| 47 | +There are three categories of errors: |
32 | 48 |
|
33 |
| -## `graphqlSuccess` |
| 49 | +1. `execution` – Anything that went wrong during execution. Examples: invalid input given, resolver threw an error. |
| 50 | +2. `schema` – Only present if the [schema errors](#schema-errors) are being used. Any time a result field returns an error type. |
| 51 | +3. `other` – Anything else. Examples: network error during request, extension threw error, your anyware threw an error. |
34 | 52 |
|
35 |
| -Return the standard graphql execution output. However, if there would be any errors then they're thrown as an `AggregateError`. |
36 |
| -This mode acts like you were using [`OrThrow`](#orthrow) method variants all the time. |
| 53 | +You can choose to output error categories in the following ways: |
37 | 54 |
|
38 |
| -## `dataSuccess` |
| 55 | +1. `throw` – Errors from category will be thrown. There is no type safety with this approach. |
| 56 | +2. `return` – Errors from category will be returned. The return type will thus become a union type. |
| 57 | +3. `default` – Use whatever the default is (you can change the default). |
39 | 58 |
|
40 |
| -Return just the data excluding [schema errors](#schema-errors). Errors are thrown as an `AggregateError`. |
41 |
| -This mode acts like you were using [`OrThrow`](#orthrow) method variants all the time. |
| 59 | +## Envelope |
42 | 60 |
|
43 |
| -This mode is only available when using [schema errors](#schema-errors). |
| 61 | +You can choose to use an envelope. When you use an envelope the data will be returned in a `data` property. Additional metadata properties will be exposed: |
44 | 62 |
|
45 |
| -## `data` |
| 63 | +1. `errors` – errors that you have chosen to include in the envelope. |
| 64 | +2. `extensions` – GraphQL execution result extensions. |
| 65 | +3. `response` – Only present if [transport](#link-todo) is `http`. The HTTP response to the request that was sent for the given GraphQL document. |
46 | 66 |
|
47 |
| -Return just the data including [schema errors](#schema-errors) (if using). Other errors are thrown as an `AggregateError`. |
| 67 | +## Examples |
48 | 68 |
|
49 |
| -**This mode is the default.** |
| 69 | +### Standard GraphQL |
50 | 70 |
|
51 |
| -## `dataAndErrors` |
| 71 | +```ts |
| 72 | +const graffle = Graffle.create({ |
| 73 | + output: { |
| 74 | + envelope: { |
| 75 | + errors: { |
| 76 | + execution: true, // Bring execution errors into envelope. |
| 77 | + }, |
| 78 | + }, |
| 79 | + errors: { |
| 80 | + other: 'throw', |
| 81 | + }, |
| 82 | + }, |
| 83 | +}) |
| 84 | + |
| 85 | +assertType<{ |
| 86 | + data: { |
| 87 | + foo: string /* or whatever */ |
| 88 | + } |
| 89 | + errors: GraphQLError[] |
| 90 | + extensions: unknown[] |
| 91 | + response: Response // Non-standard. Present when using HTTP transport. |
| 92 | +}>(await graffle.query.foo()) |
| 93 | +``` |
52 | 94 |
|
53 |
| -Return a union type of data and errors. This is the most type-safe mode. It never throws. |
| 95 | +### Full Type Safety |
| 96 | + |
| 97 | +```ts |
| 98 | +const graffle = Graffle.create({ |
| 99 | + output: { |
| 100 | + defaults: { |
| 101 | + errorChannel: 'return', |
| 102 | + }, |
| 103 | + envelope: false, |
| 104 | + }, |
| 105 | +}) |
| 106 | + |
| 107 | +assertType< |
| 108 | + | string /* or whatever */ |
| 109 | + | GraphQLError |
| 110 | + | Error |
| 111 | +>(await graffle.query.foo()) |
| 112 | +``` |
54 | 113 |
|
55 | 114 | # Schema Errors
|
56 | 115 |
|
|
0 commit comments