Skip to content

Commit d93c02e

Browse files
authored
Swapped to new syntax
1 parent 967b552 commit d93c02e

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/pages/blog/2024-08-14-semantic-nullability-for-application-developers.mdx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ byline: Alex Reilly
99

1010
> This blog post is directed at application developers using GraphQL. If you are a library author, you should read the more detailed feature spec instead.
1111
12+
Today we're providing a progress update from the Nullablility Working Group on Semantic Nullability which is our new approach to fixing GraphQL's nullability issues.
13+
1214
GraphQL has some fundamental problems, and to talk about them, we first have to talk about GraphQL's type system. GraphQL allows you to define a schema, and to do that many developers write a document in Schema Definition Language, or SDL. A SDL document may look like this
1315

1416
```graphql
@@ -42,34 +44,36 @@ The root of the problem is that developers want a way to express that all `User`
4244

4345
This is what we're calling "Semantic non-null". The syntax we've chosen is the following
4446

45-
| Syntax | Meaning |
46-
| -------- | ----------------- |
47-
| String | Nullable |
48-
| String! | Semantic non-null |
49-
| String!! | Strict non-null |
47+
| Syntax | Meaning |
48+
| ------- | ----------------- |
49+
| String? | Nullable |
50+
| String | Semantic non-null |
51+
| String! | Strict non-null |
52+
53+
Types are now semantic non-null by default. Question marks are used to indicate a nullable field similar to many other modern languages. `String!` retains its meaning. This is of course, a breaking change, and GraphQL prides itself in offering a path to non-breaking evolution for existing services. So alongside the new type, we're introducing some mechanics to assist developers in making incremental updates to their applications.
5054

51-
`String` remains exactly the same in meaning, `String!` becomes `String!!`, and `String!` is the new type meaning `String` or `(Error, null)`. This is of course, a breaking change, and GraphQL prides itself in offering a path to non-breaking evolution for existing services. So alongside the new type, we're introducing some mechanics to assist developers to make incremental updates to their applications.
5255
### Server migration
53-
In order to migrate, start by updating your service to use the most recent version of GraphQL.
56+
Once Semantic Nullability has been released, you will be able to start migrating by updating your service to use the most recent version of GraphQL.
5457

55-
This will open up the option to begin evolving your schema document by document. You can place the document directive `@extendedNullability` at the top of a file to begin using the new type of nullability in that file. The directive will not impact the interpretation of any other files in your schema.
58+
This will open up the option to begin evolving your schema document by document. You can place the document directive `@SemanticNullability` at the top of a file to begin using the new nullability features in that file. The directive will not impact the interpretation of any other files in your schema.
5659

5760
After migration, a `User` type would look like this.
5861

5962
```graphql
6063
@extendedNullability
6164

6265
type User {
63-
id: ID!!
64-
name: String!!
65-
age: Int!
66-
posts: [Post!]!
66+
id: ID!
67+
name: String!
68+
age: Int
69+
posts: [Post]
6770
}
6871
```
6972

7073
We can now trust that `age` and `posts` will never be null unless an error causes their values to fail to resolve.
74+
7175
### Frontend migration
72-
Client libraries and code generation tools that support Semantic Nullability may provide a flags to alter their error handling behavior. In this example, `ApolloClient` is providing a configuration option that causes the access of a field with an error to throw.
76+
Client libraries that take advantage of the new features in this release may provide a flags to alter their error handling behavior. In this example, `ApolloClient` is providing a configuration option that causes the access of a field with an error to throw.
7377

7478
```typescript
7579
const client = new ApolloClient({
@@ -82,7 +86,7 @@ Semantic Nullability gives clients and GraphQL tools more flexibility in how the
8286

8387
Because your client can decide how it handles errors, it will also be responsible for providing a modified version of the schema. For example, if the client raises an exception when an errored field is read, it can mark all "semantically non-null" fields as non-nullable in the schema provided to you as a frontend developer.
8488

85-
![Example code generation](https://i.imgur.com/VeblGEx.png)
89+
![Example code generation](https://i.imgur.com/i3hdCND.png)
8690

8791
You should use the modified version of the schema when doing code generation for your frontend application.
8892

0 commit comments

Comments
 (0)