fetch-graphql
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

fetch-graphql

Super simple GraphQL fetch client, with built in error handling.

Installing

npm add fetch-graphql

Usage

import fetchGraphQL from 'fetch-graphql'

const query = `
  query ListUsers($filter: UserFilter) {
    users(filter: $filter) {
      id

      firstName
      lastName
    }
  }
`

const variables = {
  filter: {
    firstName: 'Safaiyeh'
  }
}

const data = await fetchGraphQL(
  'https://yourAPI.com/graphql',
  query,
  variables,
  { 'header1': 'headerValue1', ...headers }
)

console.log(data.users)

Error handling

If the GraphQL response contains errors, the returned Promise will reject with a fully populated error. If there was multiple errors, it rejects with an AggregateError that contains all the errors.

AWS AppSync

Pass your API Key to x-api-key header

const headers = {
  'x-api-key': '<AWS KEY HERE>'
}

Readme

Keywords

none

Package Sidebar

Install

npm i fetch-graphql

Weekly Downloads

12

Version

1.1.0

License

MIT

Unpacked Size

6.03 kB

Total Files

5

Last publish

Collaborators

  • linusu
  • safaiyeh