UNPKG

4.32 kBTypeScriptView Raw
1import { BatchRequestDocument, ClientError, RequestDocument, Variables } from './types';
2import * as Dom from './types.dom';
3export { BatchRequestDocument, ClientError, RequestDocument, Variables };
4/**
5 * todo
6 */
7export declare class GraphQLClient {
8 private url;
9 private options;
10 constructor(url: string, options?: Dom.RequestInit);
11 rawRequest<T = any, V = Variables>(query: string, variables?: V, requestHeaders?: Dom.RequestInit['headers']): Promise<{
12 data: T;
13 extensions?: any;
14 headers: Dom.Headers;
15 status: number;
16 }>;
17 /**
18 * Send a GraphQL document to the server.
19 */
20 request<T = any, V = Variables>(document: RequestDocument, variables?: V, requestHeaders?: Dom.RequestInit['headers']): Promise<T>;
21 /**
22 * Send a GraphQL document to the server.
23 */
24 batchRequests<T extends any = any, V = Variables>(documents: BatchRequestDocument<V>[], requestHeaders?: Dom.RequestInit['headers']): Promise<T>;
25 setHeaders(headers: Dom.RequestInit['headers']): GraphQLClient;
26 /**
27 * Attach a header to the client. All subsequent requests will have this header.
28 */
29 setHeader(key: string, value: string): GraphQLClient;
30 /**
31 * Change the client endpoint. All subsequent requests will send to this endpoint.
32 */
33 setEndpoint(value: string): GraphQLClient;
34}
35/**
36 * todo
37 */
38export declare function rawRequest<T = any, V = Variables>(url: string, query: string, variables?: V, requestHeaders?: Dom.RequestInit['headers']): Promise<{
39 data: T;
40 extensions?: any;
41 headers: Dom.Headers;
42 status: number;
43}>;
44/**
45 * Send a GraphQL Document to the GraphQL server for exectuion.
46 *
47 * @example
48 *
49 * ```ts
50 * // You can pass a raw string
51 *
52 * await request('https://foo.bar/graphql', `
53 * {
54 * query {
55 * users
56 * }
57 * }
58 * `)
59 *
60 * // You can also pass a GraphQL DocumentNode. Convenient if you
61 * // are using graphql-tag package.
62 *
63 * import gql from 'graphql-tag'
64 *
65 * await request('https://foo.bar/graphql', gql`...`)
66 *
67 * // If you don't actually care about using DocumentNode but just
68 * // want the tooling support for gql template tag like IDE syntax
69 * // coloring and prettier autoformat then note you can use the
70 * // passthrough gql tag shipped with graphql-request to save a bit
71 * // of performance and not have to install another dep into your project.
72 *
73 * import { gql } from 'graphql-request'
74 *
75 * await request('https://foo.bar/graphql', gql`...`)
76 * ```
77 */
78export declare function request<T = any, V = Variables>(url: string, document: RequestDocument, variables?: V, requestHeaders?: Dom.RequestInit['headers']): Promise<T>;
79/**
80 * Send a batch of GraphQL Document to the GraphQL server for exectuion.
81 *
82 * @example
83 *
84 * ```ts
85 * // You can pass a raw string
86 *
87 * await batchRequests('https://foo.bar/graphql', [
88 * {
89 * query: `
90 * {
91 * query {
92 * users
93 * }
94 * }`
95 * },
96 * {
97 * query: `
98 * {
99 * query {
100 * users
101 * }
102 * }`
103 * }])
104 *
105 * // You can also pass a GraphQL DocumentNode as query. Convenient if you
106 * // are using graphql-tag package.
107 *
108 * import gql from 'graphql-tag'
109 *
110 * await batchRequests('https://foo.bar/graphql', [{ query: gql`...` }])
111 * ```
112 */
113export declare function batchRequests<T extends any = any, V = Variables>(url: string, documents: BatchRequestDocument<V>[], requestHeaders?: Dom.RequestInit['headers']): Promise<T>;
114export default request;
115/**
116 * 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.
117 *
118 * @example
119 *
120 * import { gql } from 'graphql-request'
121 *
122 * await request('https://foo.bar/graphql', gql`...`)
123 *
124 * @remarks
125 *
126 * 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.
127 */
128export declare function gql(chunks: TemplateStringsArray, ...variables: any[]): string;