UNPKG

1.15 kBMarkdownView Raw
1## Relay Runtime
2
3A set of Relay APIs responsible for data fetching, reading and normalization of
4the GraphQL data.
5
6Example:
7
8```js
9// @flow strict-local
10
11import type {FetchFunction} from 'relay-runtime';
12
13const {
14 Environment,
15 Network,
16 Observable,
17 RecordSource,
18 Store,
19 fetchQuery,
20 graphql,
21} = require('relay-runtime');
22
23const fetchFn: FetchFunction = function (request, variables) {
24 return new Observable.create(source => {
25 fetch('/my-graphql-api', {
26 method: 'POST',
27 body: JSON.stringify({
28 text: request.text,
29 variables,
30 }),
31 })
32 .then(response => response.json())
33 .then(data => source.next(data));
34 });
35};
36
37const network = Network.create(fetchFn);
38const store = new Store(new RecordSource());
39const environment = new Environment({
40 network,
41 store,
42});
43
44fetchQuery(
45 environment,
46 graphql`
47 query AppQuery($id: ID!) {
48 user(id: $id) {
49 name
50 }
51 }
52 `,
53 {id: 'my-node-id'},
54).subscribe({
55 error: error => {
56 console.error(error);
57 },
58 next: data => {
59 console.log(data);
60 },
61});
62```
63
64For complete API reference, visit https://relay.dev/.