Skip to content

Commit

Permalink
docs: port over examples folder
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Jun 11, 2020
1 parent 33df9d2 commit b89eb77
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 1 deletion.
23 changes: 23 additions & 0 deletions examples/authentication-via-http-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GraphQLClient } from '../esm/index.js'
;(async function () {
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))
})().catch((error) => console.error(error))
26 changes: 26 additions & 0 deletions examples/cookie-support-for-node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
;(global).fetch = require('fetch-cookie/node-fetch')(require('node-fetch'))
const { GraphQLClient } = require('../../cjs/index.js')

;(async function () {
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))
})().catch((error) => console.error(error))
1 change: 1 addition & 0 deletions examples/cookie-support-for-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"commonjs"}
23 changes: 23 additions & 0 deletions examples/error-handling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { request } from '../esm/index.js'
;(async function () {
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)
}
})().catch((error) => console.error(error))
23 changes: 23 additions & 0 deletions examples/passing-more-options-to-fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GraphQLClient } from '../esm/index.js'
;(async function () {
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))
})().catch((error) => console.error(error))
18 changes: 18 additions & 0 deletions examples/receiving-a-raw-response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { rawRequest } from '../esm/index.js'
;(async function () {
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))
})().catch((error) => console.error(error))
22 changes: 22 additions & 0 deletions examples/using-variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { request } from '../esm/index.js'
;(async function () {
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))
})().catch((error) => console.error(error))
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@
"node-fetch": "^2.6.0"
},
"devDependencies": {
"rimraf": "^3.0.2",
"@12core/eslint-config-12core": "^1.0.1",
"ascjs": "^4.0.0",
"auto-changelog": "^2.0.0",
"body-parser": "^1.19.0",
"dependency-check": "^4.1.0",
"eslint": "^7.2.0",
"express": "^4.17.1",
"fetch-cookie": "^0.9.1",
"gh-release": "^3.5.0",
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.2",
"tap": "^14.10.2",
"type-fest": "^0.15.0"
}
Expand Down

0 comments on commit b89eb77

Please sign in to comment.