Skip to content

Commit

Permalink
Add warning when pagination containers don't have @connection
Browse files Browse the repository at this point in the history
Reviewed By: JenniferWang

Differential Revision: D5369512

fbshipit-source-id: f5141c73a1e383b526c371d0ea0134a6df809e61
  • Loading branch information
pranaygp authored and facebook-github-bot committed Jul 10, 2017
1 parent 1652e78 commit 3fe68c8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/react-relay/modern/ReactRelayPaginationContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ function findConnectionMetadata(fragments): ReactConnectionMetadata {
const fragment = fragments[fragmentName];
const connectionMetadata: ?Array<ConnectionMetadata> = (fragment.metadata &&
fragment.metadata.connection: any);
// HACK: metadata is always set to `undefined` in classic. In modern, even
// if empty, it is set to null (never undefined). We use that knowlege to
// check if we're dealing with classic or modern
if (fragment.metadata !== undefined) {
warning(
connectionMetadata,
'ReactRelayPaginationContainer: A @connection directive must be present.',
);
}
if (connectionMetadata) {
invariant(
connectionMetadata.length === 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,67 @@ describe('ReactRelayPaginationContainer', () => {
expect(environment.subscribe).not.toBeCalled();
});

it('warns if missing @connection directive', () => {
jest.mock('warning');

({UserFragment, UserQuery} = generateAndCompile(
`
query UserQuery(
$after: ID
$count: Int!
$id: ID!
$orderby: [String]
) {
node(id: $id) {
id
...UserFragment
}
}
fragment UserFragment on User {
friends(after: $after, first: $count, orderby: $orderby) {
edges {
node {
id
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
`,
));

TestContainer = ReactRelayPaginationContainer.createContainer(
TestComponent,
{
user: () => UserFragment,
},
{
direction: 'forward',
getConnectionFromProps,
getFragmentVariables: (vars, totalCount) => ({
...vars,
count: totalCount,
}),
getVariables,
query: UserQuery,
},
);

expect(() => {
ReactTestRenderer.create(
<ContextSetter environment={environment} variables={variables}>
<TestContainer />
</ContextSetter>,
);
}).toWarn([
'ReactRelayPaginationContainer: A @connection directive must be present.',
]);
});

describe('hasMore()', () => {
beforeEach(() => {
const userPointer = environment.lookup({
Expand Down

0 comments on commit 3fe68c8

Please sign in to comment.