Skip to content

Commit ae94554

Browse files
authored
chore: simplify setup (#954)
1 parent 72136f3 commit ae94554

18 files changed

+96
-164
lines changed

packages/todo-example/client-apollo/src/apollo-link/create-http-apollo-link.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { HttpLink } from "@apollo/client/link/http";
99
import { isLiveQueryOperationDefinitionNode } from "@n1ru4l/graphql-live-query";
1010
import { Repeater } from "@repeaterjs/repeater";
1111
import { print, getOperationAST } from "graphql";
12-
import { applySourceToSink } from "./shared";
12+
import { applyLiveQueryJSONDiffPatch } from "@n1ru4l/graphql-live-query-patch-jsondiffpatch";
13+
import { applyAsyncIterableIteratorToSink } from "@n1ru4l/push-pull-async-iterable-iterator";
1314

1415
type SSELinkOptions = EventSourceInit & { uri: string };
1516

@@ -52,8 +53,10 @@ class SSELink extends ApolloLink {
5253
}
5354

5455
return new Observable((sink) =>
55-
applySourceToSink(
56-
makeEventStreamSource(url.toString(), this.options),
56+
applyAsyncIterableIteratorToSink(
57+
applyLiveQueryJSONDiffPatch(
58+
makeEventStreamSource(url.toString(), this.options)
59+
),
5760
sink
5861
)
5962
);

packages/todo-example/client-apollo/src/apollo-link/create-socket-io-apollo-link.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
import { ApolloLink, Operation, Observable, FetchResult } from "@apollo/client";
66
import { print } from "graphql";
77
import { io } from "socket.io-client";
8-
import { applySourceToSink } from "./shared";
8+
import { applyLiveQueryJSONDiffPatch } from "@n1ru4l/graphql-live-query-patch-jsondiffpatch";
9+
import { applyAsyncIterableIteratorToSink } from "@n1ru4l/push-pull-async-iterable-iterator";
910

1011
class SocketIOGraphQLApolloLink extends ApolloLink {
1112
private networkLayer: SocketIOGraphQLClient<FetchResult>;
@@ -16,12 +17,14 @@ class SocketIOGraphQLApolloLink extends ApolloLink {
1617

1718
public request(operation: Operation): Observable<FetchResult> | null {
1819
return new Observable<FetchResult>((sink) =>
19-
applySourceToSink(
20-
this.networkLayer.execute({
21-
operationName: operation.operationName,
22-
operation: print(operation.query),
23-
variables: operation.variables,
24-
}),
20+
applyAsyncIterableIteratorToSink(
21+
applyLiveQueryJSONDiffPatch(
22+
this.networkLayer.execute({
23+
operationName: operation.operationName,
24+
operation: print(operation.query),
25+
variables: operation.variables,
26+
})
27+
),
2528
sink
2629
)
2730
);

packages/todo-example/client-apollo/src/apollo-link/create-ws-apollo-link.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
import { print } from "graphql";
99
import { createClient, Client } from "graphql-ws";
1010
import { makeAsyncIterableIteratorFromSink } from "@n1ru4l/push-pull-async-iterable-iterator";
11-
import { applySourceToSink } from "./shared";
11+
import { applyLiveQueryJSONDiffPatch } from "@n1ru4l/graphql-live-query-patch-jsondiffpatch";
12+
import { applyAsyncIterableIteratorToSink } from "@n1ru4l/push-pull-async-iterable-iterator";
1213

1314
class GraphQLWsLink extends ApolloLink {
1415
private client: Client;
@@ -21,7 +22,7 @@ class GraphQLWsLink extends ApolloLink {
2122

2223
public request(operation: Operation): Observable<FetchResult> {
2324
return new Observable((sink) => {
24-
const source = makeAsyncIterableIteratorFromSink((sink) => {
25+
const source = makeAsyncIterableIteratorFromSink<FetchResult>((sink) => {
2526
return this.client.subscribe<FetchResult>(
2627
{ ...operation, query: print(operation.query) },
2728
{
@@ -32,7 +33,10 @@ class GraphQLWsLink extends ApolloLink {
3233
);
3334
});
3435

35-
return applySourceToSink(source, sink);
36+
return applyAsyncIterableIteratorToSink(
37+
applyLiveQueryJSONDiffPatch(source),
38+
sink
39+
);
3640
});
3741
}
3842
}

packages/todo-example/client-apollo/src/apollo-link/shared.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/todo-example/client-apollo/src/setupProxy.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/todo-example/client-apollo/src/setupTests.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/todo-example/client-relay/src/fetcher/create-http-fetcher.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
Variables,
66
} from "relay-runtime";
77
import { Repeater } from "@repeaterjs/repeater";
8-
import { applySourceToSink } from "./shared";
8+
import { applyLiveQueryJSONDiffPatch } from "@n1ru4l/graphql-live-query-patch-jsondiffpatch";
9+
import { applyAsyncIterableIteratorToSink } from "@n1ru4l/push-pull-async-iterable-iterator";
910

1011
function makeEventStreamSource(url: string) {
1112
return new Repeater<GraphQLResponse>(async (push, end) => {
@@ -49,8 +50,10 @@ export function createHTTPFetcher(url: string) {
4950
if (variables) {
5051
targetUrl.searchParams.append("variables", JSON.stringify(variables));
5152
}
52-
return applySourceToSink(
53-
makeEventStreamSource(targetUrl.toString()),
53+
return applyAsyncIterableIteratorToSink(
54+
applyLiveQueryJSONDiffPatch(
55+
makeEventStreamSource(targetUrl.toString())
56+
),
5457
sink
5558
);
5659
}

packages/todo-example/client-relay/src/fetcher/create-socket-io-fetcher.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
Variables,
77
} from "relay-runtime";
88
import { io } from "socket.io-client";
9-
import { applySourceToSink } from "./shared";
9+
import { applyLiveQueryJSONDiffPatch } from "@n1ru4l/graphql-live-query-patch-jsondiffpatch";
10+
import { applyAsyncIterableIteratorToSink } from "@n1ru4l/push-pull-async-iterable-iterator";
1011

1112
export function createSocketIOFetcher() {
1213
let host =
@@ -21,12 +22,14 @@ export function createSocketIOFetcher() {
2122
if (!request.text) throw new Error("Missing document.");
2223
const { text: operation, name } = request;
2324
return Observable.create<GraphQLResponse>((sink) =>
24-
applySourceToSink(
25-
networkInterface.execute({
26-
operation,
27-
variables,
28-
operationName: name,
29-
}),
25+
applyAsyncIterableIteratorToSink(
26+
applyLiveQueryJSONDiffPatch(
27+
networkInterface.execute({
28+
operation,
29+
variables,
30+
operationName: name,
31+
})
32+
),
3033
sink
3134
)
3235
);

packages/todo-example/client-relay/src/fetcher/create-ws-fetcher.ts

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,11 @@ import {
55
Variables,
66
} from "relay-runtime";
77
import { createClient } from "graphql-ws";
8-
import { Repeater } from "@repeaterjs/repeater";
9-
import { applySourceToSink } from "./shared";
10-
import { makeAsyncIterableIteratorFromSink } from "@n1ru4l/push-pull-async-iterable-iterator";
11-
12-
function makeEventStreamSource(url: string) {
13-
return new Repeater<GraphQLResponse>(async (push, end) => {
14-
const eventsource = new EventSource(url);
15-
eventsource.onmessage = function (event) {
16-
const data = JSON.parse(event.data);
17-
push(data);
18-
if (eventsource.readyState === 2) {
19-
end();
20-
}
21-
};
22-
eventsource.onerror = function (event) {
23-
console.log("Error", event);
24-
end(new Error("Check the console bruv."));
25-
};
26-
await end;
27-
28-
eventsource.close();
29-
});
30-
}
8+
import { applyLiveQueryJSONDiffPatch } from "@n1ru4l/graphql-live-query-patch-jsondiffpatch";
9+
import {
10+
makeAsyncIterableIteratorFromSink,
11+
applyAsyncIterableIteratorToSink,
12+
} from "@n1ru4l/push-pull-async-iterable-iterator";
3113

3214
export function createWSFetcher(url: string) {
3315
const client = createClient({ url });
@@ -39,18 +21,22 @@ export function createWSFetcher(url: string) {
3921
const { text: operation, name } = request;
4022

4123
return Observable.create<GraphQLResponse>((sink) => {
42-
const source = makeAsyncIterableIteratorFromSink((sink) => {
43-
return client.subscribe<GraphQLResponse>(
44-
{ variables, query: operation },
45-
{
46-
next: sink.next.bind(sink),
47-
complete: sink.complete.bind(sink),
48-
error: sink.error.bind(sink),
49-
}
50-
);
51-
});
52-
53-
return applySourceToSink(source, sink);
24+
const source = makeAsyncIterableIteratorFromSink<GraphQLResponse>(
25+
(sink) => {
26+
return client.subscribe<GraphQLResponse>(
27+
{ variables, query: operation },
28+
{
29+
next: sink.next.bind(sink) as any,
30+
complete: sink.complete.bind(sink),
31+
error: sink.error.bind(sink),
32+
}
33+
);
34+
}
35+
);
36+
return applyAsyncIterableIteratorToSink(
37+
applyLiveQueryJSONDiffPatch(source),
38+
sink
39+
);
5440
});
5541
};
5642
}

packages/todo-example/client-relay/src/fetcher/shared.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)