Skip to content

Commit

Permalink
feat(options) postgraphile: add contextOptions to allow custom jwt cl…
Browse files Browse the repository at this point in the history
…aims (#3915)

* add contextOptions

* improve documentation

* version bump to 0.20.18

* add code safety around contextOptions type

* add changeset

* Go

---------

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
  • Loading branch information
MarkLyck and ardatan authored Apr 3, 2023
1 parent f2b0a5b commit aea1347
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/afraid-boats-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/postgraphile': patch
'@graphql-mesh/types': patch
---

add contextOptions to allow customization of jwt claims
8 changes: 8 additions & 0 deletions packages/handlers/postgraphile/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ export default class PostGraphileHandler implements MeshHandler {
await this.pgCache.set(cachedIntrospection);
}

let contextOptions = await loadFromModuleExportExpression<any>(this.config.contextOptions, {
cwd: this.baseDir,
importFn: this.importFn,
defaultExportName: 'default',
});
if (typeof contextOptions !== 'function') contextOptions = () => ({});

return {
schema,
executor({
Expand All @@ -145,6 +152,7 @@ export default class PostGraphileHandler implements MeshHandler {
queryDocumentAst: document,
operationName,
variables,
...contextOptions(meshContext),
},
function withPgContextCallback(pgContext) {
return defaultExecutor({
Expand Down
4 changes: 4 additions & 0 deletions packages/handlers/postgraphile/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
* `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.

0 comments on commit aea1347

Please sign in to comment.