Skip to content

Commit 56ab587

Browse files
Juan Tejadafacebook-github-bot
authored andcommitted
Add readFragment_UNSTABLE to experimental package
Reviewed By: kassens Differential Revision: D9772419 fbshipit-source-id: a51a16c87f8691c08b73dd541b4e5eb13d4dd88c
1 parent 6ae3cc4 commit 56ab587

File tree

2 files changed

+70
-13
lines changed

2 files changed

+70
-13
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
const invariant = require('invariant');
14+
15+
import type {
16+
GraphQLTaggedNode,
17+
IEnvironment,
18+
Snapshot,
19+
Variables,
20+
} from 'relay-runtime';
21+
22+
function readFragment_UNSTABLE(
23+
environment: IEnvironment,
24+
fragment: GraphQLTaggedNode,
25+
fragmentRef: mixed,
26+
variables: Variables,
27+
): Snapshot | $ReadOnlyArray<Snapshot> {
28+
invariant(
29+
fragmentRef != null,
30+
'readFragment_UNSTABLE: Expected fragmentRef to be provided',
31+
);
32+
const {
33+
getFragment,
34+
getSelector,
35+
getSelectorList,
36+
} = environment.unstable_internal;
37+
const fragmentNode = getFragment(fragment);
38+
if (fragmentNode.metadata && fragmentNode.metadata.plural === true) {
39+
invariant(
40+
Array.isArray(fragmentRef),
41+
'Expected fragmentRef to be an array if fragment %s is marked as @relay(plural: true)',
42+
fragmentNode.name,
43+
);
44+
const selectors = getSelectorList(variables, fragmentNode, fragmentRef);
45+
invariant(
46+
selectors != null,
47+
'Expected to be able to read fragment %s',
48+
fragmentNode.name,
49+
);
50+
return selectors.map(selector => environment.lookup(selector));
51+
} else {
52+
invariant(
53+
!Array.isArray(fragmentRef),
54+
'Expected fragmentRef not to be an array if fragment %s is not marked as @relay(plural: true)',
55+
fragmentNode.name,
56+
);
57+
const selector = getSelector(variables, fragmentNode, fragmentRef);
58+
invariant(
59+
selector != null,
60+
'Expected to be able to read fragment %s',
61+
fragmentNode.name,
62+
);
63+
return environment.lookup(selector);
64+
}
65+
}
66+
67+
module.exports = readFragment_UNSTABLE;

packages/relay-experimental/helpers/readQuery_UNSTABLE.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
'use strict';
1212

13-
const invariant = require('invariant');
14-
1513
import type {
1614
GraphQLTaggedNode,
1715
IEnvironment,
@@ -25,19 +23,11 @@ import type {
2523
*/
2624
function readQuery_UNSTABLE(
2725
environment: IEnvironment,
28-
gqlNode: GraphQLTaggedNode,
26+
query: GraphQLTaggedNode,
2927
variables: Variables,
3028
): Snapshot {
31-
const {
32-
getRequest,
33-
isRequest,
34-
createOperationSelector,
35-
} = environment.unstable_internal;
36-
invariant(
37-
isRequest(gqlNode),
38-
'readQuery_UNSTABLE: Expected graphql node to be a query',
39-
);
40-
const queryNode = getRequest(gqlNode);
29+
const {getRequest, createOperationSelector} = environment.unstable_internal;
30+
const queryNode = getRequest(query);
4131
const operation = createOperationSelector(queryNode, variables);
4232
return environment.lookup(operation.fragment);
4333
}

0 commit comments

Comments
 (0)