Skip to content

Throw better error for missing idType#2290

Closed
brad-decker wants to merge 1 commit intofacebook:masterfrom
brad-decker:master
Closed

Throw better error for missing idType#2290
brad-decker wants to merge 1 commit intofacebook:masterfrom
brad-decker:master

Conversation

@brad-decker
Copy link
Copy Markdown

Related to #2281

A better error message for the situation i ran into would have helped me figure out my mistake much faster. This just checks to see if there is no id field defined in the schema (returns undefined) and if so throws a different error message instead of asserting that 'undefined' is a leaf type (Which is most certainly is not).

@brad-decker
Copy link
Copy Markdown
Author

There are some flow errors and what not locally but does not appear to be related to my changes -- also if you'd like a test case written for this message i am all for it and would appreciate a little guidance on where that should be best placed. 👍 Thanks

const idType = context.serverSchema.getType(ID_TYPE);
if (!idType) {
throw new Error('Schema does not contain an id field on any types');
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure I understand this. The original code appears to try to get the ID type, shouldn’t that be a builtin type that should always exist?

Regardless, can you use invariant here instead, which appears to be the way assertions are performed throughout the existing code-base?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I should dive deeper into when the ID type actually exists, as there’s #2285 by @graingert that also mentions this issue.

Interestingly enough, @sibelius mentions that it would be fixed by my #2249 PR, although I didn’t knew of that issue at the time. @sibelius have you verified that? @brad-decker & @graingert would you mind verifying if #2249 fixes your issues and then please leave feedback on the PR? 🙏

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alloy -- will do. Thanks for checking this out.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't calling:

context.serverSchema.getType(ID_TYPE);

throw the error?

doing any checks afterwards isn't helpful :(

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, if the id type isn't there this returns 'undefined' its the assertLeafType below that actually throws an error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sibelius You indicated here that #2249 fixes it; is that indeed the case or not after all?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not after all, this is the right fix for that

@alloy
Copy link
Copy Markdown
Contributor

alloy commented Mar 21, 2018

Seeing as according to @sibelius this still needs to go in, here’s my review:

  • At runtime Relay will generate IDs for records that have none, but I’m not 100% sure off the top of my head if those need the ID type to exist in the schema; so do we even really need to throw an error in the case of no ID type existing?
  • If we do indeed need to throw, then please use invariant rather than throw.

@brad-decker
Copy link
Copy Markdown
Author

@alloy in my experience without the ID type the compiler fails, i'm open to other input on whether we need to throw. I'll just go ahead and change to invariant and then we can decide after that whether its required.

@brad-decker brad-decker force-pushed the master branch 2 times, most recently from 5d049e1 to 5ad9acd Compare April 6, 2018 17:04
let idType = context.serverSchema.getType(ID_TYPE);
invariant(idType, 'Schema does not contain an id field on any types');
// reassigning avoids flow error, would love to hear thoughts on this
idType = assertLeafType(idType);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alloy @sibelius is there a better way to handle flow typing here. If i don't reassign from the assertLeafType it gives me a flow error because it doesn't know that idType is in fact a LeafType.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flow error:

Cannot assign object literal to idField because:
 • in property type:
    • Either GraphQLInputObjectType [1] is incompatible with
      GraphQLScalarType [2].
    • Or GraphQLInputObjectType [1] is incompatible with GraphQLEnumType [3].
    • Or GraphQLInputObjectType [1] is incompatible with GraphQLList [4].
    • Or GraphQLInputObjectType [1] is incompatible with GraphQLNonNull [5].
 • in property type:
    • Either GraphQLInterfaceType [1] is incompatible with
      GraphQLScalarType [2].
    • Or GraphQLInterfaceType [1] is incompatible with GraphQLEnumType [3].
    • Or GraphQLInterfaceType [1] is incompatible with GraphQLList [4].
    • Or GraphQLInterfaceType [1] is incompatible with GraphQLNonNull [5].
 • in property type:
    • Either GraphQLObjectType [1] is incompatible with GraphQLScalarType [2].
    • Or GraphQLObjectType [1] is incompatible with GraphQLEnumType [3].
    • Or GraphQLObjectType [1] is incompatible with GraphQLList [4].
    • Or GraphQLObjectType [1] is incompatible with GraphQLNonNull [5].
 • in property type:
    • Either GraphQLUnionType [1] is incompatible with GraphQLScalarType [2].
    • Or GraphQLUnionType [1] is incompatible with GraphQLEnumType [3].
    • Or GraphQLUnionType [1] is incompatible with GraphQLList [4].
    • Or GraphQLUnionType [1] is incompatible with GraphQLNonNull [5].

        packages/relay-compiler/transforms/RelayGenerateIDFieldTransform.js
         53│   const idType = context.serverSchema.getType(ID_TYPE);
         54│   invariant(idType, 'Schema does not contain an id field on any types');
         55│   assertLeafType(idType);
         56│   const idField: ScalarField = {
         57│     kind: 'ScalarField',
         58│     alias: (null: ?string),
         59│     args: [],
         60│     directives: [],
         61│     handles: null,
         62│     metadata: null,
         63│     name: ID,
         64│     type: idType,
         65│   };
         66│   const state = {
         67│     idField,
         68│   };```

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue was the 'type: idType' assignment

@brad-decker
Copy link
Copy Markdown
Author

@alloy / @sibelius ready for re-review, did something weird to get around a flow issue and would welcome feedback.

@brad-decker
Copy link
Copy Markdown
Author

Checking back in, anything I can do to make this better?

let idType = context.serverSchema.getType(ID_TYPE);
invariant(idType, 'Schema does not contain an id field on any types');
// reassigning avoids flow error, would love to hear thoughts on this
idType = assertLeafType(idType);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would splitting this into 2 variables help?

@alloy
Copy link
Copy Markdown
Contributor

alloy commented May 21, 2018

Sorry for missing your question @brad-decker, @jstejada’s suggestion makes sense to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants