Skip to content

Commit

Permalink
disallow non-root node(id:...) fields
Browse files Browse the repository at this point in the history
  • Loading branch information
josephsavona committed Oct 10, 2015
1 parent 02df4a8 commit bfb9a27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions scripts/babel-relay-plugin/lib/RelayQLPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var RelayQLFragmentSpread = _require.RelayQLFragmentSpread;
var RelayQLInlineFragment = _require.RelayQLInlineFragment;
var RelayQLMutation = _require.RelayQLMutation;
var RelayQLQuery = _require.RelayQLQuery;
var RelayQLType = _require.RelayQLType;

var invariant = require('./invariant');
var t = require('babel-core/lib/types');
Expand Down Expand Up @@ -206,6 +207,8 @@ var RelayQLPrinter = (function () {
requisiteFields.id = true;
}

validateField(field, parent.getType());

// TODO: Generalize to non-`Node` types.
if (fieldType.alwaysImplements('Node')) {
metadata.rootCall = 'node';
Expand Down Expand Up @@ -344,6 +347,13 @@ var RelayQLPrinter = (function () {
return RelayQLPrinter;
})();

function validateField(field, parentType) {
if (field.getName() === 'node') {
var args = field.getArguments();
invariant(args.length !== 1 || args[0].getName() !== 'id', 'You defined a `node(id: %s)` field on type `%s`, but Relay requires ' + 'the `node` field to be defined on the root type. See the Object ' + 'Identification Guide: \n' + 'http://facebook.github.io/relay/docs/graphql-object-identification.html', args[0] && args[0].getType().getName({ modifiers: true }), parentType.getName({ modifiers: false }));
}
}

function validateConnectionField(field) {
invariant(!field.hasArgument('first') || !field.hasArgument('before'), 'Connection arguments `%s(before: <cursor>, first: <count>)` are ' + 'not supported. Use `(first: <count>)`, ' + '`(after: <cursor>, first: <count>)`, or ' + '`(before: <cursor>, last: <count>)`.', field.getName());
invariant(!field.hasArgument('last') || !field.hasArgument('after'), 'Connection arguments `%s(after: <cursor>, last: <count>)` are ' + 'not supported. Use `(last: <count>)`, ' + '`(before: <cursor>, last: <count>)`, or ' + '`(after: <cursor>, first: <count>)`.', field.getName());
Expand Down
18 changes: 18 additions & 0 deletions scripts/babel-relay-plugin/src/RelayQLPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
RelayQLInlineFragment,
RelayQLMutation,
RelayQLQuery,
RelayQLType
} = require('./RelayQLAST');

const invariant = require('./invariant');
Expand Down Expand Up @@ -305,6 +306,8 @@ class RelayQLPrinter {
requisiteFields.id = true;
}

validateField(field, parent.getType());

// TODO: Generalize to non-`Node` types.
if (fieldType.alwaysImplements('Node')) {
metadata.rootCall = 'node';
Expand Down Expand Up @@ -493,6 +496,21 @@ class RelayQLPrinter {
}
}

function validateField(field: RelayQLField, parentType: RelayQLType): void {
if (field.getName() === 'node') {
var args = field.getArguments();
invariant(
args.length !== 1 || args[0].getName() !== 'id',
'You defined a `node(id: %s)` field on type `%s`, but Relay requires ' +
'the `node` field to be defined on the root type. See the Object ' +
'Identification Guide: \n' +
'http://facebook.github.io/relay/docs/graphql-object-identification.html',
args[0] && args[0].getType().getName({modifiers: true}),
parentType.getName({modifiers: false})
);
}
}

function validateConnectionField(field: RelayQLField): void {
invariant(
!field.hasArgument('first') || !field.hasArgument('before'),
Expand Down

0 comments on commit bfb9a27

Please sign in to comment.