Skip to content

Commit

Permalink
chore(docs): make use of gql tag
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Apr 5, 2023
1 parent a446365 commit e596527
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 252 deletions.
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
43 changes: 21 additions & 22 deletions examples/authentication-via-http-header.ts
Original file line number Diff line number Diff line change
@@ -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<TData>(query)
console.log(JSON.stringify(data, undefined, 2))
})().catch((error) => console.error(error))
const data = await graphQLClient.request<TData>(query)
console.log(JSON.stringify(data, undefined, 2))
99 changes: 49 additions & 50 deletions examples/batching-requests.ts
Original file line number Diff line number Diff line change
@@ -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))
43 changes: 21 additions & 22 deletions examples/cookie-support-for-node.ts
Original file line number Diff line number Diff line change
@@ -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<TData>(query)
console.log(JSON.stringify(data, undefined, 2))
})().catch((error) => console.error(error))
const data = await graphQLClient.rawRequest<TData>(query)
console.log(JSON.stringify(data, undefined, 2))
35 changes: 17 additions & 18 deletions examples/custom-fetch.ts
Original file line number Diff line number Diff line change
@@ -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<TData>(query)
console.log(JSON.stringify(data, undefined, 2))
})().catch((error) => console.error(error))
const data = await graphQLClient.rawRequest<TData>(query)
console.log(JSON.stringify(data, undefined, 2))
43 changes: 21 additions & 22 deletions examples/error-handling.ts
Original file line number Diff line number Diff line change
@@ -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<TData>(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<TData>(endpoint, query)
console.log(JSON.stringify(data, undefined, 2))
} catch (error) {
console.error(JSON.stringify(error, undefined, 2))
process.exit(1)
}
Loading

0 comments on commit e596527

Please sign in to comment.