Skip to content

new page for upsert #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/ROOT/content-nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* xref:mutations/index.adoc[]
** xref:mutations/create.adoc[]
** xref:mutations/update.adoc[]
** xref:mutations/upsert.adoc[]
** xref:mutations/delete.adoc[]

* xref:subscriptions/index.adoc[]
Expand Down
64 changes: 64 additions & 0 deletions modules/ROOT/pages/mutations/upsert.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[[mutations-upsert]]
:description: This page describes how to upsert nodes through mutations.
= `upsert`

Upsert (update or insert) mutations update an existing object or create the object if it doesn't exist.
Upsert is GraphQL's version of a https://neo4j.com/docs/cypher-manual/current/clauses/merge/[`MERGE` clause] in Cypher.

Consider the following type definition:

[source, graphql, indent=0]
----
type Movie {
title: String
stars: Int
released: Int
id: ID! @id @unique
}
----

The `upsert` mutation and response are generated:

[source, graphql, indent=0]
----
type UpsertMoviesMutationResponse {
movies: [Movie!]!
}

type Mutation {
upsertMovies(input: [MoviesUpsertInput!]!): UpsertMoviesMutationResponse!
}
----

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":

[source, graphql, indent=0]
----
mutation {
upsertMovies(input: [{
node: {
title: "The Matrix"
},
onCreate: {
stars: 5
},
onUpdate: {
released: 1999
}
}]) { title }
}
----

The equivalent Cypher statement is:

.Query
[source, cypher]
----
MERGE (m:Movie {title: "The Matrix"})
ON CREATE SET
m.stars = 5
ON MATCH SET
m.released = 1999
RETURN m
----

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ From these type definitions, the library generates the following operation field
* `createMovies`
* `deleteMovies`
* `updateMovies`
* `upsertMovies`

**Subscription**:

Expand Down Expand Up @@ -86,15 +87,16 @@ This directive is used to limit the availability of mutation operations in the l
enum MutationFields {
CREATE
UPDATE
UPSERT
DELETE
}

directive @mutation(operations: [MutationFields!]! = [CREATE, UPDATE, DELETE]) on OBJECT | SCHEMA
directive @mutation(operations: [MutationFields!]! = [CREATE, UPDATE, UPSERT, DELETE]) on OBJECT | SCHEMA
----

=== Usage

.Disable Create, Delete, and Update operations for _Movie_
.Disable Create, Update, Upsert, and Delete operations for _Movie_
[source, graphql, indent=0]
----
type Movie @mutation(operations: []) {
Expand Down