Prisma API提供:
- 簡單突變:創(chuàng)建,更新,插入和刪除特定對象類型的單個節(jié)點
- 批量突變:更新和刪除某個模型的許多節(jié)點
- 關(guān)系突變:連接,斷開,創(chuàng)建,更新和上傳跨關(guān)系的節(jié)點
通常,服務(wù)的Prisma API是基于其數(shù)據(jù)模型生成的。要探索Prisma API中的操作,可以使用GraphQL Playground。
在下文中,我們將探索基于此數(shù)據(jù)模型的基于Prisma服務(wù)的示例查詢:
type Post {
id: ID! @unique
title: String!
published: Boolean!
author: User!
}
type User {
id: ID! @unique
age: Int
email: String! @unique
name: String!
posts: [Post!]!
}
對象突變(Object mutations)
我們可以使用模型突變來修改某個模型的單個節(jié)點。
創(chuàng)建節(jié)點(Creating nodes)
在這里,我們使用createUser突變來創(chuàng)建一個新用戶:
# Create a new user
mutation {
createUser(
data: {
age: 42
email: "zeus@example.com"
name: "Zeus"
}
) {
id
name
}
}
注意:無默認(rèn)值的必填字段,需要在數(shù)據(jù)輸入對象中指定。
修改節(jié)點(Updating nodes)
我們可以使用updateUser更改電子郵件和名稱。請注意,我們正在使用where參數(shù)選擇要更新的節(jié)點:
mutation {
updateUser(
data: {
email: "zeus2@example.com"
name: "Zeus2"
}
where: {
email: "zeus@example.com"
}
) {
id
name
}
}
修改插入(Upserting nodes)
當(dāng)我們想要更新現(xiàn)有的節(jié)點,或者在單個突變中創(chuàng)建一個新節(jié)點時,我們可以使用upsert突變。
在這里,我們使用upsertUser通過某個email更新User,或者如果具有該email的User尚不存在,就創(chuàng)建一個新User:
# Upsert a user
mutation {
upsertUser(
where: {
email: "zeus@example.com"
}
create: {
email: "zeus@example.com"
age: 42
name: "Zeus"
}
update: {
name: "Another Zeus"
}
) {
name
}
}
注:
create和update與createUser和updateUser突變中的數(shù)據(jù)對象具有相同的類型。
刪除節(jié)點(Deleting nodes)
要刪除節(jié)點,我們所要做的就是使用delete突變刪除選擇的節(jié)點。
在這里,我們使用deleteUser突變,通過它的id刪除一個User:
mutation {
deleteUser(where: {
id: "cjcdi63l20adx0146vg20j1ck"
}) {
id
name
email
}
}
由于email用@unique指令標(biāo)記,我們也可以通過他們的email選擇(并因此刪除)User節(jié)點:
mutation {
deleteUser(where: {
email: "cjcdi63l20adx0146vg20j1ck"
}) {
id
name
email
}
}
嵌套突變(Nested mutations)
我們可以使用create和update模型突變來同時修改關(guān)系中的節(jié)點。這被稱為嵌套的突變,并被事務(wù)執(zhí)行。
概述(Overview)
存在幾個嵌套突變參數(shù):
createupdateupsertdeleteconnectdisconnect
它們的可用性和確切的行為取決于以下兩個參數(shù):
- 父突變類型
-
create突變 -
update突變 -
upsert突變
-
- 關(guān)系的類型
- 可選的一對一關(guān)系
- 必須的一對一關(guān)系
- 一對多關(guān)系
例如:
- 創(chuàng)建突變只會暴露嵌套的
create和connect突變 - 更新突變暴露
update,upsert突變?yōu)楸匦璧囊粚σ魂P(guān)系
栗子(Examples)
在這一點上,我們不提供所有可能的場景,而是提供一個例子列表。
建議使用GraphQL Playground查看不同嵌套突變的行為。
創(chuàng)建并連接相關(guān)節(jié)點
我們可以在嵌套的輸入對象字段中使用連接操作來連接到一個或多個相關(guān)節(jié)點。
在這里,我們正在創(chuàng)建一個新的Post并通過唯一的email字段connect到現(xiàn)有的author。在這種情況下,connect為節(jié)點選擇提供了一種方法:
# Create a post and connect it to an author
mutation {
createPost(data: {
title: "This is a draft"
published: false
author: {
connect: {
email: "zeus@example.com"
}
}
}) {
id
author {
name
}
}
}
如果我們在author中提供create參數(shù)而不是connect,我們將創(chuàng)建一個相關(guān)author并同時connect到它,而不是connect到現(xiàn)有的author。
在創(chuàng)建User而不是Post時,我們實際上可以同時create并connect到多個Post節(jié)點,因為User具有多對多的Post。
在這里,我們正在create一個新User并直接將其連接到幾個新的和現(xiàn)有的Post節(jié)點:
# Create a user, create and connect new posts, and connect to existing posts
mutation {
createUser(
data: {
email: "zeus@example.com"
name: "Zeus"
age: 42
posts: {
create: [{
published: true
title: "First blog post"
}, {
published: true
title: "Second blog post"
}]
connect: [{
id: "cjcdi63j80adw0146z7r59bn5"
}, {
id: "cjcdi63l80ady014658ud1u02"
}]
}
}
) {
id
posts {
id
}
}
}
更新(updating)和更新插入(upserting)相關(guān)節(jié)點
更新節(jié)點時,可以同時更新一個或多個相關(guān)節(jié)點。
mutation {
updateUser(
data: {
posts: {
update: [{
where: {
id: "cjcf1cj0r017z014605713ym0"
}
data: {
title: "Hello World"
}
}]
}
}
where: {
id: "cjcf1cj0c017y01461c6enbfe"
}
) {
id
}
}
請注意,
update接受包含適合updatePost突變的where和data字段的對象列表。
嵌套更新插入(upserting)類似:
mutation {
updatePost(
where: {
id: "cjcf1cj0r017z014605713ym0"
}
data: {
author: {
upsert: {
where: {
id: "cjcf1cj0c017y01461c6enbfe"
}
update: {
email: "zeus2@example.com"
name: "Zeus2"
}
create: {
email: "zeus@example.com"
name: "Zeus"
}
}
}
}
) {
id
}
}
刪除相關(guān)節(jié)點
更新節(jié)點時,可以同時刪除一個或多個相關(guān)節(jié)點。在這種情況下,delete提供了一種節(jié)點選擇方式:
mutation {
updateUser(
data: {
posts: {
delete: [{
id: "cjcf1cj0u01800146jii8h8ch"
}, {
id: "cjcf1cj0u01810146m84cnt34"
}]
}
}
where: {
id: "cjcf1cj0c017y01461c6enbfe"
}
) {
id
}
}
標(biāo)量列表突變(Scalar list mutations)
當(dāng)一個對象類型有一個標(biāo)量列表作為其類型的字段時,有許多特殊的突變可用。
在以下數(shù)據(jù)模型中,User類型有三個這樣的字段:
type User {
id: ID! @unique
scores: [Int!]! # scalar list for integers
friends: [String!]! # scalar list for strings
coinFlips: [Boolean!]! # scalar list for booleans
}
創(chuàng)建節(jié)點(Creating nodes)
創(chuàng)建類型為User的新節(jié)點時,可以使用set為每個標(biāo)量列表字段提供一個值列表。
例如:
mutation {
createUser(data: {
scores: { set: [1, 2, 3] }
friends: { set: ["Sarah", "Jane"] }
throws: { set: [false, false] }
}) {
id
}
}
更新節(jié)點(Updating nodes)
更新User類型的現(xiàn)有節(jié)點時,可以在標(biāo)量列表字段上執(zhí)行一些附加操作:
-
set:用一個全新的列表覆蓋現(xiàn)有列表。 -
push(coming soon):在列表中的任何位置添加一個或多個元素。 -
pop(coming soon):從列表的開頭或結(jié)尾刪除一個或多個元素。 -
remove(coming soon):刪除列表中與給定過濾器匹配的所有元素。
注意:
push,pop和remove尚未實現(xiàn)。如果你真的好奇這些將會是什么樣子,你可以在相應(yīng)的規(guī)范(specification)中看到預(yù)覽。
set
每個標(biāo)量列表字段都會在update突變中使用具有set字段的對象。該字段的值是單個值或相應(yīng)標(biāo)量類型的列表。
栗子:將現(xiàn)有User節(jié)點的scores設(shè)置為[1]:
mutation {
updateUser(
where: {
id: "cjd4lfdyww0h00144zst9alur"
}
data: {
scores: {
set: 1
}
}
) {
id
}
}
將現(xiàn)有User節(jié)點的scores設(shè)置為[10,20,30]:
mutation {
updateUser(
where: {
id: "cjd4lfdyww0h00144zst9alur"
}
data: {
scores: {
set: [10,20,30]
}
}
) {
id
}
}
批量突變(Batch Mutations)
批處理突變可用于一次更新或刪除多個節(jié)點。返回的數(shù)據(jù)只包含受影響節(jié)點的數(shù)量。
為了更新許多節(jié)點,可以使用where參數(shù)選擇受影響的節(jié)點,同時用data指定新值。所有節(jié)點將更新為相同的值。
請注意,批量突變不會觸發(fā)訂閱事件!
在這里,我們正在發(fā)布在2017年創(chuàng)建的所有未發(fā)布的Post節(jié)點:
mutation {
updateManyPosts(
where: {
createdAt_gte: "2017"
createdAt_lt: "2018"
published: false
}
data: {
published: true
}
) {
count
}
}
在這里,我們刪除某個作者的所有未發(fā)布的Post節(jié)點:
mutation {
deleteManyPosts(
where: {
published: false
author: {
name: "Zeus"
}
}
) {
count
}
}