Skip to content

Commit 6f1f9d3

Browse files
committed
new page for upsert
1 parent 627b429 commit 6f1f9d3

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

modules/ROOT/content-nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* xref:mutations/index.adoc[]
4949
** xref:mutations/create.adoc[]
5050
** xref:mutations/update.adoc[]
51+
** xref:mutations/upsert.adoc[]
5152
** xref:mutations/delete.adoc[]
5253
5354
* xref:subscriptions/index.adoc[]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[[mutations-upsert]]
2+
:description: This page describes how to upsert nodes through mutations.
3+
= `upsert`
4+
5+
Upsert (update or insert) mutations update an existing object or create the object if it doesn't exist.
6+
Upsert is GraphQL's version of a https://neo4j.com/docs/cypher-manual/current/clauses/merge/[`MERGE` clause] in Cypher.
7+
8+
Consider the following type definition:
9+
10+
[source, graphql, indent=0]
11+
----
12+
type Movie {
13+
title: String
14+
stars: Int
15+
released: Int
16+
id: ID! @id @unique
17+
}
18+
----
19+
20+
The `upsert` mutation and response are generated:
21+
22+
[source, graphql, indent=0]
23+
----
24+
type UpsertMoviesMutationResponse {
25+
movies: [Movie!]!
26+
}
27+
28+
type Mutation {
29+
upsertMovies(input: [MoviesUpsertInput!]!): UpsertMoviesMutationResponse!
30+
}
31+
----
32+
33+
The following mutation either matches an existing movie with title "The Matrix" and updates it with its release year of "1999" or, if there is no movie by that name, it creates a new movie node and gives it a star rating of "5":
34+
35+
[source, graphql, indent=0]
36+
----
37+
mutation {
38+
upsertMovies(input: [{
39+
node: {
40+
title: "The Matrix"
41+
},
42+
onCreate: {
43+
stars: 5
44+
},
45+
onUpdate: {
46+
released: 1999
47+
}
48+
}]) { title }
49+
}
50+
----
51+
52+
The equivalent Cypher statement is:
53+
54+
.Query
55+
[source, cypher]
56+
----
57+
MERGE (m:Movie {title: "The Matrix"})
58+
ON CREATE SET
59+
m.stars = 5
60+
ON MATCH SET
61+
m.released = 1999
62+
RETURN m
63+
----
64+

modules/ROOT/pages/schema-configuration/type-configuration.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ From these type definitions, the library generates the following operation field
2828
* `createMovies`
2929
* `deleteMovies`
3030
* `updateMovies`
31+
* `upsertMovies`
3132

3233
**Subscription**:
3334

@@ -86,15 +87,16 @@ This directive is used to limit the availability of mutation operations in the l
8687
enum MutationFields {
8788
CREATE
8889
UPDATE
90+
UPSERT
8991
DELETE
9092
}
9193
92-
directive @mutation(operations: [MutationFields!]! = [CREATE, UPDATE, DELETE]) on OBJECT | SCHEMA
94+
directive @mutation(operations: [MutationFields!]! = [CREATE, UPDATE, UPSERT, DELETE]) on OBJECT | SCHEMA
9395
----
9496

9597
=== Usage
9698

97-
.Disable Create, Delete, and Update operations for _Movie_
99+
.Disable Create, Update, Upsert and Delete operations for _Movie_
98100
[source, graphql, indent=0]
99101
----
100102
type Movie @mutation(operations: []) {

0 commit comments

Comments
 (0)