6.1.0 • Published 2 days ago

graphql-request v6.1.0

Weekly downloads
1,356,711
License
MIT
Repository
github
Last release
2 days ago

graphql-request

Minimal GraphQL client supporting Node and browsers for scripts or simple apps

GitHub Action npm version

Features

  • Most simple & lightweight GraphQL client
  • Promise-based API (works with async / await)
  • Typescript support
  • Isomorphic (works with Node / browsers)

Install

npm add graphql-request

Quickstart

Send a GraphQL query with a single line of code. ▶️ Try it out.

import { request } from 'graphql-request'

const query = `{
  Movie(title: "Inception") {
    releaseDate
    actors {
      name
    }
  }
}`

request('https://api.graph.cool/simple/v1/movies', query).then((data) => console.log(data))

Usage

import { request, GraphQLClient } from 'graphql-request'

// Run GraphQL queries/mutations using a static function
request(endpoint, query, variables).then((data) => console.log(data))

// ... or create a GraphQL client instance to send requests
const client = new GraphQLClient(endpoint, { headers: {} })
client.request(query, variables).then((data) => console.log(data))

Examples

Authentication via HTTP header

import { GraphQLClient } from 'graphql-request'

async function main() {
  const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

  const graphQLClient = new GraphQLClient(endpoint, {
    headers: {
      authorization: 'Bearer MY_TOKEN',
    },
  })

  const query = /* GraphQL */ `
    {
      Movie(title: "Inception") {
        releaseDate
        actors {
          name
        }
      }
    }
  `

  const data = await graphQLClient.request(query)
  console.log(JSON.stringify(data, undefined, 2))
}

main().catch((error) => console.error(error))

TypeScript Source

Passing more options to fetch

import { GraphQLClient } from 'graphql-request'

async function main() {
  const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

  const graphQLClient = new GraphQLClient(endpoint, {
    credentials: 'include',
    mode: 'cors',
  })

  const query = /* GraphQL */ `
    {
      Movie(title: "Inception") {
        releaseDate
        actors {
          name
        }
      }
    }
  `

  const data = await graphQLClient.request(query)
  console.log(JSON.stringify(data, undefined, 2))
}

main().catch((error) => console.error(error))

TypeScript Source

Using variables

import { request } from 'graphql-request'

async function main() {
  const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

  const query = /* GraphQL */ `
    query getMovie($title: String!) {
      Movie(title: $title) {
        releaseDate
        actors {
          name
        }
      }
    }
  `

  const variables = {
    title: 'Inception',
  }

  const data = await request(endpoint, query, variables)
  console.log(JSON.stringify(data, undefined, 2))
}

main().catch((error) => console.error(error))

TypeScript Source

Error handling

import { request } from 'graphql-request'

async function main() {
  const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

  const query = /* GraphQL */ `
    {
      Movie(title: "Inception") {
        releaseDate
        actors {
          fullname # "Cannot query field 'fullname' on type 'Actor'. Did you mean 'name'?"
        }
      }
    }
  `

  try {
    const data = await request(endpoint, query)
    console.log(JSON.stringify(data, undefined, 2))
  } catch (error) {
    console.error(JSON.stringify(error, undefined, 2))
    process.exit(1)
  }
}

main().catch((error) => console.error(error))

TypeScript Source

Using require instead of import

const { request } = require('graphql-request')

async function main() {
  const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

  const query = /* GraphQL */ `
    {
      Movie(title: "Inception") {
        releaseDate
        actors {
          name
        }
      }
    }
  `

  const data = await request(endpoint, query)
  console.log(JSON.stringify(data, undefined, 2))
}

main().catch((error) => console.error(error))

Cookie support for node

npm install fetch-cookie
require('fetch-cookie/node-fetch')(require('node-fetch'))

import { GraphQLClient } from 'graphql-request'

async function main() {
  const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

  const graphQLClient = new GraphQLClient(endpoint, {
    headers: {
      authorization: 'Bearer MY_TOKEN',
    },
  })

  const query = /* GraphQL */ `
    {
      Movie(title: "Inception") {
        releaseDate
        actors {
          name
        }
      }
    }
  `

  const data = await graphQLClient.rawRequest(query)
  console.log(JSON.stringify(data, undefined, 2))
}

main().catch((error) => console.error(error))

TypeScript Source

Receiving a raw response

The request method will return the data or errors key from the response. If you need to access the extensions key you can use the rawRequest method:

import { rawRequest } from 'graphql-request'

async function main() {
  const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

  const query = /* GraphQL */ `
    {
      Movie(title: "Inception") {
        releaseDate
        actors {
          name
        }
      }
    }
  `

  const { data, errors, extensions, headers, status } = await rawRequest(endpoint, query)
  console.log(JSON.stringify({ data, errors, extensions, headers, status }, undefined, 2))
}

main().catch((error) => console.error(error))

TypeScript Source

More examples coming soon...

FAQ

What's the difference between graphql-request, Apollo and Relay?

graphql-request is the most minimal and simplest to use GraphQL client. It's perfect for small scripts or simple apps.

Compared to GraphQL clients like Apollo or Relay, graphql-request doesn't have a built-in cache and has no integrations for frontend frameworks. The goal is to keep the package and API as minimal as possible.

@jujulego/jill-myr@stoplight/clibosagora-dao-sdk-client@graphql-hive/cli@balancer-labs/sdk@amygrooove1/smart-order-router-horiza-fork@teambit/legacy@blockswaplab/cip-sdk@pangolindex/components@commercetools-frontend/mc-scripts@requestnetwork/request-node@real-wagmi/smart-order-routerpratech-middleware-authorizationresource-centerpadlocks@stefancfuchs/apolar-webcrawler@stefancfuchs/firefly-chatbotqester-cliproducts-servicetesting-cli-ajay@masp/merck-cra-clientnode-meterapivida-cmsgester-cli@coprime/codashtowify_sdk@coderich/autograph-serverbeamer-gatsby-source-shopifyavailability-graphql-apigrevera-domain-models@alaka/notification-service-functions@alaka/notificationsideompotent-aideompotent-middlewareideompotent-token-middlewareideompotentcy-validatorwallet-transfermemberstack-stripedarkroom-clikili-playground-nodealexnpmtest1towify-sdkcycle-graphql@luxrobo/graphql-request@zettabit/rpglogs-api-sdkfloflorico@provablyfair/gatsby-theme-landingamm-info@wipefest/core@faros-ai/cli@whitehead/sdk@kaercher/contentful@skylar745/3box@mc3-aether/aether@luxrobo/webview-js-channelgatsby-source-pilonexample-app-nextjs8base@dexorzoswap/token-listsgithub-api-fetcherpe-layouts@nubohq/servicecalenddar-clientnuxt-gqlneutronpay-wallet-common@casthub/sdk@djaciel/api-runner@brandingbrand/fsshopifydsdfsdfjklsdfjklsdfklj@tomfern/api2btv-video-player@brgx/asset-swapper@tnotifier/sdkbuysellexchangeio-fnc-eventbuysellexchangeio-fnc-gmerchantbuysellexchangeio-fnc-shopifylinear-ci-release-toolgraphqldatagraphqlmenudatagraphqlmenuitemgraphqlmenuitemsgraphqlpackagegraphqlmenupdq-core@a1pos/engine@gokumarketdex/token-lists@cherrytwist/client-libbsmat-test@lotun/apiswash-smart-router@enrolla/node-server-sdk@ersanyakit/smart-order-router@fildomains/fnsjs@metalabel/metalabel-sdk@saasquatch-themes/stafftrack-components@samsquatch/alteryx-components@samsquatch/dooly-components@samsquatch/service-titan-components@prodam/prodam-abilities@prodam/prodam-types
7.0.0-next.23

2 days ago

7.0.0-next.21

1 month ago

7.0.0-next.22

1 month ago

7.0.0-next.20

1 month ago

7.0.0-next.19

1 month ago

7.0.0-next.18

1 month ago

7.0.0-next.17

1 month ago

7.0.0-next.15

1 month ago

7.0.0-next.16

1 month ago

7.0.0-next.14

2 months ago

7.0.0-next.13

2 months ago

7.0.0-next.12

3 months ago

7.0.0-next.9

5 months ago

7.0.0-next.7

5 months ago

7.0.0-next.8

5 months ago

7.0.0-next.5

5 months ago

7.0.0-next.6

5 months ago

7.0.0-next.10

4 months ago

7.0.0-next.11

4 months ago

6.2.0-next.3

6 months ago

6.2.0-next.4

6 months ago

6.1.0

10 months ago

6.1.1-next.1

10 months ago

6.1.1-next.2

9 months ago

6.1.0-next.4

10 months ago

6.0.0

11 months ago

6.1.0-next.3

11 months ago

6.1.0-next.1

11 months ago

6.1.0-next.2

11 months ago

5.2.0-next.5

1 year ago

5.2.0-next.4

1 year ago

5.2.0-next.3

1 year ago

5.1.1-next.2

1 year ago

5.2.0

1 year ago

5.2.1-next.1

1 year ago

6.0.0-next.2

12 months ago

6.0.0-next.3

12 months ago

6.0.0-next.4

12 months ago

6.0.0-next.5

12 months ago

6.0.0-next.6

12 months ago

6.0.0-next.7

12 months ago

5.1.1-next.1

1 year ago

5.1.0

1 year ago

5.1.0-next.3

1 year ago

5.1.0-next.4

1 year ago

5.1.0-next.5

1 year ago

5.1.0-next.6

1 year ago

5.0.1-next.2

1 year ago

5.0.0

2 years ago

5.0.1-next.1

1 year ago

5.0.0-next.6

2 years ago

4.3.0-next.2

2 years ago

4.4.0-next.3

2 years ago

4.4.0-next.2

2 years ago

4.4.0-next.4

2 years ago

4.4.0-next.1

2 years ago

5.0.0-next.5

2 years ago

4.3.0

2 years ago

4.3.0-next.1

2 years ago

4.1.0-next.3

2 years ago

4.2.0

2 years ago

4.0.0-next.2

2 years ago

4.2.0-next.1

2 years ago

4.2.0-next.2

2 years ago

4.1.0

2 years ago

4.0.0

2 years ago

4.0.0-next.1

2 years ago

3.7.0

2 years ago

3.6.1

2 years ago

3.6.0

2 years ago

3.5.0

3 years ago

3.4.0

3 years ago

3.4.0-next.1

3 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.2.0-next.2

3 years ago

3.2.0-next.3

3 years ago

3.2.0-next.1

3 years ago

3.1.0

4 years ago

3.1.0-next.4

4 years ago

3.0.1-next.3

4 years ago

3.0.1-next.2

4 years ago

3.0.1-next.1

4 years ago

3.0.0

4 years ago

3.0.0-next.4

4 years ago

2.1.0-next.2

4 years ago

2.1.0-next.3

4 years ago

2.1.0-next.1

4 years ago

2.0.0

4 years ago

1.8.2

6 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.6

7 years ago

1.3.5

7 years ago

1.3.5-alpha

7 years ago

1.3.4

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago