@canonical/summon-application 0.29.0-experimental.0 → 0.30.0

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.
Files changed (127) hide show
  1. package/LICENSE +166 -0
  2. package/README.md +75 -13
  3. package/dist/esm/application/react/index.js +347 -0
  4. package/dist/esm/application/react/index.js.map +1 -0
  5. package/dist/esm/application/react/templates/.browserslistrc +2 -0
  6. package/dist/esm/application/react/templates/.storybook/main.ts.ejs +10 -0
  7. package/{src → dist/esm}/application/react/templates/README.md.ejs +54 -0
  8. package/dist/esm/application/react/templates/biome.json.ejs +6 -0
  9. package/dist/esm/application/react/templates/gitignore +25 -0
  10. package/{src → dist/esm}/application/react/templates/package.json.ejs +36 -2
  11. package/dist/esm/application/react/templates/patches/react-relay@18.2.0.patch +52 -0
  12. package/dist/esm/application/react/templates/patches/relay-runtime-network@0.1.0.patch +46 -0
  13. package/dist/esm/application/react/templates/relay.config.json +8 -0
  14. package/{src/application/react/templates/src/client/entry.tsx → dist/esm/application/react/templates/src/client/entry.tsx.ejs} +23 -0
  15. package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx +50 -0
  16. package/dist/esm/application/react/templates/src/domains/catalog/ErrorBoundary.tests.tsx +56 -0
  17. package/dist/esm/application/react/templates/src/domains/catalog/ErrorBoundary.tsx +37 -0
  18. package/dist/esm/application/react/templates/src/domains/catalog/ProductCard.tsx +49 -0
  19. package/dist/esm/application/react/templates/src/domains/catalog/ProductList.stories.tsx +103 -0
  20. package/dist/esm/application/react/templates/src/domains/catalog/ProductList.tests.tsx +80 -0
  21. package/dist/esm/application/react/templates/src/domains/catalog/ProductList.tsx +59 -0
  22. package/dist/esm/application/react/templates/src/domains/catalog/routes.ts +11 -0
  23. package/dist/esm/application/react/templates/src/lib/ClientOnly/ClientOnly.tests.tsx +35 -0
  24. package/dist/esm/application/react/templates/src/lib/ClientOnly/ClientOnly.tsx +31 -0
  25. package/dist/esm/application/react/templates/src/lib/ClientOnly/index.ts +1 -0
  26. package/{src → dist/esm}/application/react/templates/src/lib/Navigation/Navigation.tsx.ejs +3 -0
  27. package/{src/application/react/templates/src/lib/index.ts → dist/esm/application/react/templates/src/lib/index.ts.ejs} +3 -0
  28. package/dist/esm/application/react/templates/src/relay/__generated__/ProductCard_product.graphql.ts +82 -0
  29. package/dist/esm/application/react/templates/src/relay/__generated__/ProductListQuery.graphql.ts +272 -0
  30. package/dist/esm/application/react/templates/src/relay/environment.tests.ts +87 -0
  31. package/dist/esm/application/react/templates/src/relay/environment.ts +119 -0
  32. package/dist/esm/application/react/templates/src/relay/schema.graphql +111 -0
  33. package/dist/esm/application/react/templates/src/relay/schema.tests.ts +137 -0
  34. package/dist/esm/application/react/templates/src/relay/schema.ts +205 -0
  35. package/{src → dist/esm}/application/react/templates/src/routes.tsx.ejs +10 -0
  36. package/{src/application/react/templates/src/server/entry.tsx → dist/esm/application/react/templates/src/server/entry.tsx.ejs} +36 -0
  37. package/{src → dist/esm}/application/react/templates/src/sitemap/getSitemapItems.ts.ejs +3 -0
  38. package/{src/application/react/templates/tsconfig.json → dist/esm/application/react/templates/tsconfig.json.ejs} +3 -0
  39. package/dist/esm/application/react/templates/vite.config.ts.ejs +92 -0
  40. package/dist/esm/application/react/templates/vitest.setup.ts.ejs +22 -0
  41. package/dist/esm/domain/index.js +89 -0
  42. package/dist/esm/domain/index.js.map +1 -0
  43. package/{src/index.ts → dist/esm/index.js} +6 -8
  44. package/dist/esm/index.js.map +1 -0
  45. package/dist/esm/route/index.js +115 -0
  46. package/dist/esm/route/index.js.map +1 -0
  47. package/dist/esm/route/insertRoute.js +178 -0
  48. package/dist/esm/route/insertRoute.js.map +1 -0
  49. package/dist/esm/shared/casing.js +15 -0
  50. package/dist/esm/shared/casing.js.map +1 -0
  51. package/dist/esm/shared/packageManager.js +38 -0
  52. package/dist/esm/shared/packageManager.js.map +1 -0
  53. package/dist/esm/shared/versions.js +88 -0
  54. package/dist/esm/shared/versions.js.map +1 -0
  55. package/dist/esm/wrapper/index.js +72 -0
  56. package/dist/esm/wrapper/index.js.map +1 -0
  57. package/dist/types/application/react/index.d.ts +12 -0
  58. package/dist/types/application/react/index.d.ts.map +1 -0
  59. package/dist/types/domain/index.d.ts +7 -0
  60. package/dist/types/domain/index.d.ts.map +1 -0
  61. package/dist/types/index.d.ts +8 -0
  62. package/dist/types/index.d.ts.map +1 -0
  63. package/dist/types/route/index.d.ts +7 -0
  64. package/dist/types/route/index.d.ts.map +1 -0
  65. package/dist/types/route/insertRoute.d.ts +34 -0
  66. package/dist/types/route/insertRoute.d.ts.map +1 -0
  67. package/{src/shared/casing.ts → dist/types/shared/casing.d.ts} +2 -7
  68. package/dist/types/shared/casing.d.ts.map +1 -0
  69. package/dist/types/shared/packageManager.d.ts +19 -0
  70. package/dist/types/shared/packageManager.d.ts.map +1 -0
  71. package/dist/types/shared/versions.d.ts +15 -0
  72. package/dist/types/shared/versions.d.ts.map +1 -0
  73. package/dist/types/wrapper/index.d.ts +7 -0
  74. package/dist/types/wrapper/index.d.ts.map +1 -0
  75. package/package.json +17 -15
  76. package/src/application/react/index.ts +0 -294
  77. package/src/application/react/templates/.storybook/main.ts +0 -5
  78. package/src/application/react/templates/biome.json.ejs +0 -6
  79. package/src/application/react/templates/vite.config.ts +0 -45
  80. package/src/application/react/templates/vitest.setup.ts +0 -9
  81. package/src/domain/index.ts +0 -119
  82. package/src/index.test.ts +0 -398
  83. package/src/route/index.ts +0 -154
  84. package/src/route/insertRoute.test.ts +0 -98
  85. package/src/route/insertRoute.ts +0 -236
  86. package/src/shared/versions.ts +0 -48
  87. package/src/wrapper/index.ts +0 -100
  88. /package/{src → dist/esm}/application/react/templates/.storybook/decorators/index.ts +0 -0
  89. /package/{src → dist/esm}/application/react/templates/.storybook/decorators/withRouter.tsx +0 -0
  90. /package/{src → dist/esm}/application/react/templates/.storybook/preview.ts +0 -0
  91. /package/{src → dist/esm}/application/react/templates/index.html.ejs +0 -0
  92. /package/{src → dist/esm}/application/react/templates/public/.gitkeep +0 -0
  93. /package/{src → dist/esm}/application/react/templates/public/robots.txt +0 -0
  94. /package/{src → dist/esm}/application/react/templates/src/assets/.gitkeep +0 -0
  95. /package/{src → dist/esm}/application/react/templates/src/domains/account/AccountPage.tsx +0 -0
  96. /package/{src → dist/esm}/application/react/templates/src/domains/account/LoginPage.tsx +0 -0
  97. /package/{src → dist/esm}/application/react/templates/src/domains/account/routes.ts +0 -0
  98. /package/{src → dist/esm}/application/react/templates/src/domains/contact/ContactPage.tsx +0 -0
  99. /package/{src → dist/esm}/application/react/templates/src/domains/contact/routes.ts +0 -0
  100. /package/{src → dist/esm}/application/react/templates/src/domains/marketing/GuidePage.tsx +0 -0
  101. /package/{src → dist/esm}/application/react/templates/src/domains/marketing/HomePage.tsx +0 -0
  102. /package/{src → dist/esm}/application/react/templates/src/domains/marketing/routes.ts +0 -0
  103. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.stories.tsx +0 -0
  104. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.tests.tsx +0 -0
  105. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.tsx +0 -0
  106. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/index.ts +0 -0
  107. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/styles.css +0 -0
  108. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/types.ts +0 -0
  109. /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/LazyComponent.stories.tsx +0 -0
  110. /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/LazyComponent.tsx +0 -0
  111. /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/index.ts +0 -0
  112. /package/{src → dist/esm}/application/react/templates/src/lib/Navigation/index.ts +0 -0
  113. /package/{src → dist/esm}/application/react/templates/src/lib/ThemeSelector/ThemeSelector.tsx +0 -0
  114. /package/{src → dist/esm}/application/react/templates/src/lib/ThemeSelector/index.ts +0 -0
  115. /package/{src → dist/esm}/application/react/templates/src/server/preview.bun.ts +0 -0
  116. /package/{src → dist/esm}/application/react/templates/src/server/preview.express.ts +0 -0
  117. /package/{src → dist/esm}/application/react/templates/src/server/renderer.tsx +0 -0
  118. /package/{src → dist/esm}/application/react/templates/src/server/server.bun.ts +0 -0
  119. /package/{src → dist/esm}/application/react/templates/src/server/server.express.ts +0 -0
  120. /package/{src → dist/esm}/application/react/templates/src/sitemap/renderer.ts +0 -0
  121. /package/{src → dist/esm}/application/react/templates/src/styles/app.css +0 -0
  122. /package/{src → dist/esm}/application/react/templates/src/styles/index.css.ejs +0 -0
  123. /package/{src → dist/esm}/application/react/templates/src/vite-env.d.ts +0 -0
  124. /package/{src → dist/esm}/application/react/templates/test/e2e/serverHarness.ts +0 -0
  125. /package/{src → dist/esm}/application/react/templates/test/e2e/servers.e2e.ts +0 -0
  126. /package/{src → dist/esm}/application/react/templates/vitest.config.ts +0 -0
  127. /package/{src → dist/esm}/application/react/templates/vitest.e2e.config.ts +0 -0
@@ -0,0 +1,272 @@
1
+ /**
2
+ * @generated SignedSource<<dce8cdbb66e817eb63b1c8d044cdd730>>
3
+ * @lightSyntaxTransform
4
+ * @nogrep
5
+ */
6
+
7
+ /* tslint:disable */
8
+ /* eslint-disable */
9
+ // @ts-nocheck
10
+
11
+ import { ConcreteRequest } from 'relay-runtime';
12
+ import { FragmentRefs } from "relay-runtime";
13
+ export type ProductListQuery$variables = {
14
+ count: number;
15
+ cursor?: string | null | undefined;
16
+ };
17
+ export type ProductListQuery$data = {
18
+ readonly viewer: {
19
+ readonly name: string;
20
+ readonly products: {
21
+ readonly edges: ReadonlyArray<{
22
+ readonly node: {
23
+ readonly id: string;
24
+ readonly " $fragmentSpreads": FragmentRefs<"ProductCard_product">;
25
+ };
26
+ }>;
27
+ readonly pageInfo: {
28
+ readonly hasNextPage: boolean;
29
+ };
30
+ readonly totalCount: number;
31
+ };
32
+ };
33
+ };
34
+ export type ProductListQuery = {
35
+ response: ProductListQuery$data;
36
+ variables: ProductListQuery$variables;
37
+ };
38
+
39
+ const node: ConcreteRequest = (function(){
40
+ var v0 = [
41
+ {
42
+ "defaultValue": null,
43
+ "kind": "LocalArgument",
44
+ "name": "count"
45
+ },
46
+ {
47
+ "defaultValue": null,
48
+ "kind": "LocalArgument",
49
+ "name": "cursor"
50
+ }
51
+ ],
52
+ v1 = {
53
+ "alias": null,
54
+ "args": null,
55
+ "kind": "ScalarField",
56
+ "name": "name",
57
+ "storageKey": null
58
+ },
59
+ v2 = [
60
+ {
61
+ "kind": "Variable",
62
+ "name": "after",
63
+ "variableName": "cursor"
64
+ },
65
+ {
66
+ "kind": "Variable",
67
+ "name": "first",
68
+ "variableName": "count"
69
+ }
70
+ ],
71
+ v3 = {
72
+ "alias": null,
73
+ "args": null,
74
+ "kind": "ScalarField",
75
+ "name": "totalCount",
76
+ "storageKey": null
77
+ },
78
+ v4 = {
79
+ "alias": null,
80
+ "args": null,
81
+ "concreteType": "PageInfo",
82
+ "kind": "LinkedField",
83
+ "name": "pageInfo",
84
+ "plural": false,
85
+ "selections": [
86
+ {
87
+ "alias": null,
88
+ "args": null,
89
+ "kind": "ScalarField",
90
+ "name": "hasNextPage",
91
+ "storageKey": null
92
+ }
93
+ ],
94
+ "storageKey": null
95
+ },
96
+ v5 = {
97
+ "alias": null,
98
+ "args": null,
99
+ "kind": "ScalarField",
100
+ "name": "id",
101
+ "storageKey": null
102
+ };
103
+ return {
104
+ "fragment": {
105
+ "argumentDefinitions": (v0/*: any*/),
106
+ "kind": "Fragment",
107
+ "metadata": null,
108
+ "name": "ProductListQuery",
109
+ "selections": [
110
+ {
111
+ "alias": null,
112
+ "args": null,
113
+ "concreteType": "Viewer",
114
+ "kind": "LinkedField",
115
+ "name": "viewer",
116
+ "plural": false,
117
+ "selections": [
118
+ (v1/*: any*/),
119
+ {
120
+ "alias": null,
121
+ "args": (v2/*: any*/),
122
+ "concreteType": "ProductConnection",
123
+ "kind": "LinkedField",
124
+ "name": "products",
125
+ "plural": false,
126
+ "selections": [
127
+ (v3/*: any*/),
128
+ (v4/*: any*/),
129
+ {
130
+ "alias": null,
131
+ "args": null,
132
+ "concreteType": "ProductEdge",
133
+ "kind": "LinkedField",
134
+ "name": "edges",
135
+ "plural": true,
136
+ "selections": [
137
+ {
138
+ "alias": null,
139
+ "args": null,
140
+ "concreteType": "Product",
141
+ "kind": "LinkedField",
142
+ "name": "node",
143
+ "plural": false,
144
+ "selections": [
145
+ (v5/*: any*/),
146
+ {
147
+ "args": null,
148
+ "kind": "FragmentSpread",
149
+ "name": "ProductCard_product"
150
+ }
151
+ ],
152
+ "storageKey": null
153
+ }
154
+ ],
155
+ "storageKey": null
156
+ }
157
+ ],
158
+ "storageKey": null
159
+ }
160
+ ],
161
+ "storageKey": null
162
+ }
163
+ ],
164
+ "type": "Query",
165
+ "abstractKey": null
166
+ },
167
+ "kind": "Request",
168
+ "operation": {
169
+ "argumentDefinitions": (v0/*: any*/),
170
+ "kind": "Operation",
171
+ "name": "ProductListQuery",
172
+ "selections": [
173
+ {
174
+ "alias": null,
175
+ "args": null,
176
+ "concreteType": "Viewer",
177
+ "kind": "LinkedField",
178
+ "name": "viewer",
179
+ "plural": false,
180
+ "selections": [
181
+ (v1/*: any*/),
182
+ {
183
+ "alias": null,
184
+ "args": (v2/*: any*/),
185
+ "concreteType": "ProductConnection",
186
+ "kind": "LinkedField",
187
+ "name": "products",
188
+ "plural": false,
189
+ "selections": [
190
+ (v3/*: any*/),
191
+ (v4/*: any*/),
192
+ {
193
+ "alias": null,
194
+ "args": null,
195
+ "concreteType": "ProductEdge",
196
+ "kind": "LinkedField",
197
+ "name": "edges",
198
+ "plural": true,
199
+ "selections": [
200
+ {
201
+ "alias": null,
202
+ "args": null,
203
+ "concreteType": "Product",
204
+ "kind": "LinkedField",
205
+ "name": "node",
206
+ "plural": false,
207
+ "selections": [
208
+ (v5/*: any*/),
209
+ (v1/*: any*/),
210
+ {
211
+ "alias": null,
212
+ "args": null,
213
+ "kind": "ScalarField",
214
+ "name": "tagline",
215
+ "storageKey": null
216
+ },
217
+ {
218
+ "alias": null,
219
+ "args": null,
220
+ "kind": "ScalarField",
221
+ "name": "priceCents",
222
+ "storageKey": null
223
+ },
224
+ {
225
+ "alias": null,
226
+ "args": null,
227
+ "kind": "ScalarField",
228
+ "name": "currency",
229
+ "storageKey": null
230
+ },
231
+ {
232
+ "alias": null,
233
+ "args": null,
234
+ "kind": "ScalarField",
235
+ "name": "rating",
236
+ "storageKey": null
237
+ },
238
+ {
239
+ "alias": null,
240
+ "args": null,
241
+ "kind": "ScalarField",
242
+ "name": "inStock",
243
+ "storageKey": null
244
+ }
245
+ ],
246
+ "storageKey": null
247
+ }
248
+ ],
249
+ "storageKey": null
250
+ }
251
+ ],
252
+ "storageKey": null
253
+ }
254
+ ],
255
+ "storageKey": null
256
+ }
257
+ ]
258
+ },
259
+ "params": {
260
+ "cacheID": "57efd1ef4c828e58566c12be84d1987b",
261
+ "id": null,
262
+ "metadata": {},
263
+ "name": "ProductListQuery",
264
+ "operationKind": "query",
265
+ "text": "query ProductListQuery(\n $count: Int!\n $cursor: String\n) {\n viewer {\n name\n products(first: $count, after: $cursor) {\n totalCount\n pageInfo {\n hasNextPage\n }\n edges {\n node {\n id\n ...ProductCard_product\n }\n }\n }\n }\n}\n\nfragment ProductCard_product on Product {\n name\n tagline\n priceCents\n currency\n rating\n inStock\n}\n"
266
+ }
267
+ };
268
+ })();
269
+
270
+ (node as any).hash = "c95b3cfceaf197d5391b4d63a8c1ed36";
271
+
272
+ export default node;
@@ -0,0 +1,87 @@
1
+ import { fetchQuery } from "relay-runtime";
2
+ import { afterEach, describe, expect, it, vi } from "vitest";
3
+ import productListQueryNode, {
4
+ type ProductListQuery,
5
+ } from "./__generated__/ProductListQuery.graphql.js";
6
+ import { createEnvironment } from "./environment.js";
7
+ import { CATALOG_PRODUCTS } from "./schema.js";
8
+
9
+ /** Minimal valid GraphQL payload for {@link ProductListQuery}. */
10
+ const ENDPOINT_PAYLOAD = {
11
+ data: {
12
+ viewer: {
13
+ name: "Endpoint Viewer",
14
+ products: {
15
+ totalCount: 0,
16
+ pageInfo: { hasNextPage: false },
17
+ edges: [],
18
+ },
19
+ },
20
+ },
21
+ };
22
+
23
+ const fetchProductList = (environment: ReturnType<typeof createEnvironment>) =>
24
+ fetchQuery<ProductListQuery>(environment, productListQueryNode, {
25
+ count: 2,
26
+ }).toPromise();
27
+
28
+ describe("createEnvironment", () => {
29
+ afterEach(() => {
30
+ vi.unstubAllGlobals();
31
+ vi.unstubAllEnvs();
32
+ });
33
+
34
+ it("executes operations against the local mock schema by default", async () => {
35
+ const data = await fetchProductList(createEnvironment());
36
+
37
+ expect(data?.viewer.name).toBe("Ada Lovelace");
38
+ expect(data?.viewer.products.totalCount).toBe(CATALOG_PRODUCTS.length);
39
+ expect(data?.viewer.products.edges).toHaveLength(2);
40
+ expect(data?.viewer.products.pageInfo.hasNextPage).toBe(true);
41
+ });
42
+
43
+ it("posts operations to the endpoint when a URL is passed", async () => {
44
+ const fetchSpy = vi.fn<
45
+ (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>
46
+ >(async () => Response.json(ENDPOINT_PAYLOAD, { status: 200 }));
47
+ vi.stubGlobal("fetch", fetchSpy);
48
+
49
+ const data = await fetchProductList(
50
+ createEnvironment({ graphqlUrl: "https://api.example.test/graphql" }),
51
+ );
52
+
53
+ expect(fetchSpy).toHaveBeenCalledTimes(1);
54
+ expect(fetchSpy.mock.calls[0]?.[0]).toBe(
55
+ "https://api.example.test/graphql",
56
+ );
57
+ expect(data?.viewer.name).toBe("Endpoint Viewer");
58
+ });
59
+
60
+ it("reads the endpoint URL from VITE_GRAPHQL_URL", async () => {
61
+ const fetchSpy = vi.fn<
62
+ (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>
63
+ >(async () => Response.json(ENDPOINT_PAYLOAD, { status: 200 }));
64
+ vi.stubGlobal("fetch", fetchSpy);
65
+ vi.stubEnv("VITE_GRAPHQL_URL", "https://env.example.test/graphql");
66
+
67
+ await fetchProductList(createEnvironment());
68
+
69
+ expect(fetchSpy.mock.calls[0]?.[0]).toBe(
70
+ "https://env.example.test/graphql",
71
+ );
72
+ });
73
+
74
+ it("keeps stores independent across environments", async () => {
75
+ const first = createEnvironment();
76
+ const second = createEnvironment();
77
+ await fetchProductList(first);
78
+
79
+ expect(first.getStore().getSource().getRecordIDs().length).toBeGreaterThan(
80
+ 1,
81
+ );
82
+ // An untouched store holds only Relay's client root record.
83
+ expect(second.getStore().getSource().getRecordIDs()).toEqual([
84
+ "client:root",
85
+ ]);
86
+ });
87
+ });
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Relay environment factory.
3
+ *
4
+ * Builds the app's Relay `Environment` on top of `relay-runtime-network`'s
5
+ * middleware-driven fetch pipeline, in one of two modes:
6
+ *
7
+ * - **local (default)** — a `localGraphExecutor` resolves every operation
8
+ * in-process against the mock catalog schema (`./schema.ts`), so the
9
+ * boilerplate runs with zero backend.
10
+ * - **endpoint** — when `VITE_GRAPHQL_URL` is set (or a URL is passed
11
+ * explicitly), an `httpExecutor` + `urlMiddleware` posts operations to a
12
+ * real GraphQL server.
13
+ */
14
+
15
+ import {
16
+ Environment,
17
+ type FetchFunction,
18
+ type GraphQLResponse,
19
+ Network,
20
+ RecordSource,
21
+ Store,
22
+ } from "relay-runtime";
23
+ import {
24
+ createRelayRuntimeNetwork,
25
+ httpExecutor,
26
+ localGraphExecutor,
27
+ type RelayFetchContext,
28
+ type RelayRuntimeFetch,
29
+ urlMiddleware,
30
+ } from "relay-runtime-network";
31
+
32
+ /** Options for {@link createEnvironment}. */
33
+ export interface CreateEnvironmentOptions {
34
+ /**
35
+ * GraphQL endpoint URL. Overrides the `VITE_GRAPHQL_URL` env var; when
36
+ * neither is set the environment executes against the local mock schema.
37
+ */
38
+ readonly graphqlUrl?: string;
39
+ }
40
+
41
+ /** Reads the endpoint URL from Vite's env, treating the empty string as unset. */
42
+ const readConfiguredGraphqlUrl = (): string | undefined => {
43
+ const configured: unknown = import.meta.env.VITE_GRAPHQL_URL;
44
+ return typeof configured === "string" && configured.length > 0
45
+ ? configured
46
+ : undefined;
47
+ };
48
+
49
+ /**
50
+ * Builds the in-process network that executes against the mock schema.
51
+ *
52
+ * The schema module (and its `graphql` dependency) is loaded lazily inside
53
+ * the executor — `execute` already returns a promise, so the dynamic import
54
+ * adds no async boundary — keeping graphql-js and the mock catalog out of
55
+ * the main bundle, and unparsed, whenever the endpoint path is taken.
56
+ */
57
+ const createLocalNetwork = () =>
58
+ createRelayRuntimeNetwork({
59
+ fetch: {
60
+ executor: localGraphExecutor({
61
+ execute: async (context: RelayFetchContext) => {
62
+ const { text } = context.operation;
63
+ if (!text) {
64
+ throw new Error(
65
+ "The local mock schema requires full operation text; persisted queries are not supported.",
66
+ );
67
+ }
68
+ const { executeLocalOperation } = await import("./schema.js");
69
+ return executeLocalOperation({
70
+ text,
71
+ variables: context.variables,
72
+ });
73
+ },
74
+ }),
75
+ },
76
+ });
77
+
78
+ /** Builds the HTTP network that posts operations to `graphqlUrl`. */
79
+ const createHttpNetwork = (graphqlUrl: string) =>
80
+ createRelayRuntimeNetwork({
81
+ fetch: {
82
+ executor: httpExecutor(),
83
+ middlewares: [urlMiddleware({ url: graphqlUrl })],
84
+ },
85
+ });
86
+
87
+ /**
88
+ * Adapts the pipeline's fetch to relay-runtime's `FetchFunction`. Both
89
+ * describe the same GraphQL response wire shape, but relay-runtime types the
90
+ * payload as `PayloadData` where the pipeline says `unknown`, and takes its
91
+ * cache config as an interface where the pipeline wants a plain record —
92
+ * hence the spread and the single response cast at this library boundary.
93
+ */
94
+ const toFetchFunction =
95
+ (fetchGraphQL: RelayRuntimeFetch): FetchFunction =>
96
+ (params, variables, cacheConfig) =>
97
+ fetchGraphQL(params, variables, {
98
+ ...cacheConfig,
99
+ }) as Promise<GraphQLResponse>;
100
+
101
+ /**
102
+ * Creates a Relay `Environment` for the app.
103
+ *
104
+ * Call once per browser session (module scope in the client entry) and once
105
+ * per request on the server, so no store state leaks across requests.
106
+ */
107
+ export const createEnvironment = (
108
+ options: CreateEnvironmentOptions = {},
109
+ ): Environment => {
110
+ const graphqlUrl = options.graphqlUrl ?? readConfiguredGraphqlUrl();
111
+ const network = graphqlUrl
112
+ ? createHttpNetwork(graphqlUrl)
113
+ : createLocalNetwork();
114
+
115
+ return new Environment({
116
+ network: Network.create(toFetchFunction(network.fetch)),
117
+ store: new Store(new RecordSource()),
118
+ });
119
+ };
@@ -0,0 +1,111 @@
1
+ # Mock GraphQL schema for the Relay data-layer example.
2
+ #
3
+ # A compact catalog domain, shaped the Relay way: an object identification
4
+ # `Node` interface with globally unique IDs, a `viewer` root field as the
5
+ # entry point, and one paginated connection (`Viewer.products`) following the
6
+ # Relay Cursor Connections specification. `relay-compiler` validates queries
7
+ # against this file (see relay.config.json); `schema.ts` makes it executable
8
+ # against an in-memory dataset so the app runs without a GraphQL backend.
9
+
10
+ """
11
+ An object with a globally unique ID, per the Relay object identification spec.
12
+ """
13
+ interface Node {
14
+ """
15
+ The globally unique ID of the object.
16
+ """
17
+ id: ID!
18
+ }
19
+
20
+ """
21
+ The authenticated entry point into the graph.
22
+ """
23
+ type Viewer {
24
+ """
25
+ Display name of the current user.
26
+ """
27
+ name: String!
28
+
29
+ """
30
+ Products in the catalog, paginated forwards through a connection.
31
+ """
32
+ products(first: Int, after: String): ProductConnection!
33
+ }
34
+
35
+ """
36
+ A product in the catalog.
37
+ """
38
+ type Product implements Node {
39
+ id: ID!
40
+
41
+ """
42
+ Human-readable product name.
43
+ """
44
+ name: String!
45
+
46
+ """
47
+ One-line marketing description.
48
+ """
49
+ tagline: String!
50
+
51
+ """
52
+ Price in the minor unit of `currency` (e.g. cents).
53
+ """
54
+ priceCents: Int!
55
+
56
+ """
57
+ ISO 4217 currency code for `priceCents`.
58
+ """
59
+ currency: String!
60
+
61
+ """
62
+ Average review rating, from 0.0 to 5.0.
63
+ """
64
+ rating: Float!
65
+
66
+ """
67
+ Whether the product can currently be ordered.
68
+ """
69
+ inStock: Boolean!
70
+ }
71
+
72
+ """
73
+ A page of products and the cursors to continue paginating.
74
+ """
75
+ type ProductConnection {
76
+ edges: [ProductEdge!]!
77
+ pageInfo: PageInfo!
78
+
79
+ """
80
+ Total number of products in the catalog, across all pages.
81
+ """
82
+ totalCount: Int!
83
+ }
84
+
85
+ """
86
+ A product plus the opaque cursor identifying its position.
87
+ """
88
+ type ProductEdge {
89
+ cursor: String!
90
+ node: Product!
91
+ }
92
+
93
+ """
94
+ Forward-pagination info per the Relay connection specification.
95
+ """
96
+ type PageInfo {
97
+ endCursor: String
98
+ hasNextPage: Boolean!
99
+ }
100
+
101
+ type Query {
102
+ """
103
+ Fetches an object by its globally unique ID.
104
+ """
105
+ node(id: ID!): Node
106
+
107
+ """
108
+ The entry point for the current user's view of the graph.
109
+ """
110
+ viewer: Viewer!
111
+ }