
The GraphQlAPI class extends the RestAPI class and manages the requests to a GraphQL API.
Fetch class, so all features of the Fetch class are available.The query method is used to send a query to the GraphQL API.
import { GraphQlAPI } from '@aracna/core';
const api = new GraphQlAPI('https://graphqlzero.almansi.me/api');
(async () => {
let query, response;
query =
'query getUsers($limit: Int) { users(options: { paginate: { limit: $limit } }) { data { email id name username } } }';
response = await api.query(query);
if (response instanceof Error) return;
// will log FetchResponse
console.log(response.data);
})();The mutation method is used to send a mutation to the GraphQL API.
import { GraphQlAPI } from '@aracna/core';
const api = new GraphQlAPI('https://graphqlzero.almansi.me/api');
(async () => {
let query, variables, response;
query =
'mutation createUser($email: String!, $name: String!, $username: String!) { createUser(input: { email: $email, name: $name, username: $username }) { email id name username } }';
variables = {
email: 'john.doe@email.com',
name: 'John Doe',
username: 'john.doe'
};
response = await api.mutation(query, variables);
if (response instanceof Error) return;
// will log FetchResponse
console.log(response.data);
})();