Open anhldbk opened 5 years ago
@IvanGoncharov FYI
@anhldbk Thanks for detail report.
Also, note that isValidJSValue
is deprecated and will be removed in upcoming 15.0.0
:
https://github.com/graphql/graphql-js/blob/b8a0f3b9a9fc7057acb8e91327818b37f0eaf406/src/utilities/isValidJSValue.js#L14-L22
I have to patch astFromValue: https://github.com/graphql/graphql-js/blob/8aef229cb2/src/utilities/astFromValue.js#L139-L140 by adding following lines before L139
It's a little bit more complex, correct AST node for object value should contain fields
instead of value
:
https://github.com/graphql/graphql-js/blob/b8a0f3b9a9fc7057acb8e91327818b37f0eaf406/src/language/ast.js#L346-L350
I think correct solution would be introduce serializeAsLiteral
(opposite to parseLiteral
) and make it default to astFromValueUntypped(this.serialize(value))
(opposite to valueFromASTUntyped
).
@IvanGoncharov So when users define a custom scalar, they must provide a function named serializeAsLiteral
?
@anhldbk It's designed proposal for a fix. Idea is to have optional serializeAsLiteral
that if omitted equals to astFromValueUntypped(this.serialize(value))
.
I think this is a better fix:
if (typeof serialized === 'object') {
if (Array.isArray(serialized)) {
return {
kind: Kind.LIST,
values: serialized.map(v => astFromValue(v, type)),
};
}
return {
kind: Kind.OBJECT,
fields: Object.entries(serialized).map(([k, v]) => {
return {
kind: 'ObjectField',
name: { kind: 'Name', value: k },
value: astFromValue(v, type),
};
}),
};
}
I think we can keep Enum and GraphQLID logic in this function and move all the stuff regarding scalars (boolean, string, number and this fix also) to astFromValueUntyped
function.
@IvanGoncharov Do you want me to make PR for these changes?
just fyi: GraphQL Java will add a very similar method to the proposed serializeAsLiteral
. (cc @bbakerman)
Reporting issues with GraphQL.js
This issue is related to: https://github.com/graphql/graphql-js/issues/1815
Expected:
Code:
Result
Solution
I have to patch
astFromValue
: https://github.com/graphql/graphql-js/blob/8aef229cb2/src/utilities/astFromValue.js#L139-L140 by adding following lines before L139Question
The solution above may not be adequate. Would you please tell me it's worth to make a RP against this issue and how to properly resolve it?