diff --git a/README.md b/README.md index fa21e83d..67566a68 100644 --- a/README.md +++ b/README.md @@ -609,33 +609,33 @@ request('/api/graphql', UploadUserAvatar, { It is possible with `graphql-request` to use [batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md) via the `batchRequests()` function. Example available at [examples/batching-requests.ts](examples/batching-requests.ts) ```ts -import { batchRequests } from 'graphql-request' -;(async function () { - const endpoint = 'https://api.spacex.land/graphql/' - - const query1 = /* GraphQL */ ` - query ($id: ID!) { - capsule(id: $id) { - id - landings - } +import { batchRequests, gql } from 'graphql-request' + +const endpoint = 'https://api.spacex.land/graphql/' + +const query1 = gql` + query ($id: ID!) { + capsule(id: $id) { + id + landings } - ` + } +` - const query2 = /* GraphQL */ ` - { - rockets(limit: 10) { - active - } +const query2 = gql` + { + rockets(limit: 10) { + active } - ` + } +` - const data = await batchRequests(endpoint, [ - { document: query1, variables: { id: 'C105' } }, - { document: query2 }, - ]) - console.log(JSON.stringify(data, undefined, 2)) -})().catch((error) => console.error(error)) +const data = await batchRequests(endpoint, [ + { document: query1, variables: { id: 'C105' } }, + { document: query2 }, +]) + +console.log(JSON.stringify(data, undefined, 2)) ``` ### Cancellation @@ -800,7 +800,7 @@ Installing and configuring [GraphQL Code Generator](https://www.the-guild.dev/gr import request from 'graphql-request' import { graphql } from './gql/gql' -const getMovieQueryDocument = graphql(/* GraphQL */ ` +const getMovieQueryDocument = graphql(gql` query getMovie($title: String!) { Movie(title: $title) { releaseDate diff --git a/examples/authentication-via-http-header.ts b/examples/authentication-via-http-header.ts index d4564e4b..6589bae4 100644 --- a/examples/authentication-via-http-header.ts +++ b/examples/authentication-via-http-header.ts @@ -1,28 +1,27 @@ -import { GraphQLClient } from '../src/index.js' -;(async () => { - const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` +import { gql, GraphQLClient } from '../src/index.js' - const graphQLClient = new GraphQLClient(endpoint, { - headers: { - authorization: `Bearer MY_TOKEN`, - }, - }) +const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` - const query = /* GraphQL */ ` - { - Movie(title: "Inception") { - releaseDate - actors { - name - } +const graphQLClient = new GraphQLClient(endpoint, { + headers: { + authorization: `Bearer MY_TOKEN`, + }, +}) + +const query = gql` + { + Movie(title: "Inception") { + releaseDate + actors { + name } } - ` - - interface TData { - Movie: { releaseDate: string; actors: Array<{ name: string }> } } +` + +interface TData { + Movie: { releaseDate: string; actors: Array<{ name: string }> } +} - const data = await graphQLClient.request(query) - console.log(JSON.stringify(data, undefined, 2)) -})().catch((error) => console.error(error)) +const data = await graphQLClient.request(query) +console.log(JSON.stringify(data, undefined, 2)) diff --git a/examples/batching-requests.ts b/examples/batching-requests.ts index 9ac7c414..7156ae70 100644 --- a/examples/batching-requests.ts +++ b/examples/batching-requests.ts @@ -1,57 +1,56 @@ -import { batchRequests } from '../src/index.js' -;(async function () { - const endpoint = `https://api.spacex.land/graphql/` - - const query1 = /* GraphQL */ ` - query ($id: ID!) { - capsule(id: $id) { - id - landings - } - } - ` - const variables1 = { - id: `C105`, - } +import { batchRequests, gql } from '../src/index.js' - interface TData1 { - data: { capsule: { id: string; landings: number } } - } +const endpoint = `https://api.spacex.land/graphql/` - const query2 = /* GraphQL */ ` - { - rockets(limit: 10) { - active - } +const query1 = gql` + query ($id: ID!) { + capsule(id: $id) { + id + landings } - ` - - interface TData2 { - data: { rockets: { active: boolean }[] } } - - const query3 = /* GraphQL */ ` - query ($id: ID!) { - core(id: $id) { - id - block - original_launch - } +` +const variables1 = { + id: `C105`, +} + +interface TData1 { + data: { capsule: { id: string; landings: number } } +} + +const query2 = gql` + { + rockets(limit: 10) { + active } - ` - - const variables3 = { - id: `B1015`, } - - interface TData3 { - data: { core: { id: string; block: number; original_launch: string } } +` + +interface TData2 { + data: { rockets: { active: boolean }[] } +} + +const query3 = gql` + query ($id: ID!) { + core(id: $id) { + id + block + original_launch + } } - - const data = await batchRequests<[TData1, TData2, TData3]>(endpoint, [ - { document: query1, variables: variables1 }, - { document: query2 }, - { document: query3, variables: variables3 }, - ]) - console.log(JSON.stringify(data, undefined, 2)) -})().catch((error) => console.error(error)) +` + +const variables3 = { + id: `B1015`, +} + +interface TData3 { + data: { core: { id: string; block: number; original_launch: string } } +} + +const data = await batchRequests<[TData1, TData2, TData3]>(endpoint, [ + { document: query1, variables: variables1 }, + { document: query2 }, + { document: query3, variables: variables3 }, +]) +console.log(JSON.stringify(data, undefined, 2)) diff --git a/examples/cookie-support-for-node.ts b/examples/cookie-support-for-node.ts index 0f9561d6..5bfe2611 100644 --- a/examples/cookie-support-for-node.ts +++ b/examples/cookie-support-for-node.ts @@ -1,30 +1,29 @@ ;(global as any).fetch = require(`fetch-cookie/node-fetch`)(require(`node-fetch`)) -import { GraphQLClient } from '../src/index.js' -;(async function () { - const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` +import { gql, GraphQLClient } from '../src/index.js' - const graphQLClient = new GraphQLClient(endpoint, { - headers: { - authorization: `Bearer MY_TOKEN`, - }, - }) +const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` - const query = /* GraphQL */ ` - { - Movie(title: "Inception") { - releaseDate - actors { - name - } +const graphQLClient = new GraphQLClient(endpoint, { + headers: { + authorization: `Bearer MY_TOKEN`, + }, +}) + +const query = gql` + { + Movie(title: "Inception") { + releaseDate + actors { + name } } - ` - - interface TData { - Movie: { releaseDate: string; actors: Array<{ name: string }> } } +` + +interface TData { + Movie: { releaseDate: string; actors: Array<{ name: string }> } +} - const data = await graphQLClient.rawRequest(query) - console.log(JSON.stringify(data, undefined, 2)) -})().catch((error) => console.error(error)) +const data = await graphQLClient.rawRequest(query) +console.log(JSON.stringify(data, undefined, 2)) diff --git a/examples/custom-fetch.ts b/examples/custom-fetch.ts index bc970419..2acf1c4e 100644 --- a/examples/custom-fetch.ts +++ b/examples/custom-fetch.ts @@ -1,25 +1,24 @@ -import { GraphQLClient } from '../src/index.js' +import { gql, GraphQLClient } from '../src/index.js' import fetch from 'cross-fetch' -;(async function () { - const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` - const graphQLClient = new GraphQLClient(endpoint, { fetch: fetch }) +const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` - const query = /* GraphQL */ ` - { - Movie(title: "Inception") { - releaseDate - actors { - name - } +const graphQLClient = new GraphQLClient(endpoint, { fetch: fetch }) + +const query = gql` + { + Movie(title: "Inception") { + releaseDate + actors { + name } } - ` - - interface TData { - Movie: { releaseDate: string; actors: Array<{ name: string }> } } +` + +interface TData { + Movie: { releaseDate: string; actors: Array<{ name: string }> } +} - const data = await graphQLClient.rawRequest(query) - console.log(JSON.stringify(data, undefined, 2)) -})().catch((error) => console.error(error)) +const data = await graphQLClient.rawRequest(query) +console.log(JSON.stringify(data, undefined, 2)) diff --git a/examples/error-handling.ts b/examples/error-handling.ts index ef6ebb68..7fb08a6c 100644 --- a/examples/error-handling.ts +++ b/examples/error-handling.ts @@ -1,27 +1,26 @@ -import { request } from '../src/index.js' -;(async function () { - const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` +import { gql, request } from '../src/index.js' - const query = /* GraphQL */ ` - { - Movie(title: "Inception") { - releaseDate - actors { - fullname # "Cannot query field 'fullname' on type 'Actor'. Did you mean 'name'?" - } +const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` + +const query = gql` + { + Movie(title: "Inception") { + releaseDate + actors { + fullname # "Cannot query field 'fullname' on type 'Actor'. Did you mean 'name'?" } } - ` - - interface TData { - Movie: { releaseDate: string; actors: Array<{ name: string }> } } +` - 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) - } -})().catch((error) => console.error(error)) +interface TData { + Movie: { releaseDate: string; actors: Array<{ name: string }> } +} + +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) +} diff --git a/examples/passing-custom-header-per-request.ts b/examples/passing-custom-header-per-request.ts index 0a9a1ee3..2c7de90e 100644 --- a/examples/passing-custom-header-per-request.ts +++ b/examples/passing-custom-header-per-request.ts @@ -1,33 +1,32 @@ -import { GraphQLClient } from '../src/index.js' -;(async function () { - const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` +import { gql, GraphQLClient } from '../src/index.js' - const client = new GraphQLClient(endpoint, { - headers: { - authorization: `Bearer MY_TOKEN`, - }, - }) +const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` - const query = /* GraphQL */ ` - { - Movie(title: "Inception") { - releaseDate - actors { - name - } +const client = new GraphQLClient(endpoint, { + headers: { + authorization: `Bearer MY_TOKEN`, + }, +}) + +const query = gql` + { + Movie(title: "Inception") { + releaseDate + actors { + name } } - ` - - const requestHeaders = { - authorization: `Bearer MY_TOKEN_2`, - 'x-custom': `foo`, } +` - interface TData { - Movie: { releaseDate: string; actors: Array<{ name: string }> } - } +const requestHeaders = { + authorization: `Bearer MY_TOKEN_2`, + 'x-custom': `foo`, +} + +interface TData { + Movie: { releaseDate: string; actors: Array<{ name: string }> } +} - const data = await client.request(query, {}, requestHeaders) - console.log(JSON.stringify(data, undefined, 2)) -})().catch((error) => console.error(error)) +const data = await client.request(query, {}, requestHeaders) +console.log(JSON.stringify(data, undefined, 2)) diff --git a/examples/passing-more-options-to-fetch.ts b/examples/passing-more-options-to-fetch.ts index ceef1eb4..7362fa14 100644 --- a/examples/passing-more-options-to-fetch.ts +++ b/examples/passing-more-options-to-fetch.ts @@ -1,27 +1,26 @@ -import { GraphQLClient } from '../src/index.js' -;(async function () { - const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` +import { gql, GraphQLClient } from '../src/index.js' - const graphQLClient = new GraphQLClient(endpoint, { - credentials: `include`, - mode: `cors`, - }) +const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` - const query = /* GraphQL */ ` - { - Movie(title: "Inception") { - releaseDate - actors { - name - } +const graphQLClient = new GraphQLClient(endpoint, { + credentials: `include`, + mode: `cors`, +}) + +const query = gql` + { + Movie(title: "Inception") { + releaseDate + actors { + name } } - ` - - interface TData { - Movie: { releaseDate: string; actors: Array<{ name: string }> } } +` + +interface TData { + Movie: { releaseDate: string; actors: Array<{ name: string }> } +} - const data = await graphQLClient.request(query) - console.log(JSON.stringify(data, undefined, 2)) -})().catch((error) => console.error(error)) +const data = await graphQLClient.request(query) +console.log(JSON.stringify(data, undefined, 2)) diff --git a/examples/receiving-a-raw-response.ts b/examples/receiving-a-raw-response.ts index 59753463..9eeb6bb2 100644 --- a/examples/receiving-a-raw-response.ts +++ b/examples/receiving-a-raw-response.ts @@ -1,22 +1,21 @@ -import { rawRequest } from '../src/index.js' -;(async function () { - const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` +import { gql, rawRequest } from '../src/index.js' - const query = /* GraphQL */ ` - { - Movie(title: "Inception") { - releaseDate - actors { - name - } +const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` + +const query = gql` + { + Movie(title: "Inception") { + releaseDate + actors { + name } } - ` - - interface TData { - Movie: { releaseDate: string; actors: Array<{ name: string }> } } +` + +interface TData { + Movie: { releaseDate: string; actors: Array<{ name: string }> } +} - const { data, errors, extensions, headers, status } = await rawRequest(endpoint, query) - console.log(JSON.stringify({ data, errors, extensions, headers, status }, undefined, 2)) -})().catch((error) => console.error(error)) +const { data, errors, extensions, headers, status } = await rawRequest(endpoint, query) +console.log(JSON.stringify({ data, errors, extensions, headers, status }, undefined, 2)) diff --git a/examples/typed-document-node.ts b/examples/typed-document-node.ts index 08d9f2ef..20c9c187 100644 --- a/examples/typed-document-node.ts +++ b/examples/typed-document-node.ts @@ -1,10 +1,11 @@ -import { GraphQLClient, request } from '../src/index.js' +import { gql, GraphQLClient, request } from '../src/index.js' import type { TypedDocumentNode } from '@graphql-typed-document-node/core' import { parse } from 'graphql' -;(async function () { + +{ const endpoint = `https://graphql-yoga.com/api/graphql` - const query: TypedDocumentNode<{ greetings: string }, never | Record> = parse(/* GraphQL */ ` + const query: TypedDocumentNode<{ greetings: string }, never | Record> = parse(gql` query greetings { greetings } @@ -15,13 +16,14 @@ import { parse } from 'graphql' const data = await request(endpoint, query, variables) console.log(data.greetings) -})().catch(console.error) -;(async function () { +} + +{ const endpoint = `https://graphql-yoga.com/api/graphql` const client = new GraphQLClient(endpoint) - const query: TypedDocumentNode<{ greetings: string }, never | Record> = parse(/* GraphQL */ ` + const query: TypedDocumentNode<{ greetings: string }, never | Record> = parse(gql` query greetings { greetings } @@ -33,4 +35,4 @@ import { parse } from 'graphql' // const data = await client.request({ document: query, variables: { a: 1 } }) console.log(data.greetings) -})().catch(console.error) +} diff --git a/examples/using-variables.ts b/examples/using-variables.ts index 168ff446..2f97d1d1 100644 --- a/examples/using-variables.ts +++ b/examples/using-variables.ts @@ -1,26 +1,25 @@ -import { request } from '../src/index.js' -;(async function () { - const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` +import { gql, request } from '../src/index.js' - const query = /* GraphQL */ ` - query getMovie($title: String!) { - Movie(title: $title) { - releaseDate - actors { - name - } +const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` + +const query = gql` + query getMovie($title: String!) { + Movie(title: $title) { + releaseDate + actors { + name } } - ` - - const variables = { - title: `Inception`, } +` - interface TData { - Movie: { releaseDate: string; actors: Array<{ name: string }> } - } +const variables = { + title: `Inception`, +} + +interface TData { + Movie: { releaseDate: string; actors: Array<{ name: string }> } +} - const data = await request(endpoint, query, variables) - console.log(JSON.stringify(data, undefined, 2)) -})().catch((error) => console.error(error)) +const data = await request(endpoint, query, variables) +console.log(JSON.stringify(data, undefined, 2)) diff --git a/tests/typed-document-node.test.ts b/tests/typed-document-node.test.ts index 7a43e5ee..242df04d 100644 --- a/tests/typed-document-node.test.ts +++ b/tests/typed-document-node.test.ts @@ -1,4 +1,4 @@ -import request from '../src/index.js' +import request, { gql } from '../src/index.js' import { setupMockServer } from './__helpers.js' import type { TypedDocumentNode } from '@graphql-typed-document-node/core' import { parse } from 'graphql' @@ -9,7 +9,7 @@ const ctx = setupMockServer() test(`typed-document-node code should TS compile with variables`, async () => { ctx.res({ body: { data: { foo: 1 } } }) - const query: TypedDocumentNode<{ echo: string }, { str: string }> = parse(/* GraphQL */ ` + const query: TypedDocumentNode<{ echo: string }, { str: string }> = parse(gql` query greetings($str: String!) { echo(str: $echo) } @@ -91,7 +91,7 @@ test(`typed-document-node code should TS compile with variables`, async () => { test(`typed-document-node code should TS compile without variables`, async () => { ctx.res({ body: { data: { foo: 1 } } }) - const query: TypedDocumentNode<{ echo: string }> = parse(/* GraphQL */ ` + const query: TypedDocumentNode<{ echo: string }> = parse(gql` query greetings { echo }