From 08d379b8e2b6ed9a5505ede544948863b38d6548 Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 7 Jan 2019 13:03:33 -0800 Subject: [PATCH 1/3] docs(README): update code examples --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f03800d4..7f38fe41 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Send a simple query ```js const graphql = require('@octokit/graphql') -const { data } = await graphql(`{ +const { repository } = await graphql(`{ repository(owner:"octokit", name:"graphql.js") { issues(last:3) { edges { @@ -34,7 +34,7 @@ const { data } = await graphql(`{ ```js const graphql = require('@octokit/graphql') -const { data } = await graphql(`query lastIssues($owner: String!, $repo: String!, $num: Int = 3) { +const { lastIssues } = await graphql(`query lastIssues($owner: String!, $repo: String!, $num: Int = 3) { repository(owner:$owner, name:$repo) { issues(last:$num) { edges { @@ -62,7 +62,7 @@ const graphql = require('@octokit/graphql').defaults({ authorization: `token secret123` } }) -const { data } = await graphql(`{ +const { repository } = await graphql(`{ repository(owner:"octokit", name:"graphql.js") { issues(last:3) { edges { @@ -79,7 +79,7 @@ Pass query together with headers and variables ```js const graphql = require('@octokit/graphql') -const { data } = await graphql({ +const { lastIssues } = await graphql({ query: `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) { repository(owner:$owner, name:$repo) { issues(last:$num) { @@ -108,7 +108,7 @@ const graphql = require('@octokit/graphql').defaults({ authorization: `token secret123` } }) -const { data } = await graphql(`{ +const { repository } = await graphql(`{ repository(owner:"acme-project", name:"acme-repo") { issues(last:3) { edges { @@ -138,7 +138,7 @@ const query = `{ }` try { - const { data } = await graphql(query) + const result = await graphql(query) } catch (error) { // server responds with // { From cd069b89a69ae3099e3285a75929a7fe2b273812 Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 7 Jan 2019 13:04:02 -0800 Subject: [PATCH 2/3] test: remove `{data}` namespace from result of `graphql(query)` --- test/defaults-test.js | 8 ++++---- test/error-test.js | 2 +- test/graphql-test.js | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/defaults-test.js b/test/defaults-test.js index 6be7add0..dc4a17d4 100644 --- a/test/defaults-test.js +++ b/test/defaults-test.js @@ -59,8 +59,8 @@ describe('graphql.defaults()', () => { } } }`) - .then(response => { - expect(response.data).to.deep.equal(mockData) + .then(result => { + expect(result).to.deep.equal(mockData) }) }) @@ -115,8 +115,8 @@ describe('graphql.defaults()', () => { } } }`) - .then(response => { - expect(response.data).to.deep.equal(mockData) + .then(result => { + expect(result).to.deep.equal(mockData) }) }) }) diff --git a/test/error-test.js b/test/error-test.js index 9379dcd9..33138122 100644 --- a/test/error-test.js +++ b/test/error-test.js @@ -41,7 +41,7 @@ describe('errors', () => { } }) - .then(response => { + .then(result => { throw new Error('Should not resolve') }) diff --git a/test/graphql-test.js b/test/graphql-test.js index f01b98be..50112a99 100644 --- a/test/graphql-test.js +++ b/test/graphql-test.js @@ -69,8 +69,8 @@ describe('graphql()', () => { } }) - .then(response => { - expect(response.data).to.deep.equal(mockData) + .then(result => { + expect(result).to.deep.equal(mockData) }) }) From a7710b50b8831cc32e9cf8c12389586beff4978f Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 7 Jan 2019 13:06:08 -0800 Subject: [PATCH 3/3] remove `{data}` namespace from result of `graphql(query)` BREAKING CHANGE: The `{data}` namespace from the graphql query result has been removed. The resulting data is now returned directly. Before ```js const {data: {viewer}} = await graphql(`{ viewer { bioHtml } }`) ``` After ```js const {viewer} = await graphql(`{ viewer { bioHtml } }`) ``` --- lib/error.js | 1 + lib/graphql.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/error.js b/lib/error.js index f1fc4921..4478abd2 100644 --- a/lib/error.js +++ b/lib/error.js @@ -4,6 +4,7 @@ module.exports = class GraphqlError extends Error { super(message) Object.assign(this, response.data) + this.name = 'GraphqlError' this.request = request // Maintains proper stack trace (only available on V8) diff --git a/lib/graphql.js b/lib/graphql.js index cf985a58..637dcab8 100644 --- a/lib/graphql.js +++ b/lib/graphql.js @@ -27,7 +27,7 @@ function graphql (request, query, options) { return request(requestOptions) .then(response => { if (response.data.data) { - return response.data + return response.data.data } throw new GraphqlError(requestOptions, response)