-
Notifications
You must be signed in to change notification settings - Fork 5
/
createRelayEnvironment.js
34 lines (30 loc) 路 1.03 KB
/
createRelayEnvironment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Environment, Network, RecordSource, Store } from "relay-runtime";
import * as AbsintheSocket from "@absinthe/socket";
import { createSubscriber } from "@absinthe/socket-relay";
import { Socket as PhoenixSocket } from "phoenix";
export const tokenKey = "token";
const source = new RecordSource();
const store = new Store(source);
const fetchQueryFactory = (customHeaders = {}) => (operation, variables) =>
fetch("/graphql", {
method: "POST",
headers: {
...customHeaders,
"content-type": "application/json",
},
body: JSON.stringify({
query: operation.text, // GraphQL text from input
variables,
}),
}).then(response => {
return response.json();
});
const absintheSocket = AbsintheSocket.create(new PhoenixSocket("/socket"));
export default token => {
const customHeaders = token ? { Authorization: `Bearer ${token}` } : null;
const network = Network.create(
fetchQueryFactory(customHeaders),
createSubscriber(absintheSocket),
);
return new Environment({ network, store });
};