Skip to content

Convert arbitrary scalars to AST. #1815

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

Closed
anhldbk opened this issue Apr 7, 2019 · 3 comments
Closed

Convert arbitrary scalars to AST. #1815

anhldbk opened this issue Apr 7, 2019 · 3 comments
Labels

Comments

@anhldbk
Copy link

anhldbk commented Apr 7, 2019

Hi guys,

I'm trying to make a configuration language based on GraphQL (so I don't have write lexer/parser on my own). I want to support JSON as an input type by using graphql-type-json. But I've got an issue of parsing AST as described by this issue taion/graphql-type-json#73.

@taion tell me that

That utility function (astFromValue) doesn't support converting arbitrary scalars to AST

I've found a hack to overcome the issue. Should I make a PR against this one?

@IvanGoncharov
Copy link
Member

@anhldbk That's why we implemented valueFromASTUntyped

* Produces a JavaScript value given a GraphQL Value AST.
*
* Unlike `valueFromAST()`, no type is provided. The resulting JavaScript value
* will reflect the provided GraphQL value AST.
*
* | GraphQL Value | JavaScript Value |
* | -------------------- | ---------------- |
* | Input Object | Object |
* | List | Array |
* | Boolean | Boolean |
* | String / Enum | String |
* | Int / Float | Number |
* | Null | null |
*
*/
export function valueFromASTUntyped(
valueNode: ValueNode,
variables?: ?ObjMap<mixed>,
): mixed {

Moreover it's used by default:

this.parseLiteral = config.parseLiteral || valueFromASTUntyped;

So you don't need to use package for it just do:

 GraphQLScalarType({
  name: 'JSON',
  serialize: (value) => value,
});

@anhldbk
Copy link
Author

anhldbk commented Apr 8, 2019

@IvanGoncharov Thank you for your hint.

But would you please tell me how to properly parse JSON-enabled value into AST? 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

if (typeof serialized === 'object' ){
    return {
      kind: _kinds.Kind.OBJECT,
      value: serialized
    };
}

@IvanGoncharov
Copy link
Member

@anhldbk Yes, it looks like a bug. We should support scalars that are serialized to complex values.
Can you please open a separate issue for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants