Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add readFragment_UNSTABLE to experimental package
Reviewed By: kassens Differential Revision: D9772419 fbshipit-source-id: a51a16c87f8691c08b73dd541b4e5eb13d4dd88c
- Loading branch information
1 parent
6ae3cc4
commit 56ab587
Showing
2 changed files
with
70 additions
and
13 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
packages/relay-experimental/helpers/readFragment_UNSTABLE.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* 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. | ||
* | ||
* @flow strict-local | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const invariant = require('invariant'); | ||
|
||
import type { | ||
GraphQLTaggedNode, | ||
IEnvironment, | ||
Snapshot, | ||
Variables, | ||
} from 'relay-runtime'; | ||
|
||
function readFragment_UNSTABLE( | ||
environment: IEnvironment, | ||
fragment: GraphQLTaggedNode, | ||
fragmentRef: mixed, | ||
variables: Variables, | ||
): Snapshot | $ReadOnlyArray<Snapshot> { | ||
invariant( | ||
fragmentRef != null, | ||
'readFragment_UNSTABLE: Expected fragmentRef to be provided', | ||
); | ||
const { | ||
getFragment, | ||
getSelector, | ||
getSelectorList, | ||
} = environment.unstable_internal; | ||
const fragmentNode = getFragment(fragment); | ||
if (fragmentNode.metadata && fragmentNode.metadata.plural === true) { | ||
invariant( | ||
Array.isArray(fragmentRef), | ||
'Expected fragmentRef to be an array if fragment %s is marked as @relay(plural: true)', | ||
fragmentNode.name, | ||
); | ||
const selectors = getSelectorList(variables, fragmentNode, fragmentRef); | ||
invariant( | ||
selectors != null, | ||
'Expected to be able to read fragment %s', | ||
fragmentNode.name, | ||
); | ||
return selectors.map(selector => environment.lookup(selector)); | ||
} else { | ||
invariant( | ||
!Array.isArray(fragmentRef), | ||
'Expected fragmentRef not to be an array if fragment %s is not marked as @relay(plural: true)', | ||
fragmentNode.name, | ||
); | ||
const selector = getSelector(variables, fragmentNode, fragmentRef); | ||
invariant( | ||
selector != null, | ||
'Expected to be able to read fragment %s', | ||
fragmentNode.name, | ||
); | ||
return environment.lookup(selector); | ||
} | ||
} | ||
|
||
module.exports = readFragment_UNSTABLE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters