49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import {
|
|
Environment,
|
|
FetchFunction,
|
|
Network,
|
|
RecordSource,
|
|
Store,
|
|
} from "relay-runtime";
|
|
import { buildEndpoint } from "./utils";
|
|
|
|
const fetchRelay: FetchFunction = async (request, variables) => {
|
|
const response = await fetch(buildEndpoint("/console/v1/query"), {
|
|
method: "POST",
|
|
credentials: "include",
|
|
headers: {
|
|
Accept:
|
|
"application/graphql-response+json; charset=utf-8, application/json; charset=utf-8",
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
operationName: request.name,
|
|
query: request.text,
|
|
variables,
|
|
}),
|
|
});
|
|
|
|
const json = await response.json();
|
|
|
|
if (Array.isArray(json.errors)) {
|
|
throw new Error(
|
|
`Error fetching GraphQL query '${
|
|
request.name
|
|
}' with variables '${JSON.stringify(variables)}': ${JSON.stringify(
|
|
json.errors,
|
|
)}`,
|
|
);
|
|
}
|
|
|
|
return json;
|
|
};
|
|
|
|
function createRelayEnvironment() {
|
|
return new Environment({
|
|
network: Network.create(fetchRelay),
|
|
store: new Store(new RecordSource()),
|
|
});
|
|
}
|
|
|
|
export const RelayEnvironment: Environment = createRelayEnvironment();
|