graphql-request

  • Version 6.1.0
  • Published
  • 204 kB
  • 2 dependencies
  • MIT license

Install

npm i graphql-request
yarn add graphql-request
pnpm add graphql-request

Overview

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

Index

Variables

variable batchRequests

const batchRequests: BatchRequests;
  • Send a batch of GraphQL Document to the GraphQL server for execution.

    Example 1

    // You can pass a raw string
    await batchRequests('https://foo.bar/graphql', [
    {
    query: `
    {
    query {
    users
    }
    }`
    },
    {
    query: `
    {
    query {
    users
    }
    }`
    }])
    // You can also pass a GraphQL DocumentNode as query. Convenient if you
    // are using graphql-tag package.
    import gql from 'graphql-tag'
    await batchRequests('https://foo.bar/graphql', [{ query: gql`...` }])

variable rawRequest

const rawRequest: RawRequest;
  • Send a GraphQL Query to the GraphQL server for execution.

Functions

function gql

gql: (chunks: TemplateStringsArray, ...variables: unknown[]) => string;
  • Convenience passthrough template tag to get the benefits of tooling for the gql template tag. This does not actually parse the input into a GraphQL DocumentNode like graphql-tag package does. It just returns the string with any variables given interpolated. Can save you a bit of performance and having to install another package.

    Remarks

    Several tools in the Node GraphQL ecosystem are hardcoded to specially treat any template tag named "gql". For example see this prettier issue: https://github.com/prettier/prettier/issues/4360. Using this template tag has no runtime effect beyond variable interpolation.

    Example 1

    import { gql } from 'graphql-request'
    await request('https://foo.bar/graphql', gql`...`)

function request

request: {
<T, V extends Variables = Variables>(
options: RequestExtendedOptions<V, T>
): Promise<T>;
<T, V extends Variables = Variables>(
url: string,
document: any,
...variablesAndRequestHeaders: VariablesAndRequestHeadersArgs<V>
): Promise<T>;
};
  • Send a GraphQL Document to the GraphQL server for execution.

    Example 1

    // You can pass a raw string
    await request('https://foo.bar/graphql', `
    {
    query {
    users
    }
    }
    `)
    // You can also pass a GraphQL DocumentNode. Convenient if you
    // are using graphql-tag package.
    import gql from 'graphql-tag'
    await request('https://foo.bar/graphql', gql`...`)
    // If you don't actually care about using DocumentNode but just
    // want the tooling support for gql template tag like IDE syntax
    // coloring and prettier autoformat then note you can use the
    // passthrough gql tag shipped with graphql-request to save a bit
    // of performance and not have to install another dep into your project.
    import { gql } from 'graphql-request'
    await request('https://foo.bar/graphql', gql`...`)

function resolveRequestDocument

resolveRequestDocument: (document: RequestDocument) => {
query: string;
operationName?: string | undefined;
};

    Classes

    class ClientError

    class ClientError extends Error {}

      constructor

      constructor(
      response: GraphQLResponse<unknown>,
      request: GraphQLRequestContext<Variables>
      );

        property request

        request: GraphQLRequestContext<Variables>;

          property response

          response: GraphQLResponse<unknown>;

            class GraphQLClient

            class GraphQLClient {}
            • GraphQL Client.

            constructor

            constructor(url: string, requestConfig?: RequestConfig);

              property rawRequest

              rawRequest: RawRequestMethod;
              • Send a GraphQL query to the server.

              property requestConfig

              readonly requestConfig: RequestConfig;

                method batchRequests

                batchRequests: {
                <T extends BatchResult, V extends Variables = Variables>(
                documents: BatchRequestDocument<V>[],
                requestHeaders?: GraphQLClientRequestHeaders
                ): Promise<T>;
                <T extends BatchResult, V extends Variables = Variables>(
                options: BatchRequestsOptions<V>
                ): Promise<T>;
                };
                • Send GraphQL documents in batch to the server.

                method request

                request: {
                <T, V extends Variables = Variables>(
                document: RequestDocument | TypedDocumentNode<T, V>,
                ...variablesAndRequestHeaders: VariablesAndRequestHeadersArgs<V>
                ): Promise<T>;
                <T, V extends Variables = Variables>(
                options: RequestOptions<V, T>
                ): Promise<T>;
                };
                • Send a GraphQL document to the server.

                method setEndpoint

                setEndpoint: (value: string) => GraphQLClient;
                • Change the client endpoint. All subsequent requests will send to this endpoint.

                method setHeader

                setHeader: (key: string, value: string) => GraphQLClient;
                • Attach a header to the client. All subsequent requests will have this header.

                method setHeaders

                setHeaders: (headers: GraphQLClientRequestHeaders) => GraphQLClient;

                  class GraphQLWebSocketClient

                  class GraphQLWebSocketClient {}

                    constructor

                    constructor(
                    socket: WebSocket,
                    { onInit, onAcknowledged, onPing, onPong }: SocketHandler
                    );

                      property PROTOCOL

                      static PROTOCOL: string;

                        method close

                        close: () => void;

                          method ping

                          ping: (payload: Variables) => void;

                            method rawRequest

                            rawRequest: <T = any, V extends Variables = Variables, E = any>(
                            query: string,
                            variables?: V
                            ) => Promise<{ data: T; extensions?: E }>;

                              method rawSubscribe

                              rawSubscribe: <T = any, V extends Variables = Variables, E = any>(
                              query: string,
                              subscriber: GraphQLSubscriber<T, E>,
                              variables?: V
                              ) => UnsubscribeCallback;

                                method request

                                request: <T = any, V extends Variables = Variables>(
                                document: RequestDocument,
                                variables?: V
                                ) => Promise<T>;

                                  method subscribe

                                  subscribe: <T = any, V extends Variables = Variables, E = any>(
                                  document: RequestDocument,
                                  subscriber: GraphQLSubscriber<T, E>,
                                  variables?: V
                                  ) => UnsubscribeCallback;

                                    Interfaces

                                    interface BatchRequestsExtendedOptions

                                    interface BatchRequestsExtendedOptions<V extends Variables = Variables>
                                    extends BatchRequestsOptions<V> {}

                                      property url

                                      url: string;

                                        interface BatchRequestsOptions

                                        interface BatchRequestsOptions<V extends Variables = Variables> {}

                                          property documents

                                          documents: BatchRequestDocument<V>[];

                                            property requestHeaders

                                            requestHeaders?: GraphQLClientRequestHeaders;

                                              property signal

                                              signal?: RequestInit['signal'];

                                                Type Aliases

                                                type BatchRequestDocument

                                                type BatchRequestDocument<V extends Variables = Variables> = {
                                                document: RequestDocument;
                                                variables?: V;
                                                };

                                                  type RawRequestExtendedOptions

                                                  type RawRequestExtendedOptions<V extends Variables = Variables> = {
                                                  url: string;
                                                  } & RawRequestOptions<V>;

                                                    type RawRequestOptions

                                                    type RawRequestOptions<V extends Variables = Variables> = {
                                                    query: string;
                                                    requestHeaders?: GraphQLClientRequestHeaders;
                                                    signal?: RequestInit['signal'];
                                                    } & (V extends Record<any, never>
                                                    ? {
                                                    variables?: V;
                                                    }
                                                    : keyof RemoveIndex<V> extends never
                                                    ? {
                                                    variables?: V;
                                                    }
                                                    : {
                                                    variables: V;
                                                    });

                                                      type RequestDocument

                                                      type RequestDocument = string | DocumentNode;

                                                        type RequestExtendedOptions

                                                        type RequestExtendedOptions<V extends Variables = Variables, T = unknown> = {
                                                        url: string;
                                                        } & RequestOptions<V, T>;

                                                          type RequestMiddleware

                                                          type RequestMiddleware<V extends Variables = Variables> = (
                                                          request: RequestExtendedInit<V>
                                                          ) => RequestExtendedInit | Promise<RequestExtendedInit>;

                                                            type RequestOptions

                                                            type RequestOptions<V extends Variables = Variables, T = unknown> = {
                                                            document: RequestDocument | TypedDocumentNode<T, V>;
                                                            requestHeaders?: GraphQLClientRequestHeaders;
                                                            signal?: RequestInit['signal'];
                                                            } & (V extends Record<any, never>
                                                            ? {
                                                            variables?: V;
                                                            }
                                                            : keyof RemoveIndex<V> extends never
                                                            ? {
                                                            variables?: V;
                                                            }
                                                            : {
                                                            variables: V;
                                                            });

                                                              type ResponseMiddleware

                                                              type ResponseMiddleware = (response: GraphQLClientResponse<unknown> | Error) => void;

                                                                type Variables

                                                                type Variables = Record<string, unknown>;

                                                                  Package Files (4)

                                                                  Dependencies (2)

                                                                  Dev Dependencies (36)

                                                                  Peer Dependencies (1)

                                                                  Badge

                                                                  To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                  You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/graphql-request.

                                                                  • Markdown
                                                                    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/graphql-request)
                                                                  • HTML
                                                                    <a href="https://www.jsdocs.io/package/graphql-request"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>