From 649a0360d63c36187fecb2c968b161666c2b8b68 Mon Sep 17 00:00:00 2001 From: Mark Lyck Date: Fri, 6 May 2022 20:37:28 -0400 Subject: [PATCH 1/6] add contextOptions --- packages/handlers/postgraphile/src/index.ts | 7 +++++++ packages/types/src/config.ts | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/handlers/postgraphile/src/index.ts b/packages/handlers/postgraphile/src/index.ts index 681e5fce4d627..6ff9a864f5a4b 100644 --- a/packages/handlers/postgraphile/src/index.ts +++ b/packages/handlers/postgraphile/src/index.ts @@ -125,6 +125,12 @@ export default class PostGraphileHandler implements MeshHandler { const jitExecutor = jitExecutorFactory(schema, this.name, this.logger); + const contextOptions = await loadFromModuleExportExpression(this.config.contextOptions, { + cwd: this.baseDir, + importFn: this.importFn, + defaultExportName: 'default', + }); + return { schema, executor: ({ document, variables, context: meshContext, rootValue, operationName, extensions }) => @@ -134,6 +140,7 @@ export default class PostGraphileHandler implements MeshHandler { queryDocumentAst: document, operationName, variables, + ...contextOptions(meshContext), }, pgContext => jitExecutor({ diff --git a/packages/types/src/config.ts b/packages/types/src/config.ts index f8bb62cce15b2..3040b088e901e 100644 --- a/packages/types/src/config.ts +++ b/packages/types/src/config.ts @@ -901,6 +901,18 @@ export interface PostGraphileHandler { [k: string]: any; } | string; + /** + * A function that returns postgraphile context options. It can either be an object or a string pointing to the object's path (e.g. "./my-config#pgSettings"). See the [postgraphile docs](https://www.graphile.org/postgraphile/usage-schema/) for more information. (Any of: JSON, String) + */ + contextOptions?: (context: any) => { + jwtToken?: string; + jwtSecret?: string; + jwtAudiences?: string; + jwtRole?: string; + jwtVerifyOptions?: any; + pgDefaultRole?: string; + pgSettings?: {} + } | string; /** * Enable GraphQL websocket transport support for subscriptions (default: true) */ From e986955e071cf7fd70f3c7fa03744642df065b66 Mon Sep 17 00:00:00 2001 From: Mark Lyck Date: Fri, 6 May 2022 21:32:03 -0400 Subject: [PATCH 2/6] improve documentation --- packages/types/src/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/src/config.ts b/packages/types/src/config.ts index 3040b088e901e..68a480c90c307 100644 --- a/packages/types/src/config.ts +++ b/packages/types/src/config.ts @@ -902,7 +902,7 @@ export interface PostGraphileHandler { } | string; /** - * A function that returns postgraphile context options. It can either be an object or a string pointing to the object's path (e.g. "./my-config#pgSettings"). See the [postgraphile docs](https://www.graphile.org/postgraphile/usage-schema/) for more information. (Any of: JSON, String) + * A function which takes context as a paramter and returns postgraphile context options. It can either be a function or a string pointing to the function's path (e.g. "./my-function#pgSettings"). See the [postgraphile docs](https://www.graphile.org/postgraphile/usage-schema/) for more information. (Any of: Function, String) */ contextOptions?: (context: any) => { jwtToken?: string; From 7985a75945aa27d3db11c7a8ed9b5da0b3dec493 Mon Sep 17 00:00:00 2001 From: Mark Lyck Date: Fri, 6 May 2022 21:33:45 -0400 Subject: [PATCH 3/6] version bump to 0.20.18 --- packages/handlers/postgraphile/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/handlers/postgraphile/package.json b/packages/handlers/postgraphile/package.json index 209d61e993d8a..a8002fded61b5 100644 --- a/packages/handlers/postgraphile/package.json +++ b/packages/handlers/postgraphile/package.json @@ -1,6 +1,6 @@ { "name": "@graphql-mesh/postgraphile", - "version": "0.20.17", + "version": "0.20.18", "sideEffects": false, "main": "dist/index.js", "module": "dist/index.mjs", From 6239afe3fa43362229fee7bd0c70787ce4c544e9 Mon Sep 17 00:00:00 2001 From: Mark Lyck Date: Fri, 6 May 2022 21:39:19 -0400 Subject: [PATCH 4/6] add code safety around contextOptions type --- packages/handlers/postgraphile/src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/handlers/postgraphile/src/index.ts b/packages/handlers/postgraphile/src/index.ts index 6ff9a864f5a4b..0478d2fd1c802 100644 --- a/packages/handlers/postgraphile/src/index.ts +++ b/packages/handlers/postgraphile/src/index.ts @@ -125,11 +125,12 @@ export default class PostGraphileHandler implements MeshHandler { const jitExecutor = jitExecutorFactory(schema, this.name, this.logger); - const contextOptions = await loadFromModuleExportExpression(this.config.contextOptions, { + let contextOptions = await loadFromModuleExportExpression(this.config.contextOptions, { cwd: this.baseDir, importFn: this.importFn, defaultExportName: 'default', }); + if (typeof contextOptions !== 'function') contextOptions = () => ({}); return { schema, From 1c6c4cb6af82c35b24ec5766051930f7ce34c458 Mon Sep 17 00:00:00 2001 From: Mark Lyck Date: Fri, 6 May 2022 21:50:01 -0400 Subject: [PATCH 5/6] add changeset --- .changeset/afraid-boats-grow.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/afraid-boats-grow.md diff --git a/.changeset/afraid-boats-grow.md b/.changeset/afraid-boats-grow.md new file mode 100644 index 0000000000000..86d4c3882f947 --- /dev/null +++ b/.changeset/afraid-boats-grow.md @@ -0,0 +1,6 @@ +--- +'@graphql-mesh/postgraphile': patch +'@graphql-mesh/types': patch +--- + +add contextOptions to allow customization of jwt claims From e2ff960fa6fe63fd7d6161802f27fadf14ce49b9 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Mon, 3 Apr 2023 19:14:48 +0300 Subject: [PATCH 6/6] Go --- packages/handlers/postgraphile/src/index.ts | 2 +- .../handlers/postgraphile/yaml-config.graphql | 4 ++++ packages/types/src/config-schema.json | 16 ++++++++++++++++ packages/types/src/config.ts | 16 ++++------------ .../PostGraphileHandler.generated.md | 3 ++- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/packages/handlers/postgraphile/src/index.ts b/packages/handlers/postgraphile/src/index.ts index 92c0ac62d2ee7..232c39554846a 100644 --- a/packages/handlers/postgraphile/src/index.ts +++ b/packages/handlers/postgraphile/src/index.ts @@ -135,7 +135,7 @@ export default class PostGraphileHandler implements MeshHandler { defaultExportName: 'default', }); if (typeof contextOptions !== 'function') contextOptions = () => ({}); - + return { schema, executor({ diff --git a/packages/handlers/postgraphile/yaml-config.graphql b/packages/handlers/postgraphile/yaml-config.graphql index 5df53b78a0eb6..8b0aa23673250 100644 --- a/packages/handlers/postgraphile/yaml-config.graphql +++ b/packages/handlers/postgraphile/yaml-config.graphql @@ -38,6 +38,10 @@ type PostGraphileHandler @md { Enables live-query support via GraphQL subscriptions (sends updated payload any time nested collections/records change) (default: true) """ live: Boolean + """ + A file that exports a function which takes context as a paramter and returns postgraphile context options (e.g. "./my-function#pgSettings"). See the [postgraphile docs](https://www.graphile.org/postgraphile/usage-schema/) for more information. + """ + contextOptions: Any } union PostgraphileExternalOptions = JSON | String diff --git a/packages/types/src/config-schema.json b/packages/types/src/config-schema.json index 45ba66ebaff95..71f00c95a55b0 100644 --- a/packages/types/src/config-schema.json +++ b/packages/types/src/config-schema.json @@ -2881,6 +2881,22 @@ "live": { "type": "boolean", "description": "Enables live-query support via GraphQL subscriptions (sends updated payload any time nested collections/records change) (default: true)" + }, + "contextOptions": { + "anyOf": [ + { + "type": "object", + "additionalProperties": true + }, + { + "type": "string" + }, + { + "type": "array", + "additionalItems": true + } + ], + "description": "A file that exports a function which takes context as a paramter and returns postgraphile context options (e.g. \"./my-function#pgSettings\"). See the [postgraphile docs](https://www.graphile.org/postgraphile/usage-schema/) for more information." } } }, diff --git a/packages/types/src/config.ts b/packages/types/src/config.ts index fa85dd796a0ba..65690582adf51 100644 --- a/packages/types/src/config.ts +++ b/packages/types/src/config.ts @@ -896,18 +896,6 @@ export interface PostGraphileHandler { [k: string]: any; } | string; - /** - * A function which takes context as a paramter and returns postgraphile context options. It can either be a function or a string pointing to the function's path (e.g. "./my-function#pgSettings"). See the [postgraphile docs](https://www.graphile.org/postgraphile/usage-schema/) for more information. (Any of: Function, String) - */ - contextOptions?: (context: any) => { - jwtToken?: string; - jwtSecret?: string; - jwtAudiences?: string; - jwtRole?: string; - jwtVerifyOptions?: any; - pgDefaultRole?: string; - pgSettings?: {} - } | string; /** * Enable GraphQL websocket transport support for subscriptions (default: true) */ @@ -916,6 +904,10 @@ export interface PostGraphileHandler { * Enables live-query support via GraphQL subscriptions (sends updated payload any time nested collections/records change) (default: true) */ live?: boolean; + /** + * A file that exports a function which takes context as a paramter and returns postgraphile context options (e.g. "./my-function#pgSettings"). See the [postgraphile docs](https://www.graphile.org/postgraphile/usage-schema/) for more information. + */ + contextOptions?: any; } export interface RAMLHandler { source: string; diff --git a/website/src/generated-markdown/PostGraphileHandler.generated.md b/website/src/generated-markdown/PostGraphileHandler.generated.md index ff692ca69afd7..2cbefe7f3d90e 100644 --- a/website/src/generated-markdown/PostGraphileHandler.generated.md +++ b/website/src/generated-markdown/PostGraphileHandler.generated.md @@ -8,4 +8,5 @@ * `JSON` * `String` * `subscriptions` (type: `Boolean`) - Enable GraphQL websocket transport support for subscriptions (default: true) -* `live` (type: `Boolean`) - Enables live-query support via GraphQL subscriptions (sends updated payload any time nested collections/records change) (default: true) \ No newline at end of file +* `live` (type: `Boolean`) - Enables live-query support via GraphQL subscriptions (sends updated payload any time nested collections/records change) (default: true) +* `contextOptions` (type: `Any`) - A file that exports a function which takes context as a paramter and returns postgraphile context options (e.g. "./my-function#pgSettings"). See the [postgraphile docs](https://www.graphile.org/postgraphile/usage-schema/) for more information. \ No newline at end of file