Skip to content

Commit

Permalink
Add readFragment_UNSTABLE to experimental package
Browse files Browse the repository at this point in the history
Reviewed By: kassens

Differential Revision: D9772419

fbshipit-source-id: a51a16c87f8691c08b73dd541b4e5eb13d4dd88c
  • Loading branch information
Juan Tejada authored and facebook-github-bot committed Sep 17, 2018
1 parent 6ae3cc4 commit 56ab587
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
67 changes: 67 additions & 0 deletions packages/relay-experimental/helpers/readFragment_UNSTABLE.js
@@ -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;
16 changes: 3 additions & 13 deletions packages/relay-experimental/helpers/readQuery_UNSTABLE.js
Expand Up @@ -10,8 +10,6 @@

'use strict';

const invariant = require('invariant');

import type {
GraphQLTaggedNode,
IEnvironment,
Expand All @@ -25,19 +23,11 @@ import type {
*/
function readQuery_UNSTABLE(
environment: IEnvironment,
gqlNode: GraphQLTaggedNode,
query: GraphQLTaggedNode,
variables: Variables,
): Snapshot {
const {
getRequest,
isRequest,
createOperationSelector,
} = environment.unstable_internal;
invariant(
isRequest(gqlNode),
'readQuery_UNSTABLE: Expected graphql node to be a query',
);
const queryNode = getRequest(gqlNode);
const {getRequest, createOperationSelector} = environment.unstable_internal;
const queryNode = getRequest(query);
const operation = createOperationSelector(queryNode, variables);
return environment.lookup(operation.fragment);
}
Expand Down

0 comments on commit 56ab587

Please sign in to comment.