1 | ## Relay Runtime
|
2 |
|
3 | A set of Relay APIs responsible for data fetching, reading and normalization of
|
4 | the GraphQL data.
|
5 |
|
6 | Example:
|
7 |
|
8 | ```js
|
9 | // @flow strict-local
|
10 |
|
11 | import type {FetchFunction} from 'relay-runtime';
|
12 |
|
13 | const {
|
14 | Environment,
|
15 | Network,
|
16 | Observable,
|
17 | RecordSource,
|
18 | Store,
|
19 | fetchQuery,
|
20 | graphql,
|
21 | } = require('relay-runtime');
|
22 |
|
23 | const 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 |
|
37 | const network = Network.create(fetchFn);
|
38 | const store = new Store(new RecordSource());
|
39 | const environment = new Environment({
|
40 | network,
|
41 | store,
|
42 | });
|
43 |
|
44 | fetchQuery(
|
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 |
|
64 | For complete API reference, visit https://relay.dev/.
|