Library for call GraphQL API: GraphQL client
npm i graphql-call isomorphic-fetch --save
query {
users {
id
}
}
import 'isomorphic-fetch';
import api, {GraphQLCall} from "graphql-call";
let client = api({url: 'http://localhost:4000/graphql'});
client.query({
users: {
result: 'id'
}
}).then(result => {
console.log('result: ', result);
}).catch(error => {
console.error('error: ', error);
});
query {
users(search: "Test") {
id
}
}
client.query({
users: {
variables: {search: "Test"},
result: 'id'
}
});
query {
list: items (search: "Test", limit: 10, inTrash: true, labels: [1,2,4]) {
countAll
data {
id
firstName
}
}
}
let filter = {
search: "Test",
limit: 10,
inTrash: true,
labels: [1, 2, 4]
};
client.query({
items: {
variables: filter,
result: `
countAll
data {
id
firstName
}`,
alias: 'list'
}
});
query {
users {
id
}
items {
id
name
}
}
client.query({
users: {
result: 'id'
},
items: {
result: `
id
name`
}
});
Mutations using pattern: nameMutation(input: {}) ...
mutation {
addUser (input: {firstName: "Ales", lastName: "Dostal"}) {
id
}
}
client.mutation({
addUser: {
variables: {firstName: "Ales", lastName: "Dostal"},
result: 'id'
}
});
As complex query. Difference between query is:
client.query(...)
toclient.mutation(....)
let client = api({
url: 'http://localhost:4000/graphql',
headers: {
Authorization: 'Bearer xxxx',
}
});
let client = api({
url: 'http://localhost:4000/graphql',
headers: {
Authorization: 'Bearer xxxx',
ClientType: 'web',
...
}
});