GraphQL
Pengembang dapat menggunakan fungsi dalam gql
untuk operasi yang berhubungan dengan data (CRUD) pada backend dalam metode Graphql.
client.gql;
GraphQL methods
query(payload)
digunakan untuk mengirim query graphql.
Arguments
name | type | description | special use |
---|---|---|---|
payload | object | payload graphql | |
payload.query | string / Object | query graphql | |
payload.variables | Object | variables untuk query graphql |
Return
Promise<Object>
berisi response dari endpoint /graphql
Example
const query = {
query: `
query($limit: Int, $sort: UsersSortEnum){
allUsersList(limit: $limit, sort: $sort){
data {
id
name
}
count
}
}
`,
variables: {
limit: 10,
sort: "name",
},
};
(async function () {
const result = await client.gql.query(query);
console.log("gql_query => ", result);
})();
// menggunakan graphql-tag
import gqltag from "graphql-tag";
const query_tag = {
query: gqltag`
query($limit: Int, $sort: UsersSortEnum) {
allUsersList(limit: $limit, sort: $sort) {
data {
id
name
}
count
}
}
`,
variables: {
limit: 10,
sort: "name",
},
};
(async function () {
const result_tag = await client.gql.query(query_tag);
console.log("gql_query_tag => ", result_tag);
})();
mutation(payload)
digunakan untuk mengirim mutation graphql.
Arguments
name | type | description | special use |
---|---|---|---|
payload | object | payload graphql | |
payload.mutation | string / Object | mutation graphql | |
payload.variables | Object | variables untuk mutation graphql |
Return
Promise<Object>
berisi response dari endpoint /graphql
Example
const mutation = {
mutation: `
mutation($ids: [UUID!]!, $input: UsersUpdateInput!){
updateUsersById(ids: $ids , input: $input){
id
roles
email
password
name
country
address
}
}
`,
variables: {
ids: "cd64140d-abef-4a88-9ec1-bf2e87e08455",
input: { name: "name-update" },
},
};
(async function () {
const result = await client.gql.mutation(mutation);
console.log("gql_mutation => ", result);
})();
// menggunakan graphql-tag
import gqltag from "graphql-tag";
const mutation_tag = {
mutation: gqltag`
mutation($ids: [UUID!]!, $input: UsersUpdateInput!){
updateUsersById(ids: $ids , input: $input){
id
roles
email
password
name
country
address
}
}
`,
variables: {
ids: "cd64140d-abef-4a88-9ec1-bf2e87e08455",
input: { name: "name-update" },
},
};
(async function () {
const result_tag = await client.gql.mutation(mutation_tag);
console.log("gql_mutation_tag => ", result_tag);
})();