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 diff --git a/packages/handlers/postgraphile/src/index.ts b/packages/handlers/postgraphile/src/index.ts index a096f0ce7c64d..232c39554846a 100644 --- a/packages/handlers/postgraphile/src/index.ts +++ b/packages/handlers/postgraphile/src/index.ts @@ -129,6 +129,13 @@ export default class PostGraphileHandler implements MeshHandler { await this.pgCache.set(cachedIntrospection); } + let contextOptions = await loadFromModuleExportExpression(this.config.contextOptions, { + cwd: this.baseDir, + importFn: this.importFn, + defaultExportName: 'default', + }); + if (typeof contextOptions !== 'function') contextOptions = () => ({}); + return { schema, executor({ @@ -145,6 +152,7 @@ export default class PostGraphileHandler implements MeshHandler { queryDocumentAst: document, operationName, variables, + ...contextOptions(meshContext), }, function withPgContextCallback(pgContext) { return defaultExecutor({ 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 c97f00794526e..65690582adf51 100644 --- a/packages/types/src/config.ts +++ b/packages/types/src/config.ts @@ -904,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