-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
424 additions
and
85 deletions.
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,5 @@ | ||
--- | ||
'@graphql-yoga/plugin-persisted-queries': major | ||
--- | ||
|
||
New Persisted Queries Plugin |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"name": "@graphql-yoga/plugin-persisted-queries", | ||
"version": "0.0.0", | ||
"description": "", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dotansimha/graphql-yoga.git", | ||
"directory": "packages/plugins/persisted-queries" | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"scripts": { | ||
"prepack": "bob prepack", | ||
"check": "tsc --pretty --noEmit" | ||
}, | ||
"author": "Arda TANRIKULU <[email protected]>", | ||
"license": "MIT", | ||
"buildOptions": { | ||
"input": "./src/index.ts" | ||
}, | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.mjs" | ||
}, | ||
"./*": { | ||
"require": "./dist/*.js", | ||
"import": "./dist/*.mjs" | ||
} | ||
}, | ||
"typings": "dist/index.d.ts", | ||
"typescript": { | ||
"definition": "dist/index.d.ts" | ||
}, | ||
"publishConfig": { | ||
"directory": "dist", | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"tiny-lru": "^8.0.2", | ||
"tslib": "^2.3.1" | ||
}, | ||
"peerDependencies": { | ||
"@graphql-yoga/common": "^2.3.0" | ||
}, | ||
"devDependencies": { | ||
"bob-the-bundler": "^1.5.1" | ||
} | ||
} |
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,93 @@ | ||
import { GraphQLYogaError, Plugin, PromiseOrValue } from '@graphql-yoga/common' | ||
import lru from 'tiny-lru' | ||
|
||
export interface PersistedQueriesStoreOptions { | ||
max?: number | ||
ttl?: number | ||
} | ||
|
||
export function createInMemoryPersistedQueriesStore( | ||
options: PersistedQueriesStoreOptions = {}, | ||
) { | ||
return lru<string>(options.max ?? 1000, options.ttl ?? 36000) | ||
} | ||
|
||
export interface PersistedQueriesOptions { | ||
store?: PersistedQueriesStore | ||
mode?: PersistedQueriesMode | ||
hash?: (str: string) => PromiseOrValue<string> | ||
} | ||
|
||
export enum PersistedQueriesMode { | ||
AUTOMATIC = 'AUTOMATIC', | ||
MANUAL = 'MANUAL', | ||
PERSISTED_ONLY = 'PERSISTED_ONLY', | ||
} | ||
|
||
export interface PersistedQueriesStore { | ||
get(key: string): PromiseOrValue<string | null | undefined> | ||
set?(key: string, query: string): PromiseOrValue<any> | ||
} | ||
|
||
export interface PersistedQueryExtension { | ||
version: 1 | ||
sha256Hash: string | ||
} | ||
|
||
export function usePersistedQueries<TPluginContext>( | ||
options: PersistedQueriesOptions = {}, | ||
): Plugin<TPluginContext> { | ||
const { | ||
mode = PersistedQueriesMode.AUTOMATIC, | ||
store = createInMemoryPersistedQueriesStore(), | ||
hash, | ||
} = options | ||
if (mode === PersistedQueriesMode.AUTOMATIC && store.set == null) { | ||
throw new Error( | ||
`Automatic Persisted Queries require "set" method to be implemented`, | ||
) | ||
} | ||
return { | ||
onRequestParse() { | ||
return { | ||
onRequestParseDone: async function persistedQueriesOnRequestParseDone({ | ||
params, | ||
setParams, | ||
}) { | ||
const persistedQueryData: PersistedQueryExtension = | ||
params.extensions?.persistedQuery | ||
if ( | ||
mode === PersistedQueriesMode.PERSISTED_ONLY && | ||
persistedQueryData == null | ||
) { | ||
throw new GraphQLYogaError('PersistedQueryOnly') | ||
} | ||
if (persistedQueryData?.version === 1) { | ||
if (params.query == null) { | ||
const persistedQuery = await store.get( | ||
persistedQueryData.sha256Hash, | ||
) | ||
if (persistedQuery == null) { | ||
throw new GraphQLYogaError('PersistedQueryNotFound') | ||
} | ||
setParams({ | ||
...params, | ||
query: persistedQuery, | ||
}) | ||
} else { | ||
if (hash != null) { | ||
const expectedHash = await hash(params.query) | ||
if (persistedQueryData.sha256Hash !== expectedHash) { | ||
throw new GraphQLYogaError('PersistedQueryMismatch') | ||
} | ||
} | ||
if (mode === PersistedQueriesMode.AUTOMATIC) { | ||
await store.set!(persistedQueryData.sha256Hash, params.query) | ||
} | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
} |
Oops, something went wrong.