You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
These tools change the GraphQL default from "handle every field error" to "opt-in the errors you want to handle".
100
98
101
-
## Import the nullability directives
99
+
## Enabling error aware parsing
102
100
103
-
Nullability directives are experimental. You need to import them using the [`@link` directive](https://specs.apollo.dev/link/v1.0/):
101
+
To make the Apollo generated parsers aware of errors, import the [nullability directives](https://specs.apollo.dev/nullability/v0.4/) using the [`@link` directive](https://specs.apollo.dev/link/v1.0/):
104
102
105
103
```graphql
106
104
extendschema@link(
107
105
url: "https://specs.apollo.dev/nullability/v0.4",
108
-
# Note: other directives are needed later on and added here for convenience
And define the default behavior when an error happens.
114
111
115
-
You will also need to opt in a default catch but more on that [later](#catchbydefault).
116
-
</Note>
112
+
You may catch the error and expose it as a `FieldResult`:
113
+
114
+
```graphql
115
+
# Catch the error and expose it as a FieldResult<T> in the generated models.
116
+
extend schema @catchByDefault(to: RESULT)
117
+
```
118
+
119
+
or re-throw the error:
120
+
121
+
```graphql
122
+
# Re-throw the error. If no parent field catches it, `response.exception` contains an instance of `ApolloGraphQLException`.
123
+
extend schema @catchByDefault(to: THROW)
124
+
```
125
+
126
+
or coerce the error to `null`, like the current GraphQL default:
127
+
128
+
```graphql
129
+
# Coerce the error to null. The caller must read `response.errors` to disambiguate a null vs error field.
130
+
extend schema @catchByDefault(to: NULL)
131
+
```
132
+
133
+
Adding `@catchByDefault(to: NULL)` is a no-op for codegen that unlocks using `@catch` in your operations.
134
+
135
+
Because errors can never happen on non-null fields (`String!` and others), `@catchByDefault` only influences the nullable fields in your schema.
136
+
137
+
Some of those fields are only nullable for error reasons. For those cases, Apollo Kotlin supports `@semanticNonNull`.
117
138
118
139
## `@semanticNonNull`
119
140
@@ -149,7 +170,7 @@ type User {
149
170
}
150
171
```
151
172
152
-
With `@semanticNonNull`, afrontenddeveloperknowsthatagivenfieldwillneverbenullinregularoperationandcanthereforeactaccordingly. Noneedtoguessanymore!
173
+
With `@semanticNonNull`, afrontenddeveloperknowsthatagivenfieldwillneverbenullinregularoperationandcanthereforeactaccordingly. TheApolloKotlincodegengenerates `@semanticNonNull` fieldsasnon-nullKotlinproperties. Noneedtoguessanymore!
153
174
154
175
Ideally, yourbackendteamannotatestheirschema with `@semanticNonNull` directives so that different frontend teams can benefit from the new type information.
155
176
@@ -281,34 +302,7 @@ class User(
281
302
282
303
The error is thrown during parsing but still caught before it reaches your UI code. If no parent field catches it, the Apollo Kotlin runtime does and exposes the exception in `ApolloResponse.exception`.
283
304
284
-
</Note>
285
-
286
-
## `@catchByDefault`
287
-
288
-
In order to use the nullability directives, you need to opt in a default catch behaviour for nullable GraphQL fields using `@catchByDefault`.
289
-
290
-
You can choose to map nullable fields to `FieldResult`:
291
-
292
-
```graphql
293
-
# Errors stop the parsing.
294
-
extendschema@catchByDefault(to: RESULT)
295
-
```
296
-
297
-
Or throw errors:
298
-
299
-
```graphql
300
-
# Errors stop the parsing.
301
-
extend schema @catchByDefault(to: THROW)
302
-
```
303
-
304
-
Or coerce errors to `null`, like the current GraphQL default:
305
-
306
-
```graphql
307
-
# Coerce errors to null by default.
308
-
extend schema @catchByDefault(to: NULL)
309
-
```
310
-
311
-
(Adding `@catchByDefault(to: NULL)` is a no-op for codegen that unlocks using `@catch` in your operations.)
305
+
</Note>
312
306
313
307
## Migrate to semantic nullability
314
308
@@ -329,20 +323,19 @@ If you were using `@nonnull` before, you can now use `@semanticNonNull`.
329
323
330
324
`@semanticNonNull`, coupled with `@catch` is more flexible and also more in line with other frameworks.
Copy file name to clipboardExpand all lines: libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ir/OperationBasedModelGroupBuilder.kt
Copy file name to clipboardExpand all lines: libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ir/OperationBasedWithInterfacesModelGroupBuilder.kt
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -59,7 +59,7 @@ internal class OperationBasedWithInterfacesModelGroupBuilder(
59
59
return field.toProperty() to field.toModelGroup()!!
0 commit comments