From 1c6de856488dba7023630387d50af7072695dfc9 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Tue, 16 Aug 2022 10:25:59 +0200 Subject: [PATCH 1/6] test: add failing test --- .../__tests__/error-masking.spec.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/packages/graphql-yoga/__tests__/error-masking.spec.ts b/packages/graphql-yoga/__tests__/error-masking.spec.ts index 7acfdf86d2..28d4328832 100644 --- a/packages/graphql-yoga/__tests__/error-masking.spec.ts +++ b/packages/graphql-yoga/__tests__/error-masking.spec.ts @@ -367,4 +367,47 @@ describe('error masking', () => { } `) }) + + it('error thrown within context factory is exposed via originalError extension field in dev mode', async () => { + const yoga = createYoga({ + logging: false, + context: () => { + throw new Error('I am the original error.') + }, + maskedErrors: { + isDev: true, + }, + schema: createSchema({ + typeDefs: /* GraphQL */ ` + type Query { + a: String! + } + `, + }), + }) + const response = await yoga.fetch('http://yoga/graphql', { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ query: '{a}' }), + }) + + const body = JSON.parse(await response.text()) + expect(body).toStrictEqual({ + data: null, + errors: [ + { + message: 'Unexpected error.', + extensions: { + originalError: { + message: 'I am the original error.', + stack: expect.stringContaining('Error: I am the original error.'), + }, + }, + }, + ], + }) + expect(response.status).toEqual(200) + }) }) From c6dcc0f67efac391deecb107fab1de415efc0242 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Tue, 16 Aug 2022 11:44:17 +0200 Subject: [PATCH 2/6] fix implementation --- packages/graphql-yoga/package.json | 10 +-- packages/plugins/apq/__tests__/apq.spec.ts | 76 +++++++++------- .../__tests__/persisted-operations.spec.ts | 86 ++++++++++++------- packages/plugins/response-cache/package.json | 2 +- yarn.lock | 34 ++++---- 5 files changed, 123 insertions(+), 85 deletions(-) diff --git a/packages/graphql-yoga/package.json b/packages/graphql-yoga/package.json index 3f835499ec..68c4349e2c 100644 --- a/packages/graphql-yoga/package.json +++ b/packages/graphql-yoga/package.json @@ -55,9 +55,9 @@ "dependencies": { "@graphql-tools/code-file-loader": "^7.3.0", "@graphql-tools/mock": "^8.7.0", - "@envelop/core": "^2.4.1", - "@envelop/parser-cache": "^4.4.0", - "@envelop/validation-cache": "^4.4.0", + "@envelop/core": "^2.5.0", + "@envelop/parser-cache": "^4.6.0", + "@envelop/validation-cache": "^4.6.0", "@graphql-typed-document-node/core": "^3.1.1", "@graphql-tools/schema": "^9.0.0", "@graphql-tools/utils": "^8.8.0", @@ -72,8 +72,8 @@ "devDependencies": { "@types/node": "16.11.56", "html-minifier-terser": "7.0.0", - "@envelop/disable-introspection": "^3.4.0", - "@envelop/live-query": "^4.0.0", + "@envelop/disable-introspection": "^3.5.0", + "@envelop/live-query": "^4.1.0", "@types/eventsource": "1.1.9", "eventsource": "2.0.2", "graphql": "^16.0.1", diff --git a/packages/plugins/apq/__tests__/apq.spec.ts b/packages/plugins/apq/__tests__/apq.spec.ts index 639c601f7e..d18938b3c6 100644 --- a/packages/plugins/apq/__tests__/apq.spec.ts +++ b/packages/plugins/apq/__tests__/apq.spec.ts @@ -1,6 +1,4 @@ import { createYoga, createSchema } from 'graphql-yoga' - -import request from 'supertest' import { createInMemoryAPQStore, useAPQ } from '@graphql-yoga/plugin-apq' const schema = createSchema({ @@ -12,7 +10,7 @@ const schema = createSchema({ }) describe('Automatic Persisted Queries', () => { - it('should return not found error if persisted query is missing', async () => { + it('sends not found error to client if persisted query is missing', async () => { const store = createInMemoryAPQStore() const yoga = createYoga({ plugins: [ @@ -22,9 +20,12 @@ describe('Automatic Persisted Queries', () => { ], schema, }) - const response = await request(yoga) - .post('/graphql') - .send({ + const response = await yoga.fetch('http://yoga/graphql', { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ extensions: { persistedQuery: { version: 1, @@ -32,13 +33,14 @@ describe('Automatic Persisted Queries', () => { 'ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38', }, }, - }) + }), + }) - const body = JSON.parse(response.text) + const body = JSON.parse(await response.text()) expect(body.errors).toBeDefined() expect(body.errors[0].message).toBe('PersistedQueryNotFound') }) - it('should load the persisted query when stored', async () => { + it('uses a stored persisted query', async () => { const store = createInMemoryAPQStore() const yoga = createYoga({ plugins: [ @@ -54,19 +56,23 @@ describe('Automatic Persisted Queries', () => { 'ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38', } store.set(persistedQueryEntry.sha256Hash, '{__typename}') - const response = await request(yoga) - .post('/graphql') - .send({ + const response = await yoga.fetch('http://yoga/graphql', { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ extensions: { persistedQuery: persistedQueryEntry, }, - }) + }), + }) - const body = JSON.parse(response.text) + const body = JSON.parse(await response.text()) expect(body.errors).toBeUndefined() expect(body.data.__typename).toBe('Query') }) - it('should save the persisted query', async () => { + it('saves a persisted query', async () => { const store = createInMemoryAPQStore() const yoga = createYoga({ plugins: [ @@ -83,19 +89,24 @@ describe('Automatic Persisted Queries', () => { 'ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38', } const query = `{__typename}` - const response = await request(yoga) - .post('/graphql') - .send({ + + const response = await yoga.fetch('http://yoga/graphql', { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ query, extensions: { persistedQuery: persistedQueryEntry, }, - }) + }), + }) const entry = store.get(persistedQueryEntry.sha256Hash) expect(entry).toBe(query) - const body = JSON.parse(response.text) + const body = JSON.parse(await response.text()) expect(body.errors).toBeUndefined() expect(body.data.__typename).toBe('Query') }) @@ -110,9 +121,12 @@ describe('Automatic Persisted Queries', () => { schema, }) const query = `{__typename}` - const response = await request(yoga) - .post('/graphql') - .send({ + const response = await yoga.fetch('http://yoga/graphql', { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ query, extensions: { persistedQuery: { @@ -120,16 +134,12 @@ describe('Automatic Persisted Queries', () => { sha256Hash: 'leoeoel', }, }, - }) + }), + }) + expect(response.status).toEqual(500) - expect(JSON.parse(response.text)).toMatchInlineSnapshot(` - Object { - "errors": Array [ - Object { - "message": "PersistedQueryMismatch", - }, - ], - } - `) + expect(JSON.parse(await response.text())).toEqual({ + errors: [{ message: 'PersistedQueryMismatch' }], + }) }) }) diff --git a/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts b/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts index 07c9f4e698..8e3000de07 100644 --- a/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts +++ b/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts @@ -1,5 +1,4 @@ -import { createYoga, createSchema, GraphQLParams } from 'graphql-yoga' -import request from 'supertest' +import { createYoga, createSchema } from 'graphql-yoga' import { usePersistedOperations } from '@graphql-yoga/plugin-persisted-operations' const schema = createSchema({ @@ -11,7 +10,7 @@ const schema = createSchema({ }) describe('Persisted Operations', () => { - it('should return not found error if persisted query is missing', async () => { + it('returns not found error for missing persisted query', async () => { const yoga = createYoga({ plugins: [ usePersistedOperations({ @@ -22,9 +21,13 @@ describe('Persisted Operations', () => { ], schema, }) - const response = await request(yoga) - .post('/graphql') - .send({ + + const response = await yoga.fetch('http://yoga/graphql', { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ extensions: { persistedQuery: { version: 1, @@ -32,13 +35,15 @@ describe('Persisted Operations', () => { 'ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38', }, }, - }) + }), + }) - const body = JSON.parse(response.text) + const body = JSON.parse(await response.text()) expect(body.errors).toBeDefined() expect(body.errors[0].message).toBe('PersistedQueryNotFound') }) - it('should load the persisted query when stored', async () => { + + it('uses a persisted query from the store', async () => { const store = new Map() const yoga = createYoga({ plugins: [ @@ -50,25 +55,32 @@ describe('Persisted Operations', () => { ], schema, }) + const persistedQueryEntry = { version: 1, sha256Hash: 'ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38', } + store.set(persistedQueryEntry.sha256Hash, '{__typename}') - const response = await request(yoga) - .post('/graphql') - .send({ + const response = await yoga.fetch('http://yoga/graphql', { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ extensions: { persistedQuery: persistedQueryEntry, }, - }) + }), + }) - const body = JSON.parse(response.text) + const body = JSON.parse(await response.text()) expect(body.errors).toBeUndefined() expect(body.data.__typename).toBe('Query') }) - it('should reject non-persisted operations', async () => { + + it('rejects non-persisted operations', async () => { const store = new Map() const yoga = createYoga({ @@ -88,15 +100,21 @@ describe('Persisted Operations', () => { } store.set(persistedQueryEntry.sha256Hash, '{__typename}') - const response = await request(yoga).post('/graphql').send({ - query: '{__typename}', + const response = await yoga.fetch('http://yoga/graphql', { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ + query: '{__typename}', + }), }) - const body = JSON.parse(response.text) + const body = JSON.parse(await response.text()) expect(body.errors).toBeDefined() expect(body.errors[0].message).toBe('PersistedQueryOnly') }) - it('should allow non-persisted operations via allowArbitraryOperations flag', async () => { + it('allows non-persisted operations via allowArbitraryOperations flag', async () => { const store = new Map() const yoga = createYoga({ @@ -117,15 +135,21 @@ describe('Persisted Operations', () => { } store.set(persistedQueryEntry.sha256Hash, '{__typename}') - const response = await request(yoga).post('/graphql').send({ - query: '{__typename}', + const response = await yoga.fetch('http://yoga/graphql', { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ + query: '{__typename}', + }), }) - const body = JSON.parse(response.text) + const body = JSON.parse(await response.text()) expect(body.errors).toBeUndefined() expect(body.data).toEqual({ __typename: 'Query' }) }) - it('should allow non-persisted operations via allowArbitraryOperations based on a header', async () => { + it('allows non-persisted operations via allowArbitraryOperations based on a header', async () => { const store = new Map() const yoga = createYoga({ @@ -147,14 +171,18 @@ describe('Persisted Operations', () => { } store.set(persistedQueryEntry.sha256Hash, '{__typename}') - const response = await request(yoga) - .post('/graphql') - .set('foo', 'bar') - .send({ + const response = await yoga.fetch('http://yoga/graphql', { + method: 'POST', + headers: { + 'content-type': 'application/json', + foo: 'bar', + }, + body: JSON.stringify({ query: '{__typename}', - }) + }), + }) - const body = JSON.parse(response.text) + const body = JSON.parse(await response.text()) expect(body.errors).toBeUndefined() expect(body.data).toEqual({ __typename: 'Query' }) }) diff --git a/packages/plugins/response-cache/package.json b/packages/plugins/response-cache/package.json index bc406fdc1b..650d0be28b 100644 --- a/packages/plugins/response-cache/package.json +++ b/packages/plugins/response-cache/package.json @@ -49,7 +49,7 @@ "access": "public" }, "dependencies": { - "@envelop/response-cache": "^3.0.0" + "@envelop/response-cache": "^3.1.0" }, "peerDependencies": { "graphql": "^15.2.0 || ^16.0.0", diff --git a/yarn.lock b/yarn.lock index 3042c7c000..c57fa4a5c6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2234,14 +2234,14 @@ apollo-server-caching "^3.1.0" apollo-server-types "^3.2.0" -"@envelop/core@^2.4.1": - version "2.4.1" - resolved "https://registry.yarnpkg.com/@envelop/core/-/core-2.4.1.tgz#d1f6c62867415a9fb93fd598c6263397a730c340" - integrity sha512-27vOytvJSmKo8ny90coegQ9/Js8zaJtQ570vWGEqYbT7losSqJzgyg8tkNAz1WeJoJh3B4Nw4KiuAG1h2jeH3A== +"@envelop/core@^2.5.0": + version "2.5.0" + resolved "https://registry.yarnpkg.com/@envelop/core/-/core-2.5.0.tgz#f37ba50ff2e997bcb310b366eacf40b2a2107ee0" + integrity sha512-nlDC9q75bjvS/ajbkkVlwGPSYlWhZOQ6StxMTEjvUVefL4o49NpMlGgxfN2mJ64y1CJ3MI/bIemZ3jOHmiv3Og== dependencies: - "@envelop/types" "2.3.0" + "@envelop/types" "2.3.1" -"@envelop/disable-introspection@3.5.0", "@envelop/disable-introspection@^3.4.0": +"@envelop/disable-introspection@3.5.0", "@envelop/disable-introspection@^3.5.0": version "3.5.0" resolved "https://registry.yarnpkg.com/@envelop/disable-introspection/-/disable-introspection-3.5.0.tgz#2db05bab8da24ec097bf6dac707d3f0790ff2e7f" integrity sha512-zBG4eJoCdJlkeQNdQuZl9hAlst1lDQrOaClDiCLnrxze/aStXVDFdn2+xV1L2q0AKdOW4O6NRECPfBXsqWnbOA== @@ -2273,7 +2273,7 @@ resolved "https://registry.yarnpkg.com/@envelop/graphql-modules/-/graphql-modules-3.5.0.tgz#72087d1db74c9b811e0106efb778e3e772aceec3" integrity sha512-Le3ExHvzXPmfqu2e4f8Q/rkXUppO1TK6aNo18OZwydb91MaRVV+oAEkvArE5EErHilfsZM3adzwoG0OjQm/N/g== -"@envelop/live-query@4.1.0", "@envelop/live-query@^4.0.0": +"@envelop/live-query@4.1.0", "@envelop/live-query@^4.1.0": version "4.1.0" resolved "https://registry.yarnpkg.com/@envelop/live-query/-/live-query-4.1.0.tgz#ea090ce70edf2035c0b25ba1bc80340852e9a96b" integrity sha512-uj99MEdysjd3+H3BtcjYCeqQHDm/SVxTCsYGu5Gee1iW0BYGe4L7f6ZqCTh0GK+CxA0T28JNfvS3svPEiTb3FQ== @@ -2283,28 +2283,28 @@ "@n1ru4l/graphql-live-query-patch" "^0.7.0" "@n1ru4l/in-memory-live-query-store" "^0.10.0" -"@envelop/parser-cache@^4.4.0": +"@envelop/parser-cache@^4.6.0": version "4.6.0" resolved "https://registry.yarnpkg.com/@envelop/parser-cache/-/parser-cache-4.6.0.tgz#3ff71acdfbc51097d0dd8051cb961f856cddc400" integrity sha512-Oi3nX76tk5L7K6MdpPr4AjtpMW1XoyISeiaodYD8WxUWY7JzOA7qetuYguUZv/lK5VaLMsJuoWAwxbu1JKEe9A== dependencies: lru-cache "^6.0.0" -"@envelop/response-cache@^3.0.0": - version "3.0.0" - resolved "https://registry.yarnpkg.com/@envelop/response-cache/-/response-cache-3.0.0.tgz#fd53523e00c66bd221c2e1200f6eb00b7529c206" - integrity sha512-sGlX5noloUqUG5oVfckbuXTuOaFsYLEqz5zTgrTE47eTE+j+s1mooKecr0fyr3M83DHf/plF0/xilOtHsrJxyQ== +"@envelop/response-cache@^3.1.0": + version "3.1.0" + resolved "https://registry.yarnpkg.com/@envelop/response-cache/-/response-cache-3.1.0.tgz#67e3922a6c4ebf836be0e63f845c50ed5b2d4fc9" + integrity sha512-vgdAbxJ5K4YDx5d+fmvRLy8Xg/z9/SFnpdLRSulBEZe+ltdpwjNTBAJk7huBz3a5mD2AhURMlMA91UXF/kHAZQ== dependencies: "@graphql-tools/utils" "^8.8.0" fast-json-stable-stringify "^2.1.0" lru-cache "^6.0.0" -"@envelop/types@2.3.0": - version "2.3.0" - resolved "https://registry.yarnpkg.com/@envelop/types/-/types-2.3.0.tgz#d633052eb3c7e7913380165ce041e2c0e358d5b6" - integrity sha512-K20KxpGlY+HKenms4Kccuh/YcCm0ytj1Zk0Ak6b6hKA68JI4YQ2GwGMq7A7gSOb1MxlbKqrnVdeQdXaDpQfCmA== +"@envelop/types@2.3.1": + version "2.3.1" + resolved "https://registry.yarnpkg.com/@envelop/types/-/types-2.3.1.tgz#b857b3fa5d4df7fadd53ef0ee17cf70b54e827b9" + integrity sha512-c5VLCVVRJ2R9LpDHg/N2BO2l4veaJhklquW+FX8GfzXU79DPWe8WmX4MbM6ABUZmSLOJkYInifHrnlqAoucxpQ== -"@envelop/validation-cache@^4.4.0": +"@envelop/validation-cache@^4.6.0": version "4.6.0" resolved "https://registry.yarnpkg.com/@envelop/validation-cache/-/validation-cache-4.6.0.tgz#c4ad678cffbbce05a6fbdd039e8cad20e01a2bbc" integrity sha512-Xn5u/tQHid6GzWDenCJkIn5GsDm2fUCNnAudN1BGjXcRvAEFfTHuchpp1PJxvRAqGdYjznng+NkOcqrP5brQrw== From 727772235ba02593e63698d5cce40ea6ab36b3e2 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Tue, 6 Sep 2022 15:31:05 +0300 Subject: [PATCH 3/6] Rebase --- .../graphql-yoga/__tests__/error-masking.spec.ts | 3 +-- .../__tests__/persisted-operations.spec.ts | 13 ++++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/graphql-yoga/__tests__/error-masking.spec.ts b/packages/graphql-yoga/__tests__/error-masking.spec.ts index 28d4328832..a1062bc19b 100644 --- a/packages/graphql-yoga/__tests__/error-masking.spec.ts +++ b/packages/graphql-yoga/__tests__/error-masking.spec.ts @@ -395,7 +395,6 @@ describe('error masking', () => { const body = JSON.parse(await response.text()) expect(body).toStrictEqual({ - data: null, errors: [ { message: 'Unexpected error.', @@ -408,6 +407,6 @@ describe('error masking', () => { }, ], }) - expect(response.status).toEqual(200) + expect(response.status).toEqual(500) }) }) diff --git a/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts b/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts index 8e3000de07..c6125426b6 100644 --- a/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts +++ b/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts @@ -205,11 +205,18 @@ describe('Persisted Operations', () => { }) const persistedOperationKey = 'my-persisted-operation' store.set(persistedOperationKey, '{__typename}') - const response = await request(yoga).post('/graphql').send({ - doc_id: persistedOperationKey, + const response = await yoga.fetch('http://yoga/graphql', { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ + doc_id: persistedOperationKey, + }), }) - const body = JSON.parse(response.text) + const body = await response.json() + expect(body.errors).toBeUndefined() expect(body.data.__typename).toBe('Query') }) From b8fe823bc7ccc64f86ed88ed9dd30eb9f237fbc2 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Tue, 6 Sep 2022 15:33:01 +0300 Subject: [PATCH 4/6] use response.json --- .../__integration-tests__/esm.spec.mjs | 2 +- .../__integration-tests__/node-http.spec.ts | 2 +- .../__tests__/error-masking.spec.ts | 26 ++++++++--------- .../__tests__/http-extensions.spec.ts | 2 +- .../__tests__/introspection.spec.ts | 2 +- .../graphql-yoga/__tests__/requests.spec.ts | 28 +++++++++---------- packages/plugins/apq/__tests__/apq.spec.ts | 8 +++--- .../__tests__/persisted-operations.spec.ts | 10 +++---- 8 files changed, 40 insertions(+), 40 deletions(-) diff --git a/examples/node-esm/__integration-tests__/esm.spec.mjs b/examples/node-esm/__integration-tests__/esm.spec.mjs index ada7caf065..556533cc77 100644 --- a/examples/node-esm/__integration-tests__/esm.spec.mjs +++ b/examples/node-esm/__integration-tests__/esm.spec.mjs @@ -12,7 +12,7 @@ describe('Node ESM', () => { }), }) expect(response.status).toBe(200) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.data.greetings).toBe('Hello world!') }) }) diff --git a/packages/graphql-yoga/__integration-tests__/node-http.spec.ts b/packages/graphql-yoga/__integration-tests__/node-http.spec.ts index ff0bccfa47..0a19f5dac0 100644 --- a/packages/graphql-yoga/__integration-tests__/node-http.spec.ts +++ b/packages/graphql-yoga/__integration-tests__/node-http.spec.ts @@ -31,7 +31,7 @@ it('should expose Node req and res objects in the context', async () => { `http://localhost:${port}/graphql?query=query{isNode}`, ) expect(response.status).toBe(200) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data.isNode).toBe(true) } finally { diff --git a/packages/graphql-yoga/__tests__/error-masking.spec.ts b/packages/graphql-yoga/__tests__/error-masking.spec.ts index a1062bc19b..0a5835519a 100644 --- a/packages/graphql-yoga/__tests__/error-masking.spec.ts +++ b/packages/graphql-yoga/__tests__/error-masking.spec.ts @@ -38,7 +38,7 @@ describe('error masking', () => { body: JSON.stringify({ query: '{ hi hello }' }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.data.hi).toBeNull() expect(body.errors![0].message).toBe('Unexpected error.') expect(body.data.hello).toBeNull() @@ -57,7 +57,7 @@ describe('error masking', () => { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ query: '{ hi hello }' }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.data.hi).toBeNull() expect(body.errors![0].message).toBe('Hahahaha') @@ -77,7 +77,7 @@ describe('error masking', () => { body: JSON.stringify({ query: '{ hi hello }' }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.data.hi).toBeNull() expect(body.errors![0].message).toBe('Unexpected error.') expect(body.data.hello).toBeNull() @@ -99,7 +99,7 @@ describe('error masking', () => { body: JSON.stringify({ query: '{ hi hello }' }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.data.hi).toBeNull() expect(body.errors?.[0]?.message).toBe('Unexpected error.') expect(body.errors?.[0]?.extensions).toStrictEqual({ @@ -129,7 +129,7 @@ describe('error masking', () => { body: JSON.stringify({ query: '{ hi hello }' }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.data.hi).toBeNull() expect(body.errors?.[0]?.message).toBe('Unexpected error.') expect(body.errors?.[0]?.extensions).toStrictEqual({ @@ -165,7 +165,7 @@ describe('error masking', () => { expect(response.status).toBe(500) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors?.[0]?.message).toBe('Unexpected error.') }) @@ -184,7 +184,7 @@ describe('error masking', () => { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ query: '{ __typename }' }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body).toMatchInlineSnapshot(` Object { "errors": Array [ @@ -210,7 +210,7 @@ describe('error masking', () => { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ query: '{ __typename }' }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body).toMatchInlineSnapshot(` Object { "errors": Array [ @@ -236,7 +236,7 @@ describe('error masking', () => { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ query: '{ __typename }' }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body).toMatchInlineSnapshot(` Object { "errors": Array [ @@ -272,7 +272,7 @@ describe('error masking', () => { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ query: '{ greetings }' }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body).toMatchInlineSnapshot(` Object { "errors": Array [ @@ -309,7 +309,7 @@ describe('error masking', () => { }) expect(response.status).toEqual(400) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body).toMatchInlineSnapshot(` Object { @@ -349,7 +349,7 @@ describe('error masking', () => { }) expect(response.status).toEqual(400) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body).toMatchInlineSnapshot(` Object { @@ -393,7 +393,7 @@ describe('error masking', () => { body: JSON.stringify({ query: '{a}' }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body).toStrictEqual({ errors: [ { diff --git a/packages/graphql-yoga/__tests__/http-extensions.spec.ts b/packages/graphql-yoga/__tests__/http-extensions.spec.ts index e9dcdce971..f80b8fdcca 100644 --- a/packages/graphql-yoga/__tests__/http-extensions.spec.ts +++ b/packages/graphql-yoga/__tests__/http-extensions.spec.ts @@ -82,7 +82,7 @@ describe('GraphQLError.extensions.http', () => { expect(response.status).toBe(503) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body).toMatchObject({ data: { a: null, diff --git a/packages/graphql-yoga/__tests__/introspection.spec.ts b/packages/graphql-yoga/__tests__/introspection.spec.ts index 038fcc88a8..040cd3ed0e 100644 --- a/packages/graphql-yoga/__tests__/introspection.spec.ts +++ b/packages/graphql-yoga/__tests__/introspection.spec.ts @@ -33,7 +33,7 @@ describe('introspection', () => { }) expect(response.status).toBe(200) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data?.__schema.queryType.name).toBe('Query') }) diff --git a/packages/graphql-yoga/__tests__/requests.spec.ts b/packages/graphql-yoga/__tests__/requests.spec.ts index 8cada4df9d..205fd41ac4 100644 --- a/packages/graphql-yoga/__tests__/requests.spec.ts +++ b/packages/graphql-yoga/__tests__/requests.spec.ts @@ -43,7 +43,7 @@ describe('requests', () => { }) expect(response.status).toBe(200) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data.ping).toBe('pong') }) @@ -57,7 +57,7 @@ describe('requests', () => { ) expect(response.status).toBe(200) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data.ping).toBe('pong') }) @@ -75,7 +75,7 @@ describe('requests', () => { expect(response.status).toBe(405) expect(response.headers.get('allow')).toEqual('POST') - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.data).toBeUndefined() expect(body.errors).toHaveLength(1) @@ -98,7 +98,7 @@ describe('requests', () => { }) expect(response.status).toBe(200) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data.echo).toBe(null) }) @@ -118,7 +118,7 @@ describe('requests', () => { }) expect(response.status).toBe(200) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data.echo).toBe('hello') }) @@ -131,7 +131,7 @@ describe('requests', () => { }) expect(response.status).toBe(400) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeDefined() expect(body.data).toBeUndefined() @@ -145,7 +145,7 @@ describe('requests', () => { }) expect(response.status).toBe(400) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeDefined() expect(body.errors[0].message).toEqual('POST body sent invalid JSON.') @@ -161,7 +161,7 @@ describe('requests', () => { expect(response.status).toBe(400) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeDefined() expect(body.data).toBeUndefined() @@ -176,7 +176,7 @@ describe('requests', () => { expect(response.status).toBe(400) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.data).toBeUndefined() expect(body.errors?.[0].message).toBe('Must provide query string.') }) @@ -190,7 +190,7 @@ describe('requests', () => { expect(response.status).toBe(400) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.data).toBeUndefined() expect(body.errors?.[0].message).toBe( 'Expected "query" param to be a string, but given object.', @@ -224,7 +224,7 @@ describe('requests', () => { }) expect(response.status).toBe(200) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data.ping).toBe('pong') }) @@ -239,7 +239,7 @@ describe('requests', () => { }) expect(response.status).toBe(200) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data.ping).toBe('pong') }) @@ -254,7 +254,7 @@ describe('requests', () => { }) expect(response.status).toBe(200) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data.ping).toBe('pong') }) @@ -269,7 +269,7 @@ describe('requests', () => { }) expect(response.status).toBe(400) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.data).toBeUndefined() expect(body.errors?.[0].message).toBe( 'Unexpected parameter "test" in the request body.', diff --git a/packages/plugins/apq/__tests__/apq.spec.ts b/packages/plugins/apq/__tests__/apq.spec.ts index d18938b3c6..bca8c7ed8e 100644 --- a/packages/plugins/apq/__tests__/apq.spec.ts +++ b/packages/plugins/apq/__tests__/apq.spec.ts @@ -36,7 +36,7 @@ describe('Automatic Persisted Queries', () => { }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeDefined() expect(body.errors[0].message).toBe('PersistedQueryNotFound') }) @@ -68,7 +68,7 @@ describe('Automatic Persisted Queries', () => { }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data.__typename).toBe('Query') }) @@ -106,7 +106,7 @@ describe('Automatic Persisted Queries', () => { const entry = store.get(persistedQueryEntry.sha256Hash) expect(entry).toBe(query) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data.__typename).toBe('Query') }) @@ -138,7 +138,7 @@ describe('Automatic Persisted Queries', () => { }) expect(response.status).toEqual(500) - expect(JSON.parse(await response.text())).toEqual({ + expect(await response.json()).toEqual({ errors: [{ message: 'PersistedQueryMismatch' }], }) }) diff --git a/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts b/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts index c6125426b6..b9a85856ba 100644 --- a/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts +++ b/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts @@ -38,7 +38,7 @@ describe('Persisted Operations', () => { }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeDefined() expect(body.errors[0].message).toBe('PersistedQueryNotFound') }) @@ -75,7 +75,7 @@ describe('Persisted Operations', () => { }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data.__typename).toBe('Query') }) @@ -110,7 +110,7 @@ describe('Persisted Operations', () => { }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeDefined() expect(body.errors[0].message).toBe('PersistedQueryOnly') }) @@ -145,7 +145,7 @@ describe('Persisted Operations', () => { }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data).toEqual({ __typename: 'Query' }) }) @@ -182,7 +182,7 @@ describe('Persisted Operations', () => { }), }) - const body = JSON.parse(await response.text()) + const body = await response.json() expect(body.errors).toBeUndefined() expect(body.data).toEqual({ __typename: 'Query' }) }) From 147d3c5c5f2950c82efa9714ae0816e16ecab7ad Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Tue, 6 Sep 2022 15:35:40 +0300 Subject: [PATCH 5/6] Satisfy TS --- .../persisted-operations/__tests__/persisted-operations.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts b/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts index b9a85856ba..1584308e59 100644 --- a/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts +++ b/packages/plugins/persisted-operations/__tests__/persisted-operations.spec.ts @@ -1,4 +1,4 @@ -import { createYoga, createSchema } from 'graphql-yoga' +import { createYoga, createSchema, GraphQLParams } from 'graphql-yoga' import { usePersistedOperations } from '@graphql-yoga/plugin-persisted-operations' const schema = createSchema({ From 913c879727b500a087b215a6f6d5c97d79468049 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 6 Sep 2022 12:36:11 +0000 Subject: [PATCH 6/6] chore(dependencies): updated changesets for modified dependencies --- ...aphql-yoga_plugin-response-cache-1604-dependencies.md | 7 +++++++ .changeset/graphql-yoga-1604-dependencies.md | 9 +++++++++ 2 files changed, 16 insertions(+) create mode 100644 .changeset/@graphql-yoga_plugin-response-cache-1604-dependencies.md create mode 100644 .changeset/graphql-yoga-1604-dependencies.md diff --git a/.changeset/@graphql-yoga_plugin-response-cache-1604-dependencies.md b/.changeset/@graphql-yoga_plugin-response-cache-1604-dependencies.md new file mode 100644 index 0000000000..443b73562b --- /dev/null +++ b/.changeset/@graphql-yoga_plugin-response-cache-1604-dependencies.md @@ -0,0 +1,7 @@ +--- +"@graphql-yoga/plugin-response-cache": patch +--- + +dependencies updates: + +- Updated dependency [`@envelop/response-cache@^3.1.0` ↗︎](https://www.npmjs.com/package/@envelop/response-cache/v/null) (from `^3.0.0`, in `dependencies`) diff --git a/.changeset/graphql-yoga-1604-dependencies.md b/.changeset/graphql-yoga-1604-dependencies.md new file mode 100644 index 0000000000..93244885dd --- /dev/null +++ b/.changeset/graphql-yoga-1604-dependencies.md @@ -0,0 +1,9 @@ +--- +"graphql-yoga": patch +--- + +dependencies updates: + +- Updated dependency [`@envelop/core@^2.5.0` ↗︎](https://www.npmjs.com/package/@envelop/core/v/null) (from `^2.4.1`, in `dependencies`) +- Updated dependency [`@envelop/parser-cache@^4.6.0` ↗︎](https://www.npmjs.com/package/@envelop/parser-cache/v/null) (from `^4.4.0`, in `dependencies`) +- Updated dependency [`@envelop/validation-cache@^4.6.0` ↗︎](https://www.npmjs.com/package/@envelop/validation-cache/v/null) (from `^4.4.0`, in `dependencies`)