GraphQL Cookbook
更新对象
9 分
通过 parse graphql api 更新对象 问题 您想通过 parse graphql api 更新数据库中的现有对象。 解决方案 使用 parse graphql,有两种不同的方法可以更新数据库中的现有对象: https //www back4app com/docs/parse graphql/graphql mutation update object#mutation generic 如果您想设置尚未属于对象类的字段,则必须使用此变更。 https //www back4app com/docs/parse graphql/graphql mutation update object#mutation class 如果对象的类已经具有您想要更新的所有字段,则推荐使用此变更。 版本信息 根据您选择运行的 parse 版本,graphql 查询、变更和结果会略有不同。 请根据您正在运行的 parse 版本选择正确的示例。 使用通用变更 当您使用 更新 更新 通用变更时,parse 服务器表现得像一个无模式数据库。这意味着您不需要事先定义对象的所有字段。您只需发送要更新的字段,parse 服务器不仅会存储它,还会从中学习,并自动在该对象的类中创建任何新字段。 因此,对象的 更新 更新 通用变更是您必须使用的方法,用于更新现有对象,如果您想设置尚未属于对象类的字段。实际上,您可以使用此变更来更新任何现有对象,但我们建议在对象的类已经具有您想要更新的所有字段时使用 https //www back4app com/docs/parse graphql/graphql mutation update object#mutation class 。 此示例仅在您使用 类名 类名 和一个 对象 id 对象 id 的现有对象时才有效。您可以使用 https //www back4app com/docs/parse graphql/graphql mutation create object 的配方来创建对象。 parse 3 8 0 request 1 mutation updateobject { 2 update(classname "hero", objectid "rr8jmfrnks", fields { height 5 6 }) { 3 updatedat 4 } 5 } response 1 { 2 "data" { 3 "updatehero" { 4 "updatedat" "2019 11 04t13 28 44 150z" 5 } 6 } 7 } 示例 parse 3 9 0 及更高版本: parse 3 9 0 及更高版本没有通用方法 update。您必须使用下面的特定方法来更新对象。 使用类变更 一旦您在应用程序的架构中创建了对象的类(例如,使用 https //www back4app com/docs/parse graphql/graphql mutation create object#mutation generic 配方),parse 服务器会立即在您的 graphql api 中添加一个新的 update\<classname> update\<classname> 变更,以更新该类的现有对象。 因此,如果您的对象类已经具有您想要更新的所有字段,则对象的类变更是更新现有对象的推荐方法。由于此变更知道您的类数据,它将自动为您提供代码自动完成和验证等附加功能。您在通过类更新变更发送日期、指针、关系、文件、地理点、多边形或字节时,也不需要指定数据类型。 此示例仅在您使用类的变更和 objectid objectid 或 id id 的现有对象时有效。您可以使用 https //www back4app com/docs/parse graphql/graphql mutation create object 配方创建对象。对象的类必须具有您尝试更新的所有字段。您可以使用 https //www back4app com/docs/parse graphql/graphql mutation update object#mutation generic 创建新字段。 parse 3 10 0 及更高版本 request 1 mutation updateobject { 2 updatehero(input { 3 id "sgvybzpvrm5tvdm1ynbp" 4 fields { 5 height 5 6 6 } 7 }){ 8 hero{ 9 updatedat 10 } 11 } 12 } response 1 { 2 "data" { 3 "updatehero" { 4 "hero" { 5 "updatedat" "2020 02 06t13 31 49 866z" 6 } 7 } 8 } 9 } 旧版 parse 服务器 parse 3 9 0 类变更: parse 服务器 3 9 0 1 mutation updateobject { 2 updatehero(id "ckhurmmjzw" fields { 3 height 5 6 4 }){ 5 updatedat 6 } 7 } 结果 parse 3 9 0 1 { 2 "data" { 3 "updatehero" { 4 "updatedat" "2019 11 04t13 30 20 457z" 5 } 6 } 7 } 解析 3 8 0 类变更: 请求 1 mutation updateobject { 2 updatehero(objectid "rr8jmfrnks" fields { 3 height 5 6 4 }){ 5 updatedat 6 } 7 } 响应 1 { 2 "data" { 3 "updatehero" { 4 "updatedat" "2019 11 04t13 38 46 343z" 5 } 6 } 7 } 解析 3 7 2 通用变更: 解析服务器 3 7 2 1 mutation updateobject { 2 objects { 3 update(classname "hero", objectid "ffyobotk85", fields { height 5 6 }) { 4 updatedat 5 } 6 } 7 } 结果 解析 3 7 2 1 { 2 "data" { 3 "objects" { 4 "update" { 5 "updatedat" "2019 07 15t05 57 14 416z" 6 } 7 } 8 } 9 } 类变更: 解析服务器 3 7 2 1 mutation updatehero { 2 objects { 3 updatehero(objectid "jjh0aqqjfs", fields { height 3 6 }) { 4 updatedat 5 } 6 } 7 } 结果 解析 3 7 2 1 { 2 "data" { 3 "objects" { 4 "updatehero" { 5 "updatedat" "2019 07 15t05 51 25 572z" 6 } 7 } 8 } 9 }