@graphcommerce/graphql-mesh 10.1.0-canary.36 → 10.1.0-canary.39

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 CHANGED
@@ -1,5 +1,15 @@
1
1
  # Change Log
2
2
 
3
+ ## 10.1.0-canary.39
4
+
5
+ ## 10.1.0-canary.38
6
+
7
+ ### Minor Changes
8
+
9
+ - [#2650](https://github.com/graphcommerce-org/graphcommerce/pull/2650) [`ccd01e9`](https://github.com/graphcommerce-org/graphcommerce/commit/ccd01e9ad061783b354fd8655b5319edb26a15f1) - Route server-side Magento traffic over an internal network with the new runtime-only `GC_MAGENTO_ENDPOINT_SERVER` environment variable (e.g. `http://varnish.magento-namespace.svc.cluster.local`). When set, every mesh request whose URL starts with the origin of `GC_MAGENTO_ENDPOINT` — GraphQL and REST — is rewritten to the internal origin and gains an `X-Forwarded-Proto: https` header, so frontend↔Magento traffic inside a Kubernetes cluster no longer hairpins over the public load balancer. Unset, behavior is unchanged. See the new "Routing Magento traffic over an internal network" section in the mesh docs. ([@paales](https://github.com/paales))
10
+
11
+ ## 10.1.0-canary.37
12
+
3
13
  ## 10.1.0-canary.36
4
14
 
5
15
  ## 10.1.0-canary.35
package/customFetch.ts CHANGED
@@ -17,6 +17,45 @@ const fetcher = fetchRetry(
17
17
  require('@whatwg-node/fetch').fetch,
18
18
  )
19
19
 
20
+ /**
21
+ * Optionally reroute server-side Magento traffic to an internal endpoint.
22
+ *
23
+ * When the runtime environment sets `GC_MAGENTO_ENDPOINT_SERVER` (e.g.
24
+ * `http://varnish.magento-namespace.svc.cluster.local`), every request whose URL starts with the
25
+ * origin of `GC_MAGENTO_ENDPOINT` is rewritten to that origin. All Magento traffic the mesh
26
+ * performs (GraphQL and REST) then stays on the internal network — e.g. within a Kubernetes cluster
27
+ * — instead of hairpinning over the public load balancer, while the public endpoint keeps serving
28
+ * the browser-facing concerns (media URLs, image optimization).
29
+ *
30
+ * - Both variables are read from `process.env` at request time: this module also runs outside the
31
+ * Next.js bundler (the mesh CLI imports it during `gc-mesh build`), so it cannot rely on
32
+ * build-inlined configuration.
33
+ * - Set `GC_MAGENTO_ENDPOINT_SERVER` in the runtime environment only (e.g. a Kubernetes ConfigMap),
34
+ * never in the build environment: schema introspection and static generation run where the
35
+ * internal endpoint is not reachable. Unset, this is a no-op.
36
+ * - The rewrite adds `X-Forwarded-Proto: https` — the internal path bypasses the TLS-terminating
37
+ * proxy that normally adds it, and without it Magento considers the request insecure and
38
+ * generates http:// URLs in responses.
39
+ */
40
+ function toServerEndpoint(
41
+ url: RequestInfo | URL,
42
+ options: RequestInitWithRetry | undefined,
43
+ ): [RequestInfo | URL, RequestInitWithRetry | undefined] {
44
+ const serverEndpoint = process.env.GC_MAGENTO_ENDPOINT_SERVER
45
+ const publicEndpoint = process.env.GC_MAGENTO_ENDPOINT
46
+ if (!serverEndpoint || !publicEndpoint) return [url, options]
47
+ if (typeof url !== 'string' && !(url instanceof URL)) return [url, options]
48
+
49
+ const publicOrigin = new URL(publicEndpoint).origin
50
+ const target = url.toString()
51
+ if (!target.startsWith(publicOrigin)) return [url, options]
52
+
53
+ return [
54
+ new URL(serverEndpoint).origin + target.slice(publicOrigin.length),
55
+ { ...options, headers: { ...options?.headers, 'x-forwarded-proto': 'https' } },
56
+ ]
57
+ }
58
+
20
59
  /**
21
60
  * @param {RequestInfo | URL} url
22
61
  * @param {import('fetch-retry').RequestInitWithRetry | undefined} options
@@ -27,12 +66,14 @@ export const fetch = (
27
66
  url: RequestInfo | URL,
28
67
  options: RequestInitWithRetry | undefined,
29
68
  ): Promise<Response> => {
69
+ const [serverUrl, serverOptions] = toServerEndpoint(url, options)
70
+
30
71
  const headers = Object.fromEntries(
31
- Object.entries(options?.headers ?? {}).filter(([, value]) => value !== ''),
72
+ Object.entries(serverOptions?.headers ?? {}).filter(([, value]) => value !== ''),
32
73
  )
33
74
 
34
- return fetcher(url, {
35
- ...options,
75
+ return fetcher(serverUrl, {
76
+ ...serverOptions,
36
77
  headers,
37
78
  retries: 6,
38
79
  retryDelay: (attempt) => 2 ** attempt * 200, // 200, 400, 800, 1600, 3200, 6400
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": "10.1.0-canary.36",
5
+ "version": "10.1.0-canary.39",
6
6
  "dependencies": {
7
7
  "@whatwg-node/fetch": "^0.10.13",
8
8
  "fetch-retry": "^5.0.6",
@@ -20,9 +20,9 @@
20
20
  },
21
21
  "peerDependencies": {
22
22
  "@apollo/client": "*",
23
- "@graphcommerce/eslint-config-pwa": "^10.1.0-canary.36",
24
- "@graphcommerce/prettier-config-pwa": "^10.1.0-canary.36",
25
- "@graphcommerce/typescript-config-pwa": "^10.1.0-canary.36",
23
+ "@graphcommerce/eslint-config-pwa": "^10.1.0-canary.39",
24
+ "@graphcommerce/prettier-config-pwa": "^10.1.0-canary.39",
25
+ "@graphcommerce/typescript-config-pwa": "^10.1.0-canary.39",
26
26
  "@graphql-mesh/runtime": "*",
27
27
  "@graphql-mesh/types": "*"
28
28
  },