@hyper-fetch/graphql 10.1.0 → 10.2.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 CHANGED
@@ -188,6 +188,7 @@ One typed client for every backend. Swap the adapter and keep the same `createRe
188
188
  <td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/core"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-hyper-fetch.svg" width="36" height="36" alt="" /><br /><sub><b>Core</b></sub></a></td>
189
189
  <td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/react"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-react.svg" width="36" height="36" alt="" /><br /><sub><b>React</b></sub></a></td>
190
190
  <td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/sockets"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-sockets.svg" width="36" height="36" alt="" /><br /><sub><b>Sockets</b></sub></a></td>
191
+ <td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/db"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-hyper-fetch.svg" width="36" height="36" alt="" /><br /><sub><b>DB</b></sub></a></td>
191
192
  <td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/cli"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-openapi.svg" width="36" height="36" alt="" /><br /><sub><b>OpenAPI CLI</b></sub></a></td>
192
193
  <td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/adapter-graphql"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-graphql.svg" width="36" height="36" alt="" /><br /><sub><b>GraphQL</b></sub></a></td>
193
194
  <td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/adapter-trpc"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-trpc.svg" width="36" height="36" alt="" /><br /><sub><b>tRPC</b></sub></a></td>
@@ -0,0 +1,78 @@
1
+ import { Client } from "@hyper-fetch/core";
2
+ import { collection, createDb, eq, relayConnection, rowType } from "@hyper-fetch/db";
3
+ import type { RelayConnection } from "@hyper-fetch/db";
4
+ import { createGraphqlMockingServer } from "@hyper-fetch/testing";
5
+ import { GraphqlAdapter } from "adapter";
6
+ import { HttpResponse, graphql } from "msw";
7
+
8
+ const { server, startServer, stopServer, resetMocks } = createGraphqlMockingServer();
9
+
10
+ type Post = { id: string; teamId: number; title: string };
11
+ type PostsResponse = { posts: RelayConnection<Post> };
12
+ type PostsVariables = { teamId: number; after?: string };
13
+
14
+ const postsQuery = `query Posts($teamId: Int!, $after: String) {
15
+ posts(teamId: $teamId, after: $after) { edges { node { id teamId title } } pageInfo { hasNextPage endCursor } }
16
+ }`;
17
+
18
+ const posts: Post[] = [
19
+ { id: "a", teamId: 1, title: "First" },
20
+ { id: "b", teamId: 1, title: "Second" },
21
+ { id: "c", teamId: 1, title: "Third" },
22
+ ];
23
+
24
+ describe("GraphQL [ @hyper-fetch/db loaders ]", () => {
25
+ beforeAll(() => startServer());
26
+ beforeEach(() => resetMocks());
27
+ afterAll(() => stopServer());
28
+
29
+ it("loads a paginated GraphQL connection with filters mapped to variables", async () => {
30
+ const client = new Client({ url: "http://localhost:4000/graphql" }).setAdapter(GraphqlAdapter());
31
+ const getPosts = client.createRequest<{ response: PostsResponse; payload: PostsVariables }>()({
32
+ endpoint: postsQuery,
33
+ });
34
+ const Post = collection(rowType<Post>(), { key: "id" }).load(getPosts, {
35
+ ...relayConnection((response: PostsResponse) => response.posts),
36
+ page: (after) => ({ payload: { after } }),
37
+ where: { teamId: { eq: (teamId) => ({ payload: { teamId } }), required: true } },
38
+ });
39
+ const db = createDb({ post: Post });
40
+
41
+ const variables: PostsVariables[] = [];
42
+ server.use(
43
+ graphql.query("Posts", ({ variables: received }) => {
44
+ variables.push(received as PostsVariables);
45
+ const start = received.after ? posts.findIndex((post) => post.id === received.after) + 1 : 0;
46
+ const page = posts.slice(start, start + 2);
47
+ const hasNextPage = start + 2 < posts.length;
48
+ return HttpResponse.json({
49
+ data: {
50
+ posts: {
51
+ edges: [...page.map((node) => ({ node })), null],
52
+ pageInfo: { hasNextPage, endCursor: hasNextPage ? page.at(-1)!.id : null },
53
+ },
54
+ },
55
+ });
56
+ }),
57
+ );
58
+
59
+ const query = db.post.where((post) => eq(post.teamId, 1));
60
+ const unsubscribe = query.subscribe(() => {});
61
+ await vi.waitFor(() => expect(query.getSnapshot().status).toBe("ready"));
62
+ expect(query.getSnapshot()).toMatchObject({ hasNextPage: true, data: [{ id: "a" }, { id: "b" }] });
63
+
64
+ await query.fetchNextPage();
65
+ expect(query.getSnapshot()).toMatchObject({ hasNextPage: false });
66
+ expect(query.getSnapshot().data?.map((post) => post.id)).toStrictEqual(["a", "b", "c"]);
67
+ expect(variables).toStrictEqual([{ teamId: 1 }, { teamId: 1, after: "b" }]);
68
+ unsubscribe();
69
+ db.destroy();
70
+ });
71
+
72
+ it("reads an empty connection", () => {
73
+ const { rows, cursor } = relayConnection((response: PostsResponse) => response.posts);
74
+ const empty: PostsResponse = { posts: { edges: null, pageInfo: { hasNextPage: true } } };
75
+ expect(rows(empty)).toStrictEqual([]);
76
+ expect(cursor(empty)).toBeNull();
77
+ });
78
+ });
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@hyper-fetch/graphql",
3
- "version": "10.1.0",
4
- "description": "GraphQL adapters for Hyper Fetch - queries and mutations over HTTP, subscriptions over WebSocket",
3
+ "version": "10.2.0",
4
+ "description": "GraphQL adapters for HyperFetch: typed queries and mutations over HTTP, subscriptions over WebSocket with graphql-ws",
5
5
  "author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
6
- "homepage": "https://hyperfetch.bettertyped.com/",
6
+ "homepage": "https://hyperfetch.bettertyped.com/docs/integrations/adapter-graphql/overview/",
7
7
  "license": "Apache-2.0",
8
8
  "main": "dist/index.mjs",
9
9
  "module": "dist/index.mjs",
@@ -48,6 +48,7 @@
48
48
  "graphql": "^15.0.0 || ^16.0.0"
49
49
  },
50
50
  "devDependencies": {
51
+ "@hyper-fetch/db": "workspace:*",
51
52
  "abortcontroller-polyfill": "^1.7.5",
52
53
  "graphql": "^16.13.2",
53
54
  "graphql-tag": "^2.12.6",
@@ -60,19 +61,13 @@
60
61
  ]
61
62
  },
62
63
  "keywords": [
63
- "fetch",
64
- "xhr",
65
- "requests",
66
- "js",
67
- "fetcher",
68
64
  "graphql",
69
- "gql",
70
- "gql-request",
71
- "gql-node",
72
- "gql-browser",
65
+ "graphql-client",
66
+ "apollo-alternative",
73
67
  "subscriptions",
74
- "websocket",
75
68
  "graphql-ws",
76
- "graphql-transport-ws"
69
+ "websocket",
70
+ "typescript",
71
+ "hyperfetch"
77
72
  ]
78
73
  }
package/vite.config.ts CHANGED
@@ -23,6 +23,7 @@ export default defineConfig({
23
23
  test: {
24
24
  alias: {
25
25
  "@hyper-fetch/core": path.resolve(__dirname, "../core/src/index.ts"),
26
+ "@hyper-fetch/db": path.resolve(__dirname, "../db/src/index.ts"),
26
27
  "@hyper-fetch/react": path.resolve(__dirname, "../react/src/index.ts"),
27
28
  "@hyper-fetch/sockets": path.resolve(__dirname, "../sockets/src/index.ts"),
28
29
  "@hyper-fetch/testing": path.resolve(__dirname, "../testing/src/index.ts"),