Skip to content

support error handling

Compare
Choose a tag to compare
@cuongndc9 cuongndc9 released this 09 Aug 05:29
176a8d0

hera

πŸ‘©πŸΌβ€πŸ’» Simple and lightweight GraphQL client.

All Contributors
Build Status
npm
david
Hits-of-Code
GitHub

🧰 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