0.5.1 • Published 4 years ago

giin v0.5.1

Weekly downloads
4
License
-
Repository
github
Last release
4 years ago

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({
  options: {
    url: 'https://example.com'
  },
  query: `
    query {
      user(id: $id) {
        id
        name
        age
      }
    }
  `,
  variables: {
    id: 1
  }
});

👻 passing parameters as objects

const { data } = await hera({
  options: {
    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({
  options: {
    url: 'https://example.com'
  },
  query: `
    query {
      user(id: $id) {
        id
        name
        age
      }
    }
  `,
  variables: {
    id: 1
  }
});

🌏 global options

You can specify config defaults that will be applied to every request.

import { hera, globalOptions } from 'hera-js';

globalOptions.url = 'https://example.com';
// globalOptions.headers = <your headers>

const { data } = await hera({
  query: `
    mutation {
      createUser(info: $info) {
        id
        name
        age
        address
        job
      }
    }
  `,
  variables: {
    info: {
      name: 'Cuong Tran',
      age: 22,
      address: 'Sai Gon / Vietnam',
      job: 'software engineer'
    }
  }
});

🚀 API

interface Options {
  url?: string;
  headers?: any;
  timeout?: number;
}

hera({
  query: string;
  variables?: any;
  options?: Options;
}) : 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

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

📜 license

MIT © Cuong Tran

0.5.1

4 years ago

0.6.0

4 years ago

0.2.0

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.0

4 years ago

0.1.1

4 years ago

0.5.5

4 years ago