Mutations
There are entry points to a GraphQL API to make requests, which both take inputs.
| Request type | Purpose | REST comparison |
|---|---|---|
| Mutations | Make a change | PUT or POST |
| Queries | Fetch data from the remote API | GET |
Each request starts with the keyword query or mutation, then specifies the field or fields you want to get back in the response.
Mutation example
Consider the following example, which calls the mutation updateAccount to update the account's language.
Note the ... on UpdateAccountSuccessPayload, in which you can choose to return the account's language in the payload.
Swan recommends adding validations systematically, as well as rejections.
Note that this is a basic example. Consider the example of requesting a merchant payment method to see more complexity. You can also explore all of Swan's mutations in API Explorer, including this one.
Mutation
mutation ChangeLanguage {
updateAccount( #mutation name
input: { accountId: "$YOUR_ACCOUNT_ID", language: en } #input information
) {
... on UpdateAccountSuccessPayload { #validation
__typename
account {
language #returns account language in payload
}
}
}
}
Payload
{
"data": {
"updateAccount": {
"__typename": "UpdateAccountSuccessPayload",
"account": {
"language": "en"
}
}
}
}