Perform multiple actions using GraphQL aliases
Skedulo GraphQL supports mutations that include aliases for performing multiple actions. All mutations in a schema
block are performed in a transaction, which reduces the number of mutations required to create, allocate, schedule, update, and dispatch a job.
Update multiple jobs using an alias prefix
You can use aliases as a prefix on update
or insert
operations to update multiple objects at a time. For example, the following mutation updates the Duration
field to 60
on three jobs:
mutation updateMultipleJobs {
schema {
_j0: updateJobs(input: {
UID: "00146e2f-c8db-4593-adc2-084abc866785"
Duration: 60
})
_j1: updateJobs(input: {
UID: "0014586b-3fa3-4f74-a5b1-a47f22a4b3a2"
Duration: 60
})
_j2: updateJobs(input: {
UID: "001419d8-ea58-40d8-8fc9-7e5e114af0e0"
Duration: 60
})
}
}
This returns a successful response that provides an ID for each successful update:
{
"data": {
"schema": {
"_j0": "00146e2f-c8db-4593-adc2-084abc866785",
"_j1": "0014586b-3fa3-4f74-a5b1-a47f22a4b3a2",
"_j2": "001419d8-ea58-40d8-8fc9-7e5e114af0e0"
}
}
}
Use idAlias
to perform multiple actions in a single mutation
Because you require the job UID
to make any changes to a job, the above method of using an alias as a prefix cannot be used to perform updates on a job that is being created in the same mutation.
GraphQL mutations are sequential
Each action in the request is performed sequentially, so it is important that you create the object being modified first before performing any mutations on it.For example, you cannot include insertJobTags
mutation before you have created the job using the insertJobs
mutation.
Making a change to a job (like adding a tag or assigning a resource) requires a UID to look up the job object and perform the mutation.
The idAlias
works as a stand-in for the UID in the mutation request. Each part of the GraphQL mutation following the use of an idAlias
can use the value to identify the object on which it is performing the mutation.
The following mutation returns an error, as we are unable to identify the UID
of the job being created in the insertJob
part of the mutation:
mutation createJobsWithAllocations {
schema {
_j0: insertJobs(input: {
RegionId: "0003035f-366a-4d18-8c2d-1b9c99cf27bd"
Start: "2019-10-31T00:00:00+00:00"
End: "2019-10-31T01:30:00+00:00"
Duration: 90
Address: "1/47 Warner St, Fortitude Valley QLD 4006"
Description: "MultipleJobQuery test"
})
_ja0: insertJobAllocations(input: {
ResourceId: "00052d08-8f11-4be7-8e72-0cd70661dbe8"
})
_j1: insertJobs(input: {
RegionId: "0003035f-366a-4d18-8c2d-1b9c99cf27bd"
Start: "2019-10-31T03:00:00+00:00"
End: "2019-10-31T03:30:00+00:00"
Duration: 30
Address: "Ann St, Fortitude Valley QLD 4006"
Description: "MultipleJobQuery test"
} )
_ja1: insertJobAllocations(input: {
ResourceId: "0005b5a9-0822-4f25-9ec0-74ae4d53b626"
})
}
}
The above mutation returns the following error that insertJobAllocations
requires an ID for the job:
"message": "Field 'NewJobAllocations.JobId' of required type 'ID!' was not provided. (line 95, column 38):\n \t_ja0: insertJobAllocations(input: {\n
To perform additional actions in the same mutation, the Skedulo schema includes the idAlias
variable. This allows you to make subsequent mutations using the value you have assigned to idAlias
. The actual value of the ID is replaced at run time.
You can successfully execute the createJobsWithAllocations
mutation in the following way:
mutation createJobsWithAllocations {
schema {
_j0: insertJobs(input: {
RegionId: "0003035f-366a-4d18-8c2d-1b9c99cf27bd"
Start: "2019-10-31T00:00:00+00:00"
End: "2019-10-31T01:30:00+00:00"
Duration: 90
Address: "1/47 Warner St, Fortitude Valley QLD 4006"
Description: "MultipleJobQuery test"
} idAlias: "NEW_JOB_ID0")
_ja0: insertJobAllocations(input: {
ResourceId: "00052d08-8f11-4be7-8e72-0cd70661dbe8"
JobId: "NEW_JOB_ID0"
Status: "Pending Dispatch"
})
_j1: insertJobs(input: {
RegionId: "0003035f-366a-4d18-8c2d-1b9c99cf27bd"
Start: "2019-10-31T03:00:00+00:00"
End: "2019-10-31T03:30:00+00:00"
Duration: 30
Address: "Ann St, Fortitude Valley QLD 4006"
Description: "MultipleJobQuery test"
} idAlias: "NEW_JOB_ID1")
_ja1: insertJobAllocations(input: {
ResourceId: "0005b5a9-0822-4f25-9ec0-74ae4d53b626"
JobId: "NEW_JOB_ID1"
Status: "Pending Dispatch"
})
}
}
Create a job and a job offer, then send a resource the offer in a single mutation using GraphQL
The following GraphQL mutation includes two actions:
insertJobs
- creates a new job, including required fields.insertJobOffers
- creates a job offer for the job created in the first part of the mutation and assigns it to a resource.
mutation createJobWithOffer{
schema {
insertJobs(input: {
RegionId: "00036206-7555-4280-b1b7-86d566437391"
Start: "2019-08-02T00:00:00+00:00"
End: "2019-08-02T01:30:00+00:00"
Duration: 90
Address: "1/47 Warner St, Fortitude Valley QLD 4006"
Description: "New Job testing Aliases"
Type: "Maintenance"
Urgency: "Normal"
}, idAlias: "NEW_JOB_ID")
insertJobOffers(input: {
JobId: "NEW_JOB_ID"
}, idAlias: "NEW_JOB_OFFER_ID")
insertResourceJobOffers(input: {
JobOfferId: "NEW_JOB_OFFER_ID"
ResourceId: "0005a7e9-b1aa-44da-937f-310b921b75cc"
})
}
}
The response confirms that the job has been created and job offer has been made by providing ID strings for each successful mutation:
{
"data": {
"schema": {
"insertJobs": "0014d653-af0d-4c73-b15d-e77afee2be01",
"insertJobOffers": "00237199-0e4c-434c-a4c1-86f9326c59e5",
"insertResourceJobOffers": "002427a8-036f-4024-91d3-740bd816c4eb"
}
}
}
Feedback
Was this page helpful?