@canonical/summon-application 0.29.0 → 0.31.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.
- package/README.md +75 -13
- package/dist/esm/application/react/index.js +347 -0
- package/dist/esm/application/react/index.js.map +1 -0
- package/dist/esm/application/react/templates/.browserslistrc +2 -0
- package/dist/esm/application/react/templates/.storybook/main.ts.ejs +10 -0
- package/{src → dist/esm}/application/react/templates/README.md.ejs +54 -0
- package/dist/esm/application/react/templates/biome.json.ejs +6 -0
- package/dist/esm/application/react/templates/gitignore +25 -0
- package/{src → dist/esm}/application/react/templates/package.json.ejs +36 -2
- package/dist/esm/application/react/templates/patches/react-relay@18.2.0.patch +52 -0
- package/dist/esm/application/react/templates/patches/relay-runtime-network@0.1.0.patch +46 -0
- package/dist/esm/application/react/templates/relay.config.json +8 -0
- package/{src/application/react/templates/src/client/entry.tsx → dist/esm/application/react/templates/src/client/entry.tsx.ejs} +23 -0
- package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx +50 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ErrorBoundary.tests.tsx +56 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ErrorBoundary.tsx +37 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ProductCard.tsx +49 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ProductList.stories.tsx +103 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ProductList.tests.tsx +80 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ProductList.tsx +59 -0
- package/dist/esm/application/react/templates/src/domains/catalog/routes.ts +11 -0
- package/dist/esm/application/react/templates/src/lib/ClientOnly/ClientOnly.tests.tsx +35 -0
- package/dist/esm/application/react/templates/src/lib/ClientOnly/ClientOnly.tsx +31 -0
- package/dist/esm/application/react/templates/src/lib/ClientOnly/index.ts +1 -0
- package/{src → dist/esm}/application/react/templates/src/lib/Navigation/Navigation.tsx.ejs +3 -0
- package/{src/application/react/templates/src/lib/index.ts → dist/esm/application/react/templates/src/lib/index.ts.ejs} +3 -0
- package/dist/esm/application/react/templates/src/relay/__generated__/ProductCard_product.graphql.ts +82 -0
- package/dist/esm/application/react/templates/src/relay/__generated__/ProductListQuery.graphql.ts +272 -0
- package/dist/esm/application/react/templates/src/relay/environment.tests.ts +87 -0
- package/dist/esm/application/react/templates/src/relay/environment.ts +119 -0
- package/dist/esm/application/react/templates/src/relay/schema.graphql +111 -0
- package/dist/esm/application/react/templates/src/relay/schema.tests.ts +137 -0
- package/dist/esm/application/react/templates/src/relay/schema.ts +205 -0
- package/{src → dist/esm}/application/react/templates/src/routes.tsx.ejs +10 -0
- package/{src/application/react/templates/src/server/entry.tsx → dist/esm/application/react/templates/src/server/entry.tsx.ejs} +36 -0
- package/{src → dist/esm}/application/react/templates/src/sitemap/getSitemapItems.ts.ejs +3 -0
- package/{src/application/react/templates/tsconfig.json → dist/esm/application/react/templates/tsconfig.json.ejs} +3 -0
- package/dist/esm/application/react/templates/vite.config.ts.ejs +92 -0
- package/dist/esm/application/react/templates/vitest.setup.ts.ejs +22 -0
- package/dist/esm/domain/index.js +89 -0
- package/dist/esm/domain/index.js.map +1 -0
- package/{src/index.ts → dist/esm/index.js} +6 -8
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/route/index.js +115 -0
- package/dist/esm/route/index.js.map +1 -0
- package/dist/esm/route/insertRoute.js +178 -0
- package/dist/esm/route/insertRoute.js.map +1 -0
- package/dist/esm/shared/casing.js +15 -0
- package/dist/esm/shared/casing.js.map +1 -0
- package/dist/esm/shared/packageManager.js +38 -0
- package/dist/esm/shared/packageManager.js.map +1 -0
- package/dist/esm/shared/versions.js +88 -0
- package/dist/esm/shared/versions.js.map +1 -0
- package/dist/esm/wrapper/index.js +72 -0
- package/dist/esm/wrapper/index.js.map +1 -0
- package/dist/types/application/react/index.d.ts +12 -0
- package/dist/types/application/react/index.d.ts.map +1 -0
- package/dist/types/domain/index.d.ts +7 -0
- package/dist/types/domain/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/route/index.d.ts +7 -0
- package/dist/types/route/index.d.ts.map +1 -0
- package/dist/types/route/insertRoute.d.ts +34 -0
- package/dist/types/route/insertRoute.d.ts.map +1 -0
- package/{src/shared/casing.ts → dist/types/shared/casing.d.ts} +2 -7
- package/dist/types/shared/casing.d.ts.map +1 -0
- package/dist/types/shared/packageManager.d.ts +19 -0
- package/dist/types/shared/packageManager.d.ts.map +1 -0
- package/dist/types/shared/versions.d.ts +15 -0
- package/dist/types/shared/versions.d.ts.map +1 -0
- package/dist/types/wrapper/index.d.ts +7 -0
- package/dist/types/wrapper/index.d.ts.map +1 -0
- package/package.json +16 -15
- package/src/application/react/index.ts +0 -294
- package/src/application/react/templates/.storybook/main.ts +0 -5
- package/src/application/react/templates/biome.json.ejs +0 -6
- package/src/application/react/templates/vite.config.ts +0 -45
- package/src/application/react/templates/vitest.setup.ts +0 -9
- package/src/domain/index.ts +0 -119
- package/src/index.test.ts +0 -398
- package/src/route/index.ts +0 -154
- package/src/route/insertRoute.test.ts +0 -98
- package/src/route/insertRoute.ts +0 -236
- package/src/shared/versions.ts +0 -48
- package/src/wrapper/index.ts +0 -100
- /package/{src → dist/esm}/application/react/templates/.storybook/decorators/index.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/.storybook/decorators/withRouter.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/.storybook/preview.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/index.html.ejs +0 -0
- /package/{src → dist/esm}/application/react/templates/public/.gitkeep +0 -0
- /package/{src → dist/esm}/application/react/templates/public/robots.txt +0 -0
- /package/{src → dist/esm}/application/react/templates/src/assets/.gitkeep +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/account/AccountPage.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/account/LoginPage.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/account/routes.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/contact/ContactPage.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/contact/routes.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/marketing/GuidePage.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/marketing/HomePage.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/marketing/routes.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.stories.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.tests.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/index.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/styles.css +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/types.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/LazyComponent.stories.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/LazyComponent.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/index.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/Navigation/index.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ThemeSelector/ThemeSelector.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ThemeSelector/index.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/server/preview.bun.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/server/preview.express.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/server/renderer.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/server/server.bun.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/server/server.express.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/sitemap/renderer.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/styles/app.css +0 -0
- /package/{src → dist/esm}/application/react/templates/src/styles/index.css.ejs +0 -0
- /package/{src → dist/esm}/application/react/templates/src/vite-env.d.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/test/e2e/serverHarness.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/test/e2e/servers.e2e.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/vitest.config.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/vitest.e2e.config.ts +0 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { renderToString } from "react-dom/server";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import ClientOnly from "./ClientOnly.js";
|
|
5
|
+
|
|
6
|
+
describe("ClientOnly component", () => {
|
|
7
|
+
it("renders children after mounting in the browser", () => {
|
|
8
|
+
render(
|
|
9
|
+
<ClientOnly fallback={<p>Loading…</p>}>
|
|
10
|
+
<p>Client content</p>
|
|
11
|
+
</ClientOnly>,
|
|
12
|
+
);
|
|
13
|
+
expect(screen.getByText("Client content")).toBeInTheDocument();
|
|
14
|
+
expect(screen.queryByText("Loading…")).not.toBeInTheDocument();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("renders the fallback when server-rendered", () => {
|
|
18
|
+
const html = renderToString(
|
|
19
|
+
<ClientOnly fallback={<p>Loading…</p>}>
|
|
20
|
+
<p>Client content</p>
|
|
21
|
+
</ClientOnly>,
|
|
22
|
+
);
|
|
23
|
+
expect(html).toContain("Loading…");
|
|
24
|
+
expect(html).not.toContain("Client content");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("renders nothing on the server without a fallback", () => {
|
|
28
|
+
const html = renderToString(
|
|
29
|
+
<ClientOnly>
|
|
30
|
+
<p>Client content</p>
|
|
31
|
+
</ClientOnly>,
|
|
32
|
+
);
|
|
33
|
+
expect(html).toBe("");
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode, useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
export interface ClientOnlyProps {
|
|
4
|
+
/** Content rendered only after the component mounts in the browser. */
|
|
5
|
+
readonly children: ReactNode;
|
|
6
|
+
/** Rendered during SSR and the initial hydration pass. Defaults to nothing. */
|
|
7
|
+
readonly fallback?: ReactNode;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Defers `children` until after hydration, so they render only in the browser.
|
|
12
|
+
*
|
|
13
|
+
* The server (and React's first client pass, so hydration stays
|
|
14
|
+
* mismatch-free) renders `fallback`; the mount effect then flips to
|
|
15
|
+
* `children`. Use this for content that must not run during server
|
|
16
|
+
* rendering — e.g. components that fetch on render, like the Relay example
|
|
17
|
+
* on the catalog page, until server-side data serialization/hydration lands
|
|
18
|
+
* in a follow-up PR.
|
|
19
|
+
*/
|
|
20
|
+
export default function ClientOnly({
|
|
21
|
+
children,
|
|
22
|
+
fallback = null,
|
|
23
|
+
}: ClientOnlyProps): ReactElement {
|
|
24
|
+
const [isMounted, setIsMounted] = useState(false);
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
setIsMounted(true);
|
|
28
|
+
}, []);
|
|
29
|
+
|
|
30
|
+
return <>{isMounted ? children : fallback}</>;
|
|
31
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { type ClientOnlyProps, default } from "./ClientOnly.js";
|
|
@@ -9,6 +9,9 @@ export default function Navigation(): ReactElement {
|
|
|
9
9
|
<Link params={{ slug: "router-core" }} to="guide">
|
|
10
10
|
Guide
|
|
11
11
|
</Link>
|
|
12
|
+
<% if (relay) { -%>
|
|
13
|
+
<Link to="catalog">Catalog</Link>
|
|
14
|
+
<% } -%>
|
|
12
15
|
<% if (forms) { -%>
|
|
13
16
|
<Link to="contact">Contact</Link>
|
|
14
17
|
<% } -%>
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
<% if (relay) { -%>
|
|
2
|
+
export { default as ClientOnly } from "./ClientOnly/index.js";
|
|
3
|
+
<% } -%>
|
|
1
4
|
export { ExampleComponent } from "./ExampleComponent/index.js";
|
|
2
5
|
export { default as LazyComponent } from "./LazyComponent/index.js";
|
|
3
6
|
export { default as Navigation } from "./Navigation/index.js";
|
package/dist/esm/application/react/templates/src/relay/__generated__/ProductCard_product.graphql.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @generated SignedSource<<a6fcbf029d2f62fcf6f378581e4985a3>>
|
|
3
|
+
* @lightSyntaxTransform
|
|
4
|
+
* @nogrep
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/* tslint:disable */
|
|
8
|
+
/* eslint-disable */
|
|
9
|
+
// @ts-nocheck
|
|
10
|
+
|
|
11
|
+
import { ReaderFragment } from 'relay-runtime';
|
|
12
|
+
import { FragmentRefs } from "relay-runtime";
|
|
13
|
+
export type ProductCard_product$data = {
|
|
14
|
+
readonly currency: string;
|
|
15
|
+
readonly inStock: boolean;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly priceCents: number;
|
|
18
|
+
readonly rating: number;
|
|
19
|
+
readonly tagline: string;
|
|
20
|
+
readonly " $fragmentType": "ProductCard_product";
|
|
21
|
+
};
|
|
22
|
+
export type ProductCard_product$key = {
|
|
23
|
+
readonly " $data"?: ProductCard_product$data;
|
|
24
|
+
readonly " $fragmentSpreads": FragmentRefs<"ProductCard_product">;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const node: ReaderFragment = {
|
|
28
|
+
"argumentDefinitions": [],
|
|
29
|
+
"kind": "Fragment",
|
|
30
|
+
"metadata": null,
|
|
31
|
+
"name": "ProductCard_product",
|
|
32
|
+
"selections": [
|
|
33
|
+
{
|
|
34
|
+
"alias": null,
|
|
35
|
+
"args": null,
|
|
36
|
+
"kind": "ScalarField",
|
|
37
|
+
"name": "name",
|
|
38
|
+
"storageKey": null
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"alias": null,
|
|
42
|
+
"args": null,
|
|
43
|
+
"kind": "ScalarField",
|
|
44
|
+
"name": "tagline",
|
|
45
|
+
"storageKey": null
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"alias": null,
|
|
49
|
+
"args": null,
|
|
50
|
+
"kind": "ScalarField",
|
|
51
|
+
"name": "priceCents",
|
|
52
|
+
"storageKey": null
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"alias": null,
|
|
56
|
+
"args": null,
|
|
57
|
+
"kind": "ScalarField",
|
|
58
|
+
"name": "currency",
|
|
59
|
+
"storageKey": null
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"alias": null,
|
|
63
|
+
"args": null,
|
|
64
|
+
"kind": "ScalarField",
|
|
65
|
+
"name": "rating",
|
|
66
|
+
"storageKey": null
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"alias": null,
|
|
70
|
+
"args": null,
|
|
71
|
+
"kind": "ScalarField",
|
|
72
|
+
"name": "inStock",
|
|
73
|
+
"storageKey": null
|
|
74
|
+
}
|
|
75
|
+
],
|
|
76
|
+
"type": "Product",
|
|
77
|
+
"abstractKey": null
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
(node as any).hash = "542f84e3fd8c5cc8222c4d637a9ee99d";
|
|
81
|
+
|
|
82
|
+
export default node;
|
package/dist/esm/application/react/templates/src/relay/__generated__/ProductListQuery.graphql.ts
ADDED
|
@@ -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
|
+
};
|