mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-23 12:56:46 +08:00
prettier --parser typescript --tab-width 4 --semi --trailing-comma es5 \
--write --print-width 120 \
types/{react-relay,relay-runtime}/*.ts*
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { Environment, Network, RecordSource, Store, ConnectionHandler, ViewerHandler } from "relay-runtime";
|
|
|
|
const source = new RecordSource();
|
|
const store = new Store(source);
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~
|
|
// Network Layer
|
|
// ~~~~~~~~~~~~~~~~~~~~~
|
|
// Define a function that fetches the results of an operation (query/mutation/etc)
|
|
// and returns its results as a Promise:
|
|
function fetchQuery(operation: any, variables: { [key: string]: string }, cacheConfig: {}) {
|
|
return fetch("/graphql", {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
query: operation.text, // GraphQL text from input
|
|
variables,
|
|
}),
|
|
}).then((response: any) => {
|
|
return response.json();
|
|
});
|
|
}
|
|
|
|
// Create a network layer from the fetch function
|
|
const network = Network.create(fetchQuery);
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~
|
|
// Environment
|
|
// ~~~~~~~~~~~~~~~~~~~~~
|
|
const environment = new Environment({
|
|
handlerProvider, // Can omit.
|
|
network,
|
|
store,
|
|
});
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~
|
|
// Handler Provider
|
|
// ~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
function handlerProvider(handle: any) {
|
|
switch (handle) {
|
|
// Augment (or remove from) this list:
|
|
case "connection":
|
|
return ConnectionHandler;
|
|
case "viewer":
|
|
return ViewerHandler;
|
|
}
|
|
throw new Error(`handlerProvider: No handler provided for ${handle}`);
|
|
}
|