GraphQL Cookbook
Creating an object
8 min
creating an object through the parse graphql api problem you want to create a new object in your database through the parse graphql api solution using the parse graphql, there are two different ways to create a new object in your database using generic mutation https //www back4app com/docs/parse graphql/graphql mutation create object#mutation generic this is the mutation that you must use if you do not have already created your object’s class using class mutation https //www back4app com/docs/parse graphql/graphql mutation create object#mutation class this is the recommended mutation if you have already created your object’s class version information depending on the version of parse you choose to run, the graphql queries, mutations and results will be slightly different please choose the correct example along with the parse version you are running using generic mutation when you use the create create generic mutation, parse server behaves like a schemaless database it means that you do not need to define your object’s class in your application’s schema beforehand you just need to send your object’s data, and parse server will not only store it, but also learn from it, and automatically create a new class in your application’s schema therefore, the objects’ create create generic mutation is the method that you must use for creating a new object if you do not have already created your object’s class you can also use this mutation for creating an object of pre existing classes, but, for these cases, we recommend using the class mutation https //www back4app com/docs/parse graphql/graphql mutation create object#mutation class class request #in parse 3 10 0 and later you must first create the class itself 1 mutation createclass { 2 createclass(input { 3 name "hero" 4 schemafields { 5 addstrings \[{name "name"}] 6 addnumbers \[{name "height"}] 7 } 8 }){ 9 class{ 10 schemafields{ 11 name 12 typename 13 } 14 } 15 } 16 } response 1 { 2 "data" { 3 "createclass" { 4 "class" { 5 "schemafields" \[ 6 { 7 "name" "objectid", 8 " typename" "schemastringfield" 9 }, 10 { 11 "name" "updatedat", 12 " typename" "schemadatefield" 13 }, 14 { 15 "name" "createdat", 16 " typename" "schemadatefield" 17 }, 18 { 19 "name" "name", 20 " typename" "schemastringfield" 21 }, 22 { 23 "name" "height", 24 " typename" "schemanumberfield" 25 }, 26 { 27 "name" "acl", 28 " typename" "schemaaclfield" 29 } 30 ] 31 } 32 } 33 } 34 } object request 1 mutation createobject{ 2 createhero(input {fields {name "luke skywalker"}}){ 3 hero{ 4 id 5 name 6 } 7 } 8 } response 1 { 2 "data" { 3 "createhero" { 4 "hero" { 5 "id" "sgvybzo5qjfpmufxcxn1", 6 "name" "luke skywalker" 7 } 8 } 9 } 10 } using class mutation once you have already created your object’s class in your application’s schema (for instance, using the generic mutation https //www back4app com/docs/parse graphql/graphql mutation create object#mutation generic ), parse server instantly adds to your graphql api a new create\<classname> create\<classname> mutation to create a new object of this class therefore, the object’s class mutation is the recommended method for creating a new object if you have already created your object’s class since this mutation knows your class’ data, it will automatically make available for you additional features like code auto complete and validation you also don’t need to specify the data types when sending dates, pointers, relations, files, geo points, polygons, or bytes through the class create mutation this example will only work if you have already created your object’s class you can create a class using the generic mutation request 1 mutation createobject{ 2 createhero(input {fields {name "r2 d2"}}){ 3 hero{ 4 id 5 createdat 6 } 7 } 8 } response #notice the id property refers to the global id in the relay specification, not to confuse with the objectid from parse 1 { 2 "data" { 3 "createhero" { 4 "hero" { 5 "id" "sgvybzpvrm5tvdm1ynbp", 6 "createdat" "2020 02 06t13 13 26 678z" 7 } 8 } 9 } 10 } older parse server versions parse server 3 9 0 generic mutation class request #in parse 3 9 0 you also must first create the class itself 1 mutation createclass { 2 createclass( 3 name "hero" 4 schemafields { 5 addstrings \[{name "name"}] 6 addnumbers \[{name "height"}] 7 }){ 8 schemafields{ 9 name 10 typename 11 } 12 } 13 } response 1 { 2 "data" { 3 "createclass" { 4 "schemafields" \[ 5 { 6 "name" "objectid", 7 " typename" "schemastringfield" 8 }, 9 { 10 "name" "updatedat", 11 " typename" "schemadatefield" 12 }, 13 { 14 "name" "createdat", 15 " typename" "schemadatefield" 16 }, 17 { 18 "name" "name", 19 " typename" "schemastringfield" 20 }, 21 { 22 "name" "height", 23 " typename" "schemanumberfield" 24 }, 25 { 26 "name" "acl", 27 " typename" "schemaaclfield" 28 } 29 ] 30 } 31 } 32 } object request #and then create the object \#in parse 3 9 0 you must first create the class itself 1 mutation createobject{ 2 createhero(fields { 3 name "luke skywalker" 4 }){ 5 id 6 createdat 7 } 8 } response 1 { 2 "data" { 3 "createhero" { 4 "id" "ckhurmmjzw", 5 "createdat" "2019 11 04t12 37 22 462z" 6 } 7 } 8 } class mutation parse server 3 9 0 1 mutation createobject{ 2 createhero(fields { 3 name "r2 d2" 4 }){ 5 id 6 createdat 7 } 8 } result parse 3 9 0 1 { 2 "data" { 3 "createhero" { 4 "id" "n5grpei0il", 5 "createdat" "2019 11 04t12 45 00 882z" 6 } 7 } 8 } parse server 3 8 0 generic mutation parse server 3 8 0 1 mutation createobject { 2 create(classname "hero" fields { 3 name "luke skywalker" 4 }){ 5 objectid 6 createdat 7 } 8 } result parse 3 8 0 1 { 2 "data" { 3 "objects" { 4 "create" { 5 "objectid" "ffyobotk85", 6 "createdat" "2019 07 15t01 25 20 875z" 7 } 8 } 9 } 10 } class mutation parse server 3 8 0 1 mutation createhero { 2 createhero(fields { name "r2 d2" }) { 3 objectid, 4 createdat 5 } 6 } result parse 3 8 0 1 { 2 "data" { 3 "createhero" { 4 "objectid" "tuecddcgno", 5 "createdat" "2019 11 04t12 44 10 951z" 6 } 7 } 8 } parse server 3 7 2 generic mutation parse server 3 7 2 1 mutation createobject { 2 objects { 3 create(classname "hero", fields { name "luke skywalker" }) { 4 objectid, 5 createdat 6 } 7 } 8 } result parse 3 7 2 1 { 2 "data" { 3 "objects" { 4 "create" { 5 "objectid" "kr9aqnzfui", 6 "createdat" "2019 07 15t01 25 20 875z" 7 } 8 } 9 } 10 } class mutation parse server 3 7 2 1 mutation createhero { 2 objects { 3 createhero(fields { name "r2 d2" }) { 4 objectid, 5 createdat 6 } 7 } 8 } result parse 3 7 2 1 { 2 "data" { 3 "objects" { 4 "createhero" { 5 "objectid" "jjh0aqqjfs", 6 "createdat" "2019 07 15t02 22 04 982z" 7 } 8 } 9 } 10 }