-
Notifications
You must be signed in to change notification settings - Fork 2k
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] "3.1.5 Enums" - requires additional consideration. Use complex objects with functions for enum values. #435
Comments
Deeper investigations shows that problem with const RootQuery = new GraphQLObjectType({
name: 'RootQuery',
fields: {
'userConnection': {
type: UserConnection,
args: {
first: { type: GraphQLInt },
after: { type: CursorType },
last: { type: GraphQLInt },
before: { type: CursorType},
...additionalArgs,
sort: {
type: SortEnumType,
defaultValue: SortEnumType.getValues()[0].name, // <<<< problem here <<<<<<<<<<<
description: 'Sort argument for data ordering',
},
},
},
});
So what |
…alues including Object-reference values. This illustrates part of the solution to #435
I added a test to illustrate complex enums which passes correctly. I believe the issue here is that your enum definitions are ill-formed. Enums are defined as a list of enum descriptors, one field of which is const Complex1 = { someRandomObject: 1 };
const Complex2 = { someOtherRandomObject: 2 };
const ComplexEnum = new GraphQLEnumType({
name: 'Complex',
values: {
ONE: { value: Complex1 },
TWO: { value: Complex2 },
}
}); |
b2c8332 also illustrates the use of the Enum getValues() method |
Added one last test illustrating Are there any other concerns you have here? I'll add more tests |
Your tests answered to all my questions with enums. No more 💚/💔 in my head. Thank's a lot!!! Enum definition in my case was covered by flow connected with graphql. Also I checked it with your example. And they are identical. Problem in my case, that I use function in sort value. This function needs for passing custom logic to resolve function. And Add PR with broken test fd96f5a |
Thanks for isolating that! I'll take a closer look |
A bug was reported in #435 and reproduced in #438 which illustrates an issue where defaultValue would serialize incorrectly or even throw an invariant violation when working with input objects or enums backed by values other than their names. The root of the issue is that astFromValue accepts an *internal* value, but then produces an AST directly from that value without serializing it to an *external* value first via calling `type.serialize(value)`. This critical missing step is responsible for the reported issue. In order to fix correctly, this refactored this method to be a closer dual to [`valueFromAST`](https://github.com/graphql/graphql-js/blob/master/src/utilities/valueFromAST.js), relying on the GraphQL type rather than the JavaScript type to guide coercion. As a consequence, `astFromValue` now *requires* a type rather than accepting one optionally.
A bug was reported in #435 and reproduced in #438 which illustrates an issue where defaultValue would serialize incorrectly or even throw an invariant violation when working with input objects or enums backed by values other than their names. The root of the issue is that astFromValue accepts an *internal* value, but then produces an AST directly from that value without serializing it to an *external* value first via calling `type.serialize(value)`. This critical missing step is responsible for the reported issue. In order to fix correctly, this refactored this method to be a closer dual to [`valueFromAST`](https://github.com/graphql/graphql-js/blob/master/src/utilities/valueFromAST.js), relying on the GraphQL type rather than the JavaScript type to guide coercion. As a consequence, `astFromValue` now *requires* a type rather than accepting one optionally.
Closing this now, and will get this fix out in the next point release - feel free to reopen if theres anything I missed |
Awesome work! All errors are GONE! All tested with Also was fixed BTW. Thanks for 0.29 flow upgrade! |
In graphql-compose-connection I use
Enum type
for storing avaliable sortings for connections, so values represent complex objects with function, eg:With such
Enum
graphql server starts and works perfectly (example).But throws error when I try to build introspection
graphql(Schema, introspectionQuery)
or runprintSchema(Schema)
:According to astFromValue.js#L116 it accepts only
string
values forEnums
:string
.flow-type
definitions value hasany
type:So, according to concept of Enum ...
names
from enum... I think, that if developer define complex value for enum, so it expects that get it in
args
forresolve
function. AndastFromValue
should not deeply extract AST forenum's value
it should stops onenum's name
.I don't know enough FB internals requirements for ENUMs, so can not provide correct PR for this problem. @leebyron please consider this issue, as soon as possible, because it blocking me.
PS. Passing to enum's value arrow functions, provides for me awesome ability to customise logic in
resolve
methods - https://github.com/nodkz/graphql-compose-connection/blob/master/src/resolvers/connectionResolver.js#L207The text was updated successfully, but these errors were encountered: