Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: disallow non-root node(id:...) fields #449

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Input:
var Relay = require('Relay');
var fragment = Relay.QL`
fragment on InvalidType {
node(id: 123) {
... on User {
name
}
}
}
`;

Output:
var Relay = require('Relay');
var fragment = (function () {
throw new Error('GraphQL validation/transform error ``You defined a `node(id: Int)` field on type `InvalidType`, but Relay requires the `node` field to be defined on the root type. See the Object Identification Guide: \nhttp://facebook.github.io/relay/docs/graphql-object-identification.html`` in file `nonRootNodeField.fixture`.');
})();
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ type Root {
media(id: Int): Media
viewer: Viewer
search(query: [SearchInput!]): [SearchResult]
_invalid: InvalidType
}

type InvalidType {
node(id: Int): Node
}

type SearchResult {
Expand Down
46 changes: 46 additions & 0 deletions scripts/babel-relay-plugin/src/__tests__/testschema.rfc.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "_invalid",
"description": null,
"args": [],
"type": {
"kind": "OBJECT",
"name": "InvalidType",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
Expand Down Expand Up @@ -1273,6 +1285,40 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "InvalidType",
"description": null,
"fields": [
{
"name": "node",
"description": null,
"args": [
{
"name": "id",
"description": null,
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"defaultValue": null
}
],
"type": {
"kind": "INTERFACE",
"name": "Node",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "Mutation",
Expand Down