-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
449 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/todo-example/client-apollo/src/apollo-link/create-ws-apollo-link.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// for Apollo Client v3 older than v3.5.10: | ||
import { | ||
ApolloLink, | ||
Operation, | ||
FetchResult, | ||
Observable, | ||
} from "@apollo/client/core"; | ||
import { print } from "graphql"; | ||
import { createClient, Client } from "graphql-ws"; | ||
import { makeAsyncIterableIteratorFromSink } from "@n1ru4l/push-pull-async-iterable-iterator"; | ||
import { applySourceToSink } from "./shared"; | ||
|
||
class GraphQLWsLink extends ApolloLink { | ||
private client: Client; | ||
constructor(url: string) { | ||
super(); | ||
this.client = createClient({ | ||
url, | ||
}); | ||
} | ||
|
||
public request(operation: Operation): Observable<FetchResult> { | ||
return new Observable((sink) => { | ||
const source = makeAsyncIterableIteratorFromSink((sink) => { | ||
return this.client.subscribe<FetchResult>( | ||
{ ...operation, query: print(operation.query) }, | ||
{ | ||
next: sink.next.bind(sink), | ||
complete: sink.complete.bind(sink), | ||
error: sink.error.bind(sink), | ||
} | ||
); | ||
}); | ||
|
||
return applySourceToSink(source, sink); | ||
}); | ||
} | ||
} | ||
|
||
export function createWSApolloLink(url: string) { | ||
return new GraphQLWsLink(url); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/todo-example/client-relay/src/fetcher/create-ws-fetcher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { | ||
GraphQLResponse, | ||
Observable, | ||
RequestParameters, | ||
Variables, | ||
} from "relay-runtime"; | ||
import { createClient } from "graphql-ws"; | ||
import { Repeater } from "@repeaterjs/repeater"; | ||
import { applySourceToSink } from "./shared"; | ||
import { makeAsyncIterableIteratorFromSink } from "@n1ru4l/push-pull-async-iterable-iterator"; | ||
|
||
function makeEventStreamSource(url: string) { | ||
return new Repeater<GraphQLResponse>(async (push, end) => { | ||
const eventsource = new EventSource(url); | ||
eventsource.onmessage = function (event) { | ||
const data = JSON.parse(event.data); | ||
push(data); | ||
if (eventsource.readyState === 2) { | ||
end(); | ||
} | ||
}; | ||
eventsource.onerror = function (event) { | ||
console.log("Error", event); | ||
end(new Error("Check the console bruv.")); | ||
}; | ||
await end; | ||
|
||
eventsource.close(); | ||
}); | ||
} | ||
|
||
export function createWSFetcher(url: string) { | ||
const client = createClient({ url }); | ||
return ( | ||
request: RequestParameters, | ||
variables: Variables | ||
): Observable<GraphQLResponse> => { | ||
if (!request.text) throw new Error("Missing document."); | ||
const { text: operation, name } = request; | ||
|
||
return Observable.create<GraphQLResponse>((sink) => { | ||
const source = makeAsyncIterableIteratorFromSink((sink) => { | ||
return client.subscribe<GraphQLResponse>( | ||
{ variables, query: operation }, | ||
{ | ||
next: sink.next.bind(sink), | ||
complete: sink.complete.bind(sink), | ||
error: sink.error.bind(sink), | ||
} | ||
); | ||
}); | ||
|
||
return applySourceToSink(source, sink); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/todo-example/client-urql/src/urql-client/ws-client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
Client, | ||
subscriptionExchange, | ||
fetchExchange, | ||
cacheExchange, | ||
dedupExchange, | ||
ExecutionResult, | ||
} from "urql"; | ||
import { createClient } from "graphql-ws"; | ||
import { applySourceToSink } from "./shared"; | ||
import { makeAsyncIterableIteratorFromSink } from "@n1ru4l/push-pull-async-iterable-iterator"; | ||
|
||
export const createUrqlClient = (url: string) => { | ||
const client = createClient({ url }); | ||
return new Client({ | ||
url: "noop", | ||
exchanges: [ | ||
cacheExchange, | ||
dedupExchange, | ||
subscriptionExchange({ | ||
forwardSubscription(operation) { | ||
return { | ||
subscribe: (sink) => { | ||
const source = makeAsyncIterableIteratorFromSink((sink) => { | ||
return client.subscribe<ExecutionResult>( | ||
{ ...operation, query: operation.query }, | ||
{ | ||
next: sink.next.bind(sink), | ||
complete: sink.complete.bind(sink), | ||
error: sink.error.bind(sink), | ||
} | ||
); | ||
}); | ||
|
||
return { | ||
unsubscribe: applySourceToSink(source, sink), | ||
}; | ||
}, | ||
}; | ||
}, | ||
enableAllOperations: true, | ||
}), | ||
fetchExchange, | ||
], | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.