2

I ran into the issue where I wanted to use graphql-dgs-extended-scalars, specifically the JSON scalar, but had trouble finding a clear tutorial on how to do it. I'm sure they're out there, but just in case someone finds themselves in the same situation as me hopefully my simple explanation below will save them some time.

Davenporten
  • 323
  • 2
  • 13

1 Answers1

0

Installation

In pom file (maven specific)

<dependency>
    <groupId>com.netflix.graphql.dgs</groupId>
    <artifactId>graphql-dgs-extended-scalars</artifactId>
    <version>${netflix.graphql.dgs.version}</version>
</dependency>

In configuration file (eg, application.yml)

dgs:
  graphql:
    extensions:
      scalars:
        objects:
          enabled: true

Using the scalar (JSON in this case)

In the schema

...
 
type SomeType {
    thing: JSON!
}
 
...
 
input SomeTypeInput {
    thing: JSON!
}
 
...
 
scalar JSON

In the class that will be used as your input to the datafetcher

public class SomethingDTO
{
 
    /**
     * This does not have to be a Map, you could use another
     * reasonable object like JSONObject, etc.
     */
    public Map<String, Object> thing;
 
}

You can then pass in as an argument a json formatted object in your query of arbitrary shape.

This was just for the JSON scalar, but the pattern follows for most (all?) the other scalars found in the lib.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Davenporten
  • 323
  • 2
  • 13
  • What DGS version are you using? I am running 4.9.0 and the type converter tries (and fails) to convert the input to String – Simas Joneliunas Nov 21 '21 at 12:55
  • I’m using 4.5.1, so maybe I’m in for an unpleasant surprise. – Davenporten Nov 22 '21 at 14:58
  • Thanks for providing a version number. Ill try to test them later. Version upgrade forced me to match the GQL types to java types accurately (i.e. GQL String mapped to Java UUID before but not after upgrade), but as I dont use JSON field widely, it slipped through. I think its worth keeping it in mind when you decide to upgrade! – Simas Joneliunas Nov 23 '21 at 15:01
  • @SimasJoneliunas did you find any solution for this problem? – Amareswar Dec 14 '21 at 14:30
  • I ended up transforming all the JSONs to Strings and then manually handled deserialization in the code. THe time investment was not worth to dig further in my case. – Simas Joneliunas Dec 15 '21 at 18:54
  • @Davenporten what type does it end up converting the JSON to? – Archmede Oct 05 '22 at 20:43