-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to configure persisted operations plugin (#6505)
* Add example and tests for persisted queries * add test for file documents * add tests to verify we can still run arbitrary queries * allow to configure persisted queries * add documentation * add persisted operation example to build list of integration test * changeset * Fix changeset version
- Loading branch information
1 parent
12ba8f2
commit ae7b085
Showing
14 changed files
with
338 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@graphql-mesh/config": patch | ||
"@graphql-mesh/types": patch | ||
--- | ||
|
||
Allow to configure persisted operations behaviour |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
sources: | ||
- name: Hello World | ||
handler: | ||
jsonSchema: | ||
operations: | ||
- type: Query | ||
field: greeting | ||
method: GET | ||
path: / | ||
responseSample: | ||
hello: world | ||
plugins: | ||
- mock: | ||
mocks: | ||
- apply: Query.greeting | ||
documents: | ||
# Documents can be specified by filename or as a glob pattern | ||
- ./src/**/*.graphql | ||
# Or by inline definition | ||
- | | ||
query TypeName { | ||
__typename | ||
} | ||
persistedOperations: | ||
allowArbitraryOperations: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "example-persisted-operations", | ||
"version": "0.0.1", | ||
"license": "MIT", | ||
"private": true, | ||
"scripts": { | ||
"build": "mesh build", | ||
"start": "mesh dev", | ||
"test": "mesh build && jest" | ||
}, | ||
"dependencies": { | ||
"@graphql-mesh/cli": "0.88.5", | ||
"@graphql-mesh/json-schema": "0.97.4", | ||
"@graphql-mesh/plugin-mock": "0.96.3", | ||
"@graphql-yoga/plugin-sofa": "3.1.1", | ||
"graphql": "16.8.1" | ||
}, | ||
"devDependencies": { | ||
"jest": "29.7.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"infiniteLoopProtection": true, | ||
"hardReloadOnChange": false, | ||
"template": "node", | ||
"container": { | ||
"node": "21" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
query HelloWorld { | ||
greeting { | ||
hello | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
examples/persisted-operations/tests/persisted-queries.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { join } from 'path'; | ||
import { ExecutionResult } from 'graphql'; | ||
import { findAndParseConfig } from '@graphql-mesh/cli'; | ||
import { createMeshHTTPHandler, MeshHTTPHandler } from '@graphql-mesh/http'; | ||
import { getMesh, MeshInstance } from '@graphql-mesh/runtime'; | ||
|
||
const baseDir = join(__dirname, '..'); | ||
|
||
const meshInstances = { | ||
'Mesh runtime': async () => { | ||
const config = await findAndParseConfig({ dir: baseDir }); | ||
return getMesh(config); | ||
}, | ||
'Mesh artifact': async () => { | ||
const { getBuiltMesh } = await import('../.mesh/index'); | ||
return getBuiltMesh(); | ||
}, | ||
}; | ||
|
||
describe('Persisted Queries', () => { | ||
describe.each(Object.entries(meshInstances))('%s', (_, getMeshInstance) => { | ||
let mesh: MeshInstance; | ||
let meshHttp: MeshHTTPHandler; | ||
|
||
beforeAll(async () => { | ||
mesh = await getMeshInstance(); | ||
meshHttp = createMeshHTTPHandler({ | ||
baseDir, | ||
getBuiltMesh: () => Promise.resolve(mesh), | ||
}); | ||
}); | ||
|
||
afterAll(() => mesh.destroy()); | ||
|
||
it('should give correct response for inline persisted operation', async () => { | ||
const response = await meshHttp.fetch('/graphql', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
extensions: { | ||
persistedQuery: { | ||
version: 1, | ||
sha256Hash: 'ece829f774dcb3e1358987feb1f86832b39472406a3ef65dce6a2a740304148a', | ||
}, | ||
}, | ||
}), | ||
}); | ||
|
||
expect(response.status).toBe(200); | ||
const result = (await response.json()) as ExecutionResult; | ||
expect(result?.errors).toBeFalsy(); | ||
expect(result.data).toEqual({ __typename: 'Query' }); | ||
}); | ||
|
||
it('should give correct response for file documents', async () => { | ||
const response = await meshHttp.fetch('/graphql', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
extensions: { | ||
persistedQuery: { | ||
version: 1, | ||
sha256Hash: '2e0534aab6b2b83bc791439094830b45b621deec2087b8567539f37defd391ac', | ||
}, | ||
}, | ||
}), | ||
}); | ||
|
||
expect(response.status).toBe(200); | ||
const result = (await response.json()) as ExecutionResult; | ||
expect(result?.errors).toBeFalsy(); | ||
expect(result.data).toEqual({ greeting: { hello: 'world' } }); | ||
}); | ||
|
||
it('should not restrict to persisted queries only', async () => { | ||
const response = await meshHttp.fetch('/graphql', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
query: /* GraphQL */ ` | ||
query HelloWorld { | ||
greeting { | ||
__typename | ||
} | ||
} | ||
`, | ||
}), | ||
}); | ||
|
||
expect(response.status).toBe(200); | ||
const result = (await response.json()) as ExecutionResult; | ||
expect(result?.errors).toBeFalsy(); | ||
expect(result.data).toEqual({ greeting: { __typename: 'query_greeting' } }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
"packageManager": "[email protected]", | ||
"scripts": { | ||
"build": "bob build", | ||
"build-test-artifacts": "yarn workspace json-schema-example build && yarn workspace example-fastify build", | ||
"build-test-artifacts": "yarn workspace json-schema-example build && yarn workspace example-fastify build && yarn workspace example-persisted-operations build", | ||
"build:website": "cd website && yarn build", | ||
"ci:lint": "eslint --output-file eslint_report.json --ext .ts --format json \"./packages/**/src/**/*.ts\"", | ||
"clean": "rm -rf packages/**/dist packages/**/**/dist examples/**/node_modules/.bin/*mesh* .bob", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.