Skip to content

Commit

Permalink
fix implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Aug 16, 2022
1 parent 503ab48 commit 3acd310
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 112 deletions.
2 changes: 1 addition & 1 deletion examples/apollo-federation/gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"@apollo/gateway": "^0.52.0",
"@envelop/apollo-federation": "^2.4.0",
"@envelop/apollo-federation": "^2.5.0",
"graphql-yoga": "2.13.4",
"graphql": "^16.5.0"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/fastify-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"check": "tsc --pretty --noEmit"
},
"dependencies": {
"@envelop/graphql-modules": "3.4.2",
"@envelop/graphql-modules": "3.5.0",
"@graphql-tools/load-files": "6.6.1",
"graphql-yoga": "2.13.4",
"fastify": "4.4.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/generic-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"typescript": "4.7.4"
},
"dependencies": {
"@envelop/generic-auth": "4.3.2",
"@envelop/generic-auth": "4.4.0",
"graphql-yoga": "2.13.4",
"graphql": "16.5.0"
}
Expand Down
10 changes: 5 additions & 5 deletions packages/graphql-yoga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -72,8 +72,8 @@
"devDependencies": {
"@types/node": "16.11.47",
"html-minifier-terser": "7.0.0-beta.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",
Expand Down
66 changes: 41 additions & 25 deletions packages/plugins/apq/__tests__/automatic-persisted-queries.spec.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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: [
Expand All @@ -22,23 +20,27 @@ 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,
sha256Hash:
'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: [
Expand All @@ -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: [
Expand All @@ -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')
})
Expand All @@ -110,19 +121,24 @@ 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: {
version: 1,
sha256Hash: 'leoeoel',
},
},
})
}),
})

expect(response.status).toEqual(200)
expect(JSON.parse(response.text)).toEqual({
expect(JSON.parse(await response.text())).toEqual({
data: null,
errors: [{ message: 'PersistedQueryMismatch' }],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createYoga, createSchema } from 'graphql-yoga'
import request from 'supertest'
import { usePersistedOperations } from '@graphql-yoga/plugin-persisted-operations'

const schema = createSchema({
Expand All @@ -11,7 +10,7 @@ const schema = createSchema({
})

describe('Automatic Persisted Queries', () => {
it('should return not found error if persisted query is missing', async () => {
it('returns not found error for missing persisted query', async () => {
const store = new Map<string, string>()
const yoga = createYoga({
plugins: [
Expand All @@ -21,23 +20,29 @@ 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,
sha256Hash:
'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<string, string>()
const yoga = createYoga({
plugins: [
Expand All @@ -47,25 +52,32 @@ describe('Automatic Persisted Queries', () => {
],
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<string, string>()

const yoga = createYoga({
Expand All @@ -83,15 +95,21 @@ describe('Automatic Persisted Queries', () => {
}
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<string, string>()

const yoga = createYoga({
Expand All @@ -110,15 +128,21 @@ describe('Automatic Persisted Queries', () => {
}
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<string, string>()

const yoga = createYoga({
Expand All @@ -138,14 +162,18 @@ describe('Automatic Persisted Queries', () => {
}
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' })
})
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/response-cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit 3acd310

Please sign in to comment.