support error handling
hera
π©πΌβπ» Simple and lightweight GraphQL client.
π§° installation
yarn add hera-js
π³ usage
import { hera } from 'hera-js';
const { data } = await hera({
option: {
url: 'https://example.com'
},
query: `
query {
user(id: $id) {
id
name
age
}
}
`,
variables: {
id: 1
}
});
π» passing parameters as objects
const { data } = await hera({
option: {
url: 'https://example.com'
},
query: `
mutation {
createUser(info: $info) {
id
name
age
address
job
}
}
`,
variables: {
info: {
name: 'Cuong Tran',
age: 22,
address: 'Sai Gon / Vietnam',
job: 'software engineer'
}
}
});
π error handling
const { data, errors } = await hera({
option: {
url: 'https://example.com'
},
query: `
query {
user(id: $id) {
id
name
age
}
}
`,
variables: {
id: 1
}
});
π API
hera({
option: Option;
query: string;
variables?: any;
}) : Promise<{ data: any; errors: any[] }>
π options
{
// `url` is the server URL that will be used for the request
url: '/example',
// `headers` are custom headers to be sent
headers: {
token: 'Fv0761DZcunUr0dKBc4oo5k55jJchwqu',
'Content-Type': 'application/json'
}
}
π query
query
is query or mutation in Graphql
graphql's query
query: `
query {
user(id: $id) {
id
name
age
}
}
`
graphql's mutation
query: `
mutation {
createUser(info: $info) {
id
name
age
address
job
}
}
`
π variables
variables is used to pass values to query's variables
query: `
mutation {
createUser(info: $info) {
id
name
age
address
job
}
}
`,
variables: {
info: {
name: 'Cuong Tran',
age: 22,
address: 'Sai Gon / Vietnam',
job: 'software engineer'
}
}
Contributors
License
MIT Β© Cuong Tran