Objects
In the previous step we defined the department
object type and its three relation
types: owner
, member
, and viewer
.
With these types in place, we can start creating objects to represent the departments in our application.
Our store has three departments: Spaceships, Teleporters, and Lasers.
Creating Objects
We can use a single request to create all three objects:
mutation CreateDepartmentObjects {
spaceships: setObject(object: {
type: "department"
key: "spaceships"
displayName: "Spaceships"
properties: {}
}) {
object {
key
displayName
type {
name
}
}
}
teleporters: setObject(object: {
type: "department"
key: "teleporters"
displayName: "Teleporters"
properties: {}
}) {
object {
key
displayName
type {
name
}
}
}
lasers: setObject(object: {
type: "department"
key: "lasers"
displayName: "Lasers"
properties: {}
}) {
object {
key
displayName
type {
name
}
}
}
}
Results
{
"data": {
"spaceships": {
"object": {
"key": "spaceships",
"displayName": "Spaceships"
"type": {
"name": "department"
}
}
},
"teleporters": {
"object": {
"key": "teleporters",
"displayName": "Teleporters"
"type": {
"name": "department"
}
}
},
"lasers": {
"object": {
"key": "lasers",
"displayName": "Lasers"
"type": {
"name": "department"
}
}
}
}
}
Similar to the name
field of the object and realtion types we created in the previous step, the key
value
of an object is used to identify it within other objects of the same type. Objects are uniquely identified by the
combination of their key
and type
.
See SetObjectInput for more details on the input fields.
Querying Objects
We use the objects query to find the department objects we just created.
query DepartmentObjects {
objects(first: 5, where: {type: {name: {equals: "department"}}}) {
nodes {
key
displayName
type {
name
}
}
}
}
Results
{
"data": {
"objects": {
"nodes": [
{
"key": "teleporters",
"displayName": "Teleporters",
"type": {
"name": "department"
}
},
{
"key": "lasers",
"displayName": "Lasers",
"type": {
"name": "department"
}
},
{
"key": "spaceships",
"displayName": "Spaceships",
"type": {
"name": "department"
}
}
]
}
}
}
We used the where
parameter in the objects query to only
select objects with a type named "department".