Creating and updating records in Brightspot Content Management API
This guide will explain how to create and update Brightspot records via CMA GraphQL mutation APIs.
Prerequisites for using the CMA
For endpoint configuration, see Custom Content Management API development and Hello Content Management API.
Introduction to the CMA
In the Content Management API, value differences for the proposed state are used to mutate an object. Passing in state differences, rather than an entire state object, accurately represents how users input data into Brightspot.
Background to the CMA
Consider the following record classes where setters and getters are omitted for brevity:
1public class Foo extends Record {23private String title;45private Integer number;67private List<Integer> numbers;89private boolean flag;1011@Recordable.Embedded12private Bar bar;1314private Baz baz;1516}
1@Recordable.Embedded2public class Bar extends Record {34private String barTitle;5}
1public class Baz extends Record {23private String bazTitle;4}
Following is a portion of the schema, concerned with mutation types and based on the aforementioned record classes, with Foo configured as a mutable entry field:
1type Mutation {2com_company_FooSave(diffs: [com_company_FooDiffInput], id: DiffId, toolUser: RefId): com_company_Foo3}45input com_company_FooDiffInput {6com_company_BarDiff: com_company_BarInput7com_company_FooDiff: com_company_FooInput8id: DiffId9}1011input com_company_FooInput {12bar: DiffId13baz: RefId14flag: Boolean15number: Int16numbers: [Int]17title: String18}1920input com_company_BarInput {21barTitle: String22}
Inputs:
id: diff ID to match the main diff input for the record creation or updatediffs: every embedded record updated by the mutation requires a diff input with its relevant changestoolUser: user ID, username, or email so history for the record can be updated appropriately
DiffId inputs can be an existing UUID, new UUID, or even a random identifier. Existing UUIDs are necessary for updating existing records while new UUIDs create new records. The random identifier feature allows callers to create new records with invalid UUIDs, such as “1,” to simplify record creation. Random identifiers are matched up and converted to new, valid UUIDs upon record creation.
RefId inputs are utilized for pointing to non-embedded records via their ID.
CMA examples
Below are a few examples of typical create and update CMA save mutations. They are run against a schema with Foo and Baz configured as mutable entry fields. Since Baz is not an embedded record on Foo, and instead referenced by Foo, it needs to be an entry field in order to retrieve more data than _id and _type.
Record creation
The following example shows the creation of a Foo record. Notice that a non-embedded Baz record, already in existence, is assigned to it.
1Query2mutation MyMutation {3com_company_FooSave(id: "1", toolUser: "[email protected]",4diffs: [5{6id: "1",7com_company_FooDiff: {8title: "Hello World!"9flag: true10number: 111numbers: [2, 3]12baz: "00000177-72e6-d6fd-a97f-76f6002a0000"13}14}15]16) {17_id18title19flag20number21numbers22baz {23_id24bazTitle25}26}27}
1Response2{3"data": {4"com_company_FooSave": {5"_id": "00000177-72ea-d6fd-a97f-76fa15700000",6"title": "Hello World!",7"flag": true,8"number": 1,9"numbers": [2, 3],10"baz": {11"_id": "00000177-72e6-d6fd-a97f-76f6002a0000",12"bazTitle": "Hello Human!"13}14}15}16}
Here’s an example where embedded record data is supplied during creation. An embedded Bar record is created and assigned to the new Foo record.
1Query2mutation MyMutation {3com_company_FooSave(id: "1", toolUser: "[email protected]",4diffs: [5{6id: "1",7com_company_FooDiff: {8bar: "2"9}10},11{12id: "2",13com_company_BarDiff: {14barTitle: "Hello Atom!"15}16}17]18) {19_id20bar {21_id22barTitle23}24}25}
1Response2{3"data": {4"com_company_FooSave": {5"_id": "00000177-72ea-d6fd-a97f-76fa15700000",6"bar": {7"_id": "00000177-72ea-d6fd-a97f-76fa15700001",8"barTitle": "Hello Atom!"9}10}11}12}
Record update
The following example shows an update to the existing Foo record from the previous example. Notice that the embedded Bar record does not need to be reassigned to the Foo record, yet its value can still be updated. Additionally, a different non-embedded Baz record is assigned.
1Query2mutation MyMutation {3com_company_FooSave(id: "00000177-72ea-d6fd-a97f-76fa15700000", toolUser: "[email protected]",4diffs: [5{6id: "00000177-72ea-d6fd-a97f-76fa15700000",7com_company_FooDiff: {8title: "Hello Universe!"9baz: "00000177-72f2-d6fd-a97f-76f281400000"10}11},12{13id: "00000177-72ea-d6fd-a97f-76fa15700001",14com_company_BarDiff: {15barTitle: "Hello Quark!"16}17}18]19) {20_id21title22bar {23_id24barTitle25}26baz {27_id28bazTitle29}30}31}
1Response2{3"data": {4"com_company_FooSave": {5"_id": "00000177-72ea-d6fd-a97f-76fa15700000",6"title": "Hello Universe!",7"bar": {8"_id": "00000177-72ea-d6fd-a97f-76fa15700001",9"barTitle": "Hello Quark!"10},11"baz": {12"_id": "00000177-72f2-d6fd-a97f-76f281400000",13"bazTitle": "Hello Sentient Being!"14}15}16}17}