47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import type { Variables, RequestParameters, CacheConfig } from "relay-runtime";
|
|
import { Environment, Network, RecordSource, Store } from "relay-runtime";
|
|
|
|
export const fetchRelay = async (
|
|
params: RequestParameters,
|
|
variables: Variables,
|
|
_cacheConfig: CacheConfig
|
|
) => {
|
|
const csrfToken =
|
|
document
|
|
.querySelector('meta[name="csrf-token"]')
|
|
?.getAttribute("content") ?? "";
|
|
|
|
const response = await fetch("/graphql", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"X-CSRF-Token": csrfToken,
|
|
},
|
|
body: JSON.stringify({
|
|
query: params.text,
|
|
variables,
|
|
}),
|
|
});
|
|
|
|
const json = await response.json();
|
|
|
|
if (Array.isArray(json.errors)) {
|
|
throw new Error(
|
|
`Error fetching GraphQL query '${
|
|
params.name
|
|
}' with variables '${JSON.stringify(variables)}': ${JSON.stringify(
|
|
json.errors
|
|
)}`
|
|
);
|
|
}
|
|
|
|
return json;
|
|
};
|
|
|
|
export const environment = new Environment({
|
|
network: Network.create(fetchRelay),
|
|
store: new Store(new RecordSource(), {
|
|
gcReleaseBufferSize: 10,
|
|
}),
|
|
});
|