question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON Scalar best practice?

See original GitHub issue

We’re using GraphQL to implement a custom JSON scalar that maps internally to a Jackson JsonNode.

What is the best practice for doing this? The challenge we’re facing is the best way to turn an ObjectValue into a Map<String,Object> or a JsonNode for parsing inputs to mutations.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
bbakermancommented, Oct 3, 2018
2reactions
loganvolkerscommented, Oct 11, 2017

So we copied the approach from GraphQL.js.

  • Hopefully this helps other people implement custom scalars
  • Or the GraphQL.java maintainers tell us this is the wrong approach.
    static JsonNode valueToJson(Value v) {
        if (v instanceof BooleanValue) {
            return JsonNodeFactory.instance.booleanNode(BooleanValue.class.cast(v).isValue());
        } else if (v instanceof EnumValue) {
            return JsonNodeFactory.instance.textNode(EnumValue.class.cast(v).getName());
        } else if (v instanceof FloatValue) {
            return JsonNodeFactory.instance.numberNode(FloatValue.class.cast(v).getValue());
        } else if (v instanceof IntValue) {
            return JsonNodeFactory.instance.numberNode(IntValue.class.cast(v).getValue());
        } else if (v instanceof NullValue) {
            return JsonNodeFactory.instance.nullNode();
        } else if (v instanceof StringValue) {
            return JsonNodeFactory.instance.textNode(StringValue.class.cast(v).getValue());
        } else if (v instanceof ArrayValue) {
            final ArrayNode result = JsonNodeFactory.instance.arrayNode();
            for (final Value v2 : ArrayValue.class.cast(v).getValues()) {
                result.add(valueToJson(v2));
            }
            return result;
        } else if (v instanceof ObjectValue) {
            final ObjectNode result = JsonNodeFactory.instance.objectNode();
            for (final ObjectField f : ObjectValue.class.cast(v).getObjectFields()) {
                result.set(f.getName(), valueToJson(f.getValue()));
            }
            return result;
        } else {
            throw new IllegalStateException(String.format("Unsupported value type: %s", v.getClass()));
        }
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom scalars - Apollo GraphQL Docs
The serialize method converts the scalar's back-end representation to a JSON-compatible format so Apollo Server can include it in an operation response.
Read more >
The Complete Guide to Working With JSON - Nylas
In this section, we'll suggest some best practices to avoid common JSON errors: Always enclose the key, value pair within double quotes.
Read more >
Extract scalar values from JSON data using JSON_VALUE()
In this article, we will explore JSON_VALUE() function in SQL Server to extract scalar values from JSON data.
Read more >
Best Practices in Designing JSON Document Model in ...
Best Practices in Designing JSON Document Model in Autonomous JSON DatabaseSlide Deck: http://bit.ly/ajd-slidedeck-210330This webinar ...
Read more >
Working with JSON data in Google Standard SQL | BigQuery
On this page · Overview · Limitations · Create a table with a JSON column · Create JSON values. Create a JSON literal;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found