From b89eb770a8c7ff4c5e32465e057fde162119a52b Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Thu, 11 Jun 2020 13:21:29 -0600 Subject: [PATCH] docs: port over examples folder --- examples/authentication-via-http-header.js | 23 ++++++++++++++++ examples/cookie-support-for-node/index.js | 26 +++++++++++++++++++ examples/cookie-support-for-node/package.json | 1 + examples/error-handling.js | 23 ++++++++++++++++ examples/passing-more-options-to-fetch.js | 23 ++++++++++++++++ examples/receiving-a-raw-response.js | 18 +++++++++++++ examples/using-variables.js | 22 ++++++++++++++++ package.json | 3 ++- 8 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 examples/authentication-via-http-header.js create mode 100644 examples/cookie-support-for-node/index.js create mode 100644 examples/cookie-support-for-node/package.json create mode 100644 examples/error-handling.js create mode 100644 examples/passing-more-options-to-fetch.js create mode 100644 examples/receiving-a-raw-response.js create mode 100644 examples/using-variables.js diff --git a/examples/authentication-via-http-header.js b/examples/authentication-via-http-header.js new file mode 100644 index 0000000..cc433fd --- /dev/null +++ b/examples/authentication-via-http-header.js @@ -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)) diff --git a/examples/cookie-support-for-node/index.js b/examples/cookie-support-for-node/index.js new file mode 100644 index 0000000..b4fa22a --- /dev/null +++ b/examples/cookie-support-for-node/index.js @@ -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)) diff --git a/examples/cookie-support-for-node/package.json b/examples/cookie-support-for-node/package.json new file mode 100644 index 0000000..729ac4d --- /dev/null +++ b/examples/cookie-support-for-node/package.json @@ -0,0 +1 @@ +{"type":"commonjs"} diff --git a/examples/error-handling.js b/examples/error-handling.js new file mode 100644 index 0000000..4ba7f0c --- /dev/null +++ b/examples/error-handling.js @@ -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)) diff --git a/examples/passing-more-options-to-fetch.js b/examples/passing-more-options-to-fetch.js new file mode 100644 index 0000000..4520366 --- /dev/null +++ b/examples/passing-more-options-to-fetch.js @@ -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)) diff --git a/examples/receiving-a-raw-response.js b/examples/receiving-a-raw-response.js new file mode 100644 index 0000000..5b5dd29 --- /dev/null +++ b/examples/receiving-a-raw-response.js @@ -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)) diff --git a/examples/using-variables.js b/examples/using-variables.js new file mode 100644 index 0000000..98e9cbe --- /dev/null +++ b/examples/using-variables.js @@ -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)) diff --git a/package.json b/package.json index 72221f4..e8da774 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "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", @@ -47,8 +46,10 @@ "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" }