-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
useFragment.js
98 lines (86 loc) · 2.66 KB
/
useFragment.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+relay
* @flow strict-local
* @format
*/
// flowlint ambiguous-object-type:error
'use strict';
import type {FragmentType, GraphQLTaggedNode} from 'relay-runtime';
const {useTrackLoadQueryInRender} = require('./loadQuery');
const useFragmentNode = require('./useFragmentNode');
const useStaticFragmentNodeWarning = require('./useStaticFragmentNodeWarning');
const {useDebugValue} = require('react');
const {getFragment} = require('relay-runtime');
// NOTE: These declares ensure that the type of the returned data is:
// - non-nullable if the provided ref type is non-nullable
// - nullable if the provided ref type is nullable
// - array of non-nullable if the privoided ref type is an array of
// non-nullable refs
// - array of nullable if the privoided ref type is an array of nullable refs
declare function useFragment<
TKey: {+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
>(
fragmentInput: GraphQLTaggedNode,
fragmentRef: TKey,
): $Call<<TFragmentData>({+$data?: TFragmentData, ...}) => TFragmentData, TKey>;
declare function useFragment<
TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
>(
fragmentInput: GraphQLTaggedNode,
fragmentRef: TKey,
): $Call<
<TFragmentData>(?{+$data?: TFragmentData, ...}) => ?TFragmentData,
TKey,
>;
declare function useFragment<
TKey: $ReadOnlyArray<{
+$data?: mixed,
+$fragmentSpreads: FragmentType,
...
}>,
>(
fragmentInput: GraphQLTaggedNode,
fragmentRef: TKey,
): $Call<
<TFragmentData>(
$ReadOnlyArray<{+$data?: TFragmentData, ...}>,
) => TFragmentData,
TKey,
>;
declare function useFragment<
TKey: ?$ReadOnlyArray<{
+$data?: mixed,
+$fragmentSpreads: FragmentType,
...
}>,
>(
fragmentInput: GraphQLTaggedNode,
fragmentRef: TKey,
): $Call<
<TFragmentData>(
?$ReadOnlyArray<{+$data?: TFragmentData, ...}>,
) => ?TFragmentData,
TKey,
>;
function useFragment(
fragmentInput: GraphQLTaggedNode,
fragmentRef: mixed,
): mixed {
// We need to use this hook in order to be able to track if
// loadQuery was called during render
useTrackLoadQueryInRender();
const fragmentNode = getFragment(fragmentInput);
useStaticFragmentNodeWarning(fragmentNode, 'first argument of useFragment()');
const {data} = useFragmentNode<_>(fragmentNode, fragmentRef, 'useFragment()');
if (__DEV__) {
// eslint-disable-next-line react-hooks/rules-of-hooks
useDebugValue({fragment: fragmentNode.name, data});
}
return data;
}
module.exports = useFragment;