javaspring-bootmavengraphqlnetflix-dgs

How to use Netflix DGS graphql-dgs-extended-scalars JSON scalar (java/spring-boot, maven)?


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.


Solution

  • 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.