Throw better error for missing idType#2290
Throw better error for missing idType#2290brad-decker wants to merge 1 commit intofacebook:masterfrom
Conversation
|
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'); | ||
| } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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? 🙏
There was a problem hiding this comment.
doesn't calling:
context.serverSchema.getType(ID_TYPE);throw the error?
doing any checks afterwards isn't helpful :(
There was a problem hiding this comment.
Nope, if the id type isn't there this returns 'undefined' its the assertLeafType below that actually throws an error
There was a problem hiding this comment.
schema without ID that will throw this error https://github.com/freitasgst/weather-express-graphql/commit/c8ee408afc2e5ac4deaeb45d260800040199f57d
There was a problem hiding this comment.
Not after all, this is the right fix for that
|
Seeing as according to @sibelius this still needs to go in, here’s my review:
|
|
@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. |
5d049e1 to
5ad9acd
Compare
| 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); |
There was a problem hiding this comment.
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│ };```There was a problem hiding this comment.
the issue was the 'type: idType' assignment
|
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); |
There was a problem hiding this comment.
would splitting this into 2 variables help?
|
Sorry for missing your question @brad-decker, @jstejada’s suggestion makes sense to me. |
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).