Files
StevenandGitHub 4466ba436b chore(examples): use default prettier for examples/templates (#60530)
## Description
This PR ensures that the default prettier config is used for examples
and templates.

This config is compatible with `prettier@3` as well (upgrading prettier
is bigger change that can be a future PR).

## Changes
- Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will
be needed upgrading to prettier@3)
- Added `examples/.prettierrc.json` with default config (this will
change every example)
- Added `packages/create-next-app/templates/.prettierrc.json` with
default config (this will change every template)

## Related

- Fixes #54402
- Closes #54409
2024-01-11 16:01:44 -07:00

54 lines
1.7 KiB
JavaScript

import { useMemo } from "react";
import { Environment, Network, RecordSource, Store } from "relay-runtime";
let relayEnvironment;
// Define a function that fetches the results of an operation (query/mutation/etc)
// and returns its results as a Promise
function fetchQuery(operation, variables, cacheConfig, uploadables) {
return fetch(process.env.NEXT_PUBLIC_RELAY_ENDPOINT, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
}, // Add authentication and other headers here
body: JSON.stringify({
query: operation.text, // GraphQL text from input
variables,
}),
}).then((response) => response.json());
}
function createEnvironment(initialRecords) {
return new Environment({
// Create a network layer from the fetch function
network: Network.create(fetchQuery),
store: new Store(new RecordSource()),
});
}
export function initEnvironment(initialRecords) {
// Create a network layer from the fetch function
const environment = relayEnvironment ?? createEnvironment(initialRecords);
// If your page has Next.js data fetching methods that use Relay, the initial records
// will get hydrated here
if (initialRecords) {
environment.getStore().publish(new RecordSource(initialRecords));
}
// For SSG and SSR always create a new Relay environment
if (typeof window === "undefined") return environment;
// Create the Relay environment once in the client
if (!relayEnvironment) relayEnvironment = environment;
return relayEnvironment;
}
export function useEnvironment(initialRecords) {
const store = useMemo(
() => initEnvironment(initialRecords),
[initialRecords],
);
return store;
}