Skip to content

Commit 5762450

Browse files
authored
docs: Add GraphQL docs for unset field (#853)
1 parent 502f7e8 commit 5762450

File tree

2 files changed

+98
-29
lines changed

2 files changed

+98
-29
lines changed

_includes/graphql/files.md

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
The GraphQL API supports file upload via [GraphQL Upload](https://github.com/jaydenseric/graphql-upload), to send a `File` through `GraphQL` it's recommended to use the [Apollo Upload Client](https://github.com/jaydenseric/apollo-upload-client).
44

55
## Add a File field to a Class
6+
67
First of all we will update our `GameScore` class with a `screenshot` field of type `File`.
78

89
```jsonc
@@ -12,15 +13,14 @@ First of all we will update our `GameScore` class with a `screenshot` field of t
1213
"X-Parse-Master-Key": "MASTER_KEY"
1314
}
1415
```
16+
1517
```graphql
1618
# GraphQL
1719
mutation updateGameScoreClass {
1820
updateClass(
19-
input: {
20-
name: "GameScore",
21-
schemaFields: {
22-
addFiles: [{ name: "screenshot" }]
23-
}
21+
input: {
22+
name: "GameScore"
23+
schemaFields: { addFiles: [{ name: "screenshot" }] }
2424
}
2525
) {
2626
class {
@@ -53,6 +53,7 @@ The GraphQL API supports nested mutation for the `File` type, so you can send th
5353
"X-Parse-Master-Key": "MASTER_KEY" // (optional)
5454
}
5555
```
56+
5657
```graphql
5758
# GraphQL
5859
# $file is a GraphQL Variable, see https://github.com/jaydenseric/apollo-upload-client
@@ -90,16 +91,17 @@ You can add an existing file to an object.
9091
"X-Parse-Master-Key": "MASTER_KEY" // (optional)
9192
}
9293
```
94+
9395
```graphql
9496
# GraphQL
9597
mutation createGameScore {
9698
createGameScore(
9799
input: {
98100
fields: {
99101
playerName: "John"
100-
screenshot: {
101-
file :{
102-
__type: "File",
102+
screenshot: {
103+
file: {
104+
__type: "File"
103105
name: "6a4d43c3f0512bcb6bf05b6b0e7db47d_file.png"
104106
url: "http://localhost:1337/graphq/files/APPLICATION_ID/6a4d43c3f0512bcb6bf05b6b0e7db47d_file.png"
105107
}
@@ -133,6 +135,7 @@ mutation createGameScore {
133135
```
134136

135137
## Create and add a file
138+
136139
Lets create a new `GameScore` object and upload the file.
137140
```jsonc
138141
// Header
@@ -141,19 +144,13 @@ Lets create a new `GameScore` object and upload the file.
141144
"X-Parse-Master-Key": "MASTER_KEY" // (optional)
142145
}
143146
```
147+
144148
```graphql
145149
# GraphQL
146150
# $file is a GraphQL Variable, see https://github.com/jaydenseric/apollo-upload-client
147-
mutation createGameScore($file: Upload! ) {
151+
mutation createGameScore($file: Upload!) {
148152
createGameScore(
149-
input: {
150-
fields: {
151-
playerName: "John"
152-
screenshot: {
153-
upload : $file
154-
}
155-
}
156-
}
153+
input: { fields: { playerName: "John", screenshot: { upload: $file } } }
157154
) {
158155
gameScore {
159156
screenshot {
@@ -179,3 +176,36 @@ mutation createGameScore($file: Upload! ) {
179176
}
180177
}
181178
```
179+
180+
## Unlink a file
181+
182+
Let's update a `GameScore` object and unset the file linked in the `screenshot` field. By setting the `screenshot` field to `null`, the linked file will be removed from the `Gamescore` object.
183+
184+
**Note:** The file will be not deleted from your file storage.
185+
186+
```graphql
187+
# GraphQL
188+
mutation updateGameScore($id: ID!) {
189+
updateGameScore(input: { id: $id, fields: { screenshot: null } }) {
190+
gameScore {
191+
screenshot {
192+
name
193+
url
194+
}
195+
}
196+
}
197+
}
198+
```
199+
200+
```js
201+
// Response
202+
{
203+
"data": {
204+
"updateGameScore": {
205+
"gameScore": {
206+
"screenshot": null
207+
}
208+
}
209+
}
210+
}
211+
```

_includes/graphql/objects.md

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ mutation createAGameScore {
2020
createGameScore(
2121
input: {
2222
clientMutationId: "anUniqueId"
23-
fields: {
24-
playerName: "Sean Plott",
25-
score: 1337,
26-
cheatMode: false
27-
}
23+
fields: { playerName: "Sean Plott", score: 1337, cheatMode: false }
2824
}
2925
) {
3026
clientMutationId
@@ -85,6 +81,7 @@ For example, if you have a class named `GameScore` in the schema, Parse Server a
8581
"X-Parse-Master-Key": "MASTER_KEY" // (optional)
8682
}
8783
```
84+
8885
```graphql
8986
# GraphQL
9087
mutation updateAGameScore {
@@ -114,8 +111,53 @@ mutation updateAGameScore {
114111
}
115112
}
116113
```
114+
117115
**Note:** If you use [Apollo Client](https://www.apollographql.com/docs/react/) it's recommended to request the modified fields and `id` during the Mutation, then the [Apollo Client](https://www.apollographql.com/docs/react/) will automatically update its local store and push the new data across your app; i.e. If you update `playerName` you should request `playerName` and `id` like the code above.
118116

117+
### Unset a field
118+
119+
Across the whole GraphQL API you can simply unset a field by setting its value to `null`.
120+
121+
Following the official GraphQL API Specs, setting a field to `null` through the GraphQL API will completly unset the field in the database on the targeted Parse Object. GraphQL API will transform `null` on the server before saving the object to correctly unset the field into the database.
122+
123+
```js
124+
// Header
125+
{
126+
"X-Parse-Application-Id": "APPLICATION_ID",
127+
"X-Parse-Master-Key": "MASTER_KEY" // (optional)
128+
}
129+
```
130+
131+
```graphql
132+
# GraphQL
133+
mutation updateAGameScore {
134+
updateGameScore(
135+
input: { id: "R2FtZVNjb3JlOmM3TVpDZEhQY2w=", fields: { playerName: null } }
136+
) {
137+
gameScore {
138+
id
139+
playerName
140+
}
141+
}
142+
}
143+
```
144+
145+
```js
146+
// Response
147+
{
148+
"data": {
149+
"updateGameScore": {
150+
"gameScore": {
151+
"id": "R2FtZVNjb3JlOmM3TVpDZEhQY2w=",
152+
"playerName": null
153+
}
154+
}
155+
}
156+
}
157+
```
158+
159+
The GraphQL API will always return `null` if the field is `null` or `undefined` in the database. The GraphQL API does not differentiate between `null` and `undefined` in the data response.
160+
119161
## Delete
120162

121163
For each class in your application's schema, Parse Server automatically generates a custom mutation for deleting this class' objects through the GraphQL API.
@@ -170,20 +212,17 @@ The GraphQL API supports nested mutations, so you can create objects with comple
170212
"X-Parse-Master-Key": "MASTER_KEY" // (optional)
171213
}
172214
```
215+
173216
```graphql
174217
mutation aNestedMutation {
175218
createCountry(
176219
input: {
177220
fields: {
178221
name: "Mars"
179222
cities: {
180-
createAndAdd: [{ name: "Alpha",
181-
companies: {
182-
createAndAdd: [{
183-
name: "Motors"
184-
}]
185-
}
186-
}]
223+
createAndAdd: [
224+
{ name: "Alpha", companies: { createAndAdd: [{ name: "Motors" }] } }
225+
]
187226
}
188227
}
189228
}

0 commit comments

Comments
 (0)