Skip to content

Commit

Permalink
Don't break in compat mode when root field is not named Query
Browse files Browse the repository at this point in the history
Summary:
Fixes #1667 and #1624

I tested using this branch of the example repo: https://github.com/relayjs/relay-examples/compare/master...robrichard:custom-query-name?expand=1

kassens
Closes #1962

Differential Revision: D5518631

Pulled By: leebyron

fbshipit-source-id: 13af016f4224885f9fd7be370f5c288693db9e30
  • Loading branch information
robrichard authored and facebook-github-bot committed Jul 28, 2017
1 parent caa8d70 commit 1544f59
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
43 changes: 32 additions & 11 deletions packages/babel-plugin-relay/createClassicNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
const GraphQL = require('graphql');

const compileRelayQLTag = require('./compileRelayQLTag');
const getClassicTransformer = require('./getClassicTransformer');
const getFragmentNameParts = require('./getFragmentNameParts');
const invariant = require('./invariant');

Expand Down Expand Up @@ -295,17 +296,44 @@ function createObject(t, obj: any) {
);
}

function getSchemaOption(state) {
const schema = state.opts && state.opts.schema;
invariant(
schema,
'babel-plugin-relay: Missing schema option. ' +
'Check your .babelrc file or wherever you configure your Babel ' +
'plugins to ensure the "relay" plugin has a "schema" option.\n' +
'https://facebook.github.io/relay/docs/babel-plugin-relay.html#additional-options',
);
return schema;
}

function createFragmentForOperation(t, path, operation, state) {
let type;
const schema = getSchemaOption(state);
const fileOpts = (state.file && state.file.opts) || {};
const transformer = getClassicTransformer(schema, state.opts || {}, fileOpts);
switch (operation.operation) {
case 'query':
type = 'Query';
const queryType = transformer.schema.getQueryType();
if (!queryType) {
throw new Error('Schema does not contain a root query type.');
}
type = queryType.name;
break;
case 'mutation':
type = 'Mutation';
const mutationType = transformer.schema.getMutationType();
if (!mutationType) {
throw new Error('Schema does not contain a root mutation type.');
}
type = mutationType.name;
break;
case 'subscription':
type = 'Subscription';
const subscriptionType = transformer.schema.getSubscriptionType();
if (!subscriptionType) {
throw new Error('Schema does not contain a root subscription type.');
}
type = subscriptionType.name;
break;
default:
throw new Error(
Expand Down Expand Up @@ -335,14 +363,7 @@ function createFragmentForOperation(t, path, operation, state) {
}

function createRelayQLTemplate(t, path, node, state) {
const schema = state.opts && state.opts.schema;
invariant(
schema,
'babel-plugin-relay: Missing schema option. ' +
'Check your .babelrc file or wherever you configure your Babel ' +
'plugins to ensure the "relay" plugin has a "schema" option.\n' +
'https://facebook.github.io/relay/docs/babel-plugin-relay.html#additional-options',
);
const schema = getSchemaOption(state);
const [documentName, propName] = getFragmentNameParts(node.name.value);
const text = GraphQL.print(node);
const quasi = t.templateLiteral(
Expand Down
6 changes: 0 additions & 6 deletions packages/react-relay/classic/store/RelayStoreConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
*/
const ROOT_ID = 'client:root';

/**
* The type of the root record.
*/
const ROOT_TYPE = 'Query';

module.exports = {
ROOT_ID,
ROOT_TYPE,
};
4 changes: 2 additions & 2 deletions packages/react-relay/classic/store/RelayStoreData.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import type {

const {CLIENT_MUTATION_ID} = RelayConnectionInterface;
const {ID, ID_TYPE, NODE, NODE_TYPE, TYPENAME} = RelayNodeInterface;
const {ROOT_ID, ROOT_TYPE} = require('RelayStoreConstants');
const {ROOT_ID} = require('RelayStoreConstants');
const {EXISTENT} = RelayClassicRecordState;

const idField = RelayQuery.Field.build({
Expand Down Expand Up @@ -350,7 +350,7 @@ class RelayStoreData {

// Ensure the root record exists
const path = RelayQueryPath.getRootRecordPath();
recordWriter.putRecord(ROOT_ID, ROOT_TYPE, path);
recordWriter.putRecord(ROOT_ID, query.getType(), path);
if (this._queuedStore.getRecordState(ROOT_ID) !== EXISTENT) {
changeTracker.createID(ROOT_ID);
} else {
Expand Down

0 comments on commit 1544f59

Please sign in to comment.