UNPKG

3.53 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @emails oncall+relay
8 *
9 * @format
10 */
11'use strict';
12
13var invariant = require("fbjs/lib/invariant");
14
15var _require = require('relay-runtime'),
16 RelayRuntimeInternal = _require.__internal,
17 createOperationDescriptor = _require.createOperationDescriptor,
18 getRequest = _require.getRequest;
19
20/**
21 * Fetches the given query and variables on the provided environment,
22 * and de-dupes identical in-flight requests.
23 *
24 * Observing a request:
25 * ====================
26 * fetchQuery returns an Observable which you can call .subscribe()
27 * on. Subscribe optionally takes an Observer, which you can provide to
28 * observe network events:
29 *
30 * ```
31 * fetchQuery(environment, query, variables).subscribe({
32 * // Called when network requests starts
33 * start: (subsctiption) => {},
34 *
35 * // Called after a payload is received and written to the local store
36 * next: (payload) => {},
37 *
38 * // Called when network requests errors
39 * error: (error) => {},
40 *
41 * // Called when network requests fully completes
42 * complete: () => {},
43 *
44 * // Called when network request is unsubscribed
45 * unsubscribe: (subscription) => {},
46 * });
47 * ```
48 *
49 * Request Promise:
50 * ================
51 * The obervable can be converted to a Promise with .toPromise(), which will
52 * resolve to a snapshot of the query data when the first response is received
53 * from the server.
54 *
55 * ```
56 * fetchQuery(environment, query, variables).then((data) => {
57 * // ...
58 * });
59 * ```
60 *
61 * In-flight request de-duping:
62 * ============================
63 * By default, calling fetchQuery multiple times with the same
64 * environment, query and variables will not initiate a new request if a request
65 * for those same parameters is already in flight.
66 *
67 * A request is marked in-flight from the moment it starts until the moment it
68 * fully completes, regardless of error or successful completion.
69 *
70 * NOTE: If the request completes _synchronously_, calling fetchQuery
71 * a second time with the same arguments in the same tick will _NOT_ de-dupe
72 * the request given that it will no longer be in-flight.
73 *
74 *
75 * Data Retention:
76 * ===============
77 * This function will NOT retain query data, meaning that it is not guaranteed
78 * that the fetched data will remain in the Relay store after the request has
79 * completed.
80 * If you need to retain the query data outside of the network request,
81 * you need to use `environment.retain()`.
82 *
83 *
84 * Cancelling requests:
85 * ====================
86 * If the disposable returned by subscribe is called while the
87 * request is in-flight, the request will be cancelled.
88 *
89 * ```
90 * const disposable = fetchQuery(...).subscribe(...);
91 *
92 * // This will cancel the request if it is in-flight.
93 * disposable.dispose();
94 * ```
95 * NOTE: When using .toPromise(), the request cannot be cancelled.
96 */
97function fetchQuery(environment, query, variables, options) {
98 var queryNode = getRequest(query);
99 !(queryNode.params.operationKind === 'query') ? process.env.NODE_ENV !== "production" ? invariant(false, 'fetchQuery: Expected query operation') : invariant(false) : void 0;
100 var operation = createOperationDescriptor(queryNode, variables);
101 return RelayRuntimeInternal.fetchQuery(environment, operation, options).map(function () {
102 return environment.lookup(operation.fragment).data;
103 });
104}
105
106module.exports = fetchQuery;
\No newline at end of file