Skip to content

Commit

Permalink
Warn when correct query can't be generated from path
Browse files Browse the repository at this point in the history
Reviewed By: kassens

Differential Revision: D3548354

fbshipit-source-id: 35f12ebb71063ccf8bdd47ef7365cdd552eb89bc
  • Loading branch information
yuzhi authored and Facebook Github Bot 7 committed Jul 15, 2016
1 parent cc04ade commit 928412d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/query/RelayQueryPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

'use strict';

const {EDGES} = require('RelayConnectionInterface');
import type {DataID} from 'RelayInternalTypes';
const RelayNodeInterface = require('RelayNodeInterface');
const RelayQuery = require('RelayQuery');
Expand Down Expand Up @@ -168,8 +169,20 @@ const RelayQueryPath = {
appendNode: RelayQuery.Fragment | RelayQuery.Field
): RelayQuery.Root {
let child = appendNode;
let prevField;
while (path.type === 'client') {
const node = path.node;
if (node instanceof RelayQuery.Field) {
const schemaName = node.getSchemaName();
warning(
!prevField || prevField !== EDGES || !node.isConnection(),
'RelayQueryPath.getQuery(): Cannot generate accurate query for ' +
'path with connection `%s`. Consider adding an `id` field to each ' +
'`node` to make them refetchable.',
schemaName
);
prevField = schemaName;
}
const idFieldName = node instanceof RelayQuery.Field ?
node.getInferredPrimaryKey() :
ID;
Expand Down
64 changes: 64 additions & 0 deletions src/query/__tests__/RelayQueryPath-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,70 @@ describe('RelayQueryPath', () => {
expect(pathQuery.getRoute().name).toBe(query.getRoute().name);
});

it('creates paths to non-refetchable connection fields', () => {
const query = getNode(Relay.QL`
query {
node(id:"123") {
id
}
}
`);
const friends = getNode(Relay.QL`
fragment on User {
friends(first:"1") {
edges {
cursor
}
}
}
`).getFieldByStorageKey('friends');
const edges = getNode(Relay.QL`
fragment on FriendsConnection {
edges {
cursor
}
}
`).getFieldByStorageKey('edges');
const cursor = getNode(Relay.QL`
fragment on FriendsEdge {
cursor
}
`).getFieldByStorageKey('cursor');

// edges is not refetchable because it is tied to a connection.
writer.putRecord('123', 'User');
const root = RelayQueryPath.create(query);
let path = RelayQueryPath.getPath(root, friends, 'client:1');
path = RelayQueryPath.getPath(path, edges, 'client:2');

const pathQuery = RelayQueryPath.getQuery(store, path, cursor);
expect(pathQuery).toEqualQueryRoot(getVerbatimNode(Relay.QL`
query {
node(id:"123") {
... on User {
__typename
id
friends(first:"1") {
edges {
cursor
}
}
}
}
}
`));
expect([
'RelayQueryPath.getQuery(): Cannot generate accurate query for ' +
'path with connection `%s`. Consider adding an `id` field to each ' +
'`node` to make them refetchable.',
'friends',
]).toBeWarnedNTimes(1);
expect(RelayQueryPath.getName(path)).toBe(query.getName());
expect(pathQuery.getName()).toBe(query.getName());
expect(pathQuery.getRoute().name).toBe(query.getRoute().name);
expect(pathQuery.isAbstract()).toBe(true);
});

it('warns if the root record\'s type is unknown', () => {
const query = getNode(Relay.QL`
query {
Expand Down

0 comments on commit 928412d

Please sign in to comment.