@graphcommerce/graphql-mesh 9.0.0-canary.57 → 9.0.0-canary.58
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/package.json +4 -4
- package/plugin/forward-headers.ts +73 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 9.0.0-canary.58
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#2330](https://github.com/graphcommerce-org/graphcommerce/pull/2330) [`bc3ec5e`](https://github.com/graphcommerce-org/graphcommerce/commit/bc3ec5e439b97cea4a2cef23e4008c7e0cfd6797) - Created a new @graphql-mesh plugin to forward headers from backends as forwardedHeaders in extensions ([@paales](https://github.com/paales))
|
|
8
|
+
|
|
3
9
|
## 9.0.0-canary.57
|
|
4
10
|
|
|
5
11
|
## 9.0.0-canary.56
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graphcommerce/graphql-mesh",
|
|
3
3
|
"homepage": "https://www.graphcommerce.org/",
|
|
4
4
|
"repository": "github:graphcommerce-org/graphcommerce",
|
|
5
|
-
"version": "9.0.0-canary.
|
|
5
|
+
"version": "9.0.0-canary.58",
|
|
6
6
|
"main": "index.ts",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@graphql-mesh/apollo-link": "latest",
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"@apollo/client": "^3",
|
|
29
|
-
"@graphcommerce/eslint-config-pwa": "^9.0.0-canary.
|
|
30
|
-
"@graphcommerce/prettier-config-pwa": "^9.0.0-canary.
|
|
31
|
-
"@graphcommerce/typescript-config-pwa": "^9.0.0-canary.
|
|
29
|
+
"@graphcommerce/eslint-config-pwa": "^9.0.0-canary.58",
|
|
30
|
+
"@graphcommerce/prettier-config-pwa": "^9.0.0-canary.58",
|
|
31
|
+
"@graphcommerce/typescript-config-pwa": "^9.0.0-canary.58",
|
|
32
32
|
"graphql": "^16.7.1"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { isAsyncIterable } from '@envelop/core'
|
|
2
|
+
import { MeshPlugin, MeshPluginOptions } from '@graphql-mesh/types'
|
|
3
|
+
import type { MeshContext } from '../.mesh'
|
|
4
|
+
|
|
5
|
+
interface ForwardHeaderConfig {
|
|
6
|
+
forwardHeaders?: string[]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Configure in your meshrc.yaml:
|
|
11
|
+
*
|
|
12
|
+
* ```yaml
|
|
13
|
+
* plugins:
|
|
14
|
+
* - '@graphcommerce/graphql-mesh/plugin/forward-headers':
|
|
15
|
+
* forwardHeaders:
|
|
16
|
+
* - X-Magento-Cache-Id
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* On your GraphQL response, you will have a new `forwardedHeaders` field in the `extensions` object (always lower case).
|
|
20
|
+
*
|
|
21
|
+
* ```json
|
|
22
|
+
* {
|
|
23
|
+
* "data": {},
|
|
24
|
+
* "extensions": {
|
|
25
|
+
* "forwardedHeaders": {
|
|
26
|
+
* "x-magento-cache-id": "homeHash"
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export default function useForwardHeadersPlugin(
|
|
33
|
+
config: MeshPluginOptions<ForwardHeaderConfig>,
|
|
34
|
+
): MeshPlugin<MeshContext> {
|
|
35
|
+
const forwardHeaders = config.forwardHeaders?.map((header) => header.toLowerCase())
|
|
36
|
+
|
|
37
|
+
const store = new WeakMap<any, Map<string, string>>()
|
|
38
|
+
function getStoredForContext(context: MeshContext) {
|
|
39
|
+
let stored = store.get(context)
|
|
40
|
+
if (!stored) {
|
|
41
|
+
stored = new Map()
|
|
42
|
+
store.set(context, stored)
|
|
43
|
+
}
|
|
44
|
+
return stored
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
onFetch({ context }) {
|
|
49
|
+
if (!context) return undefined
|
|
50
|
+
return ({ response }) => {
|
|
51
|
+
const stored = getStoredForContext(context)
|
|
52
|
+
response.headers.forEach((value, headerName) => {
|
|
53
|
+
if (forwardHeaders?.includes(headerName)) stored.set(headerName, value)
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
onExecute({ args: { contextValue } }) {
|
|
59
|
+
return {
|
|
60
|
+
onExecuteDone({ result, setResult }) {
|
|
61
|
+
if (isAsyncIterable(result)) return
|
|
62
|
+
const stored = store.get(contextValue)
|
|
63
|
+
if (stored) {
|
|
64
|
+
setResult({
|
|
65
|
+
...result,
|
|
66
|
+
extensions: { ...result.extensions, forwardedHeaders: Object.fromEntries(stored) },
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
}
|