grooviter / gql

Groovy GraphQL library
http://grooviter.github.io/gql/
Apache License 2.0
49 stars 6 forks source link

Add custom scalar definition in DSL #5

Closed mariogarcia closed 7 years ago

mariogarcia commented 7 years ago

Imagine we create a scalar type Currency which basically parses a custom string to a specific type, if we take a look at graphql-java we could come up with something like this:

public static GraphQLScalarType GraphQLCurrency = new GraphQLScalarType("Currency", "Built-in Currency", new Coercing<Currency, Currency>() {
        @Override
        public Currency serialize(Object input) {
          //
        }

        @Override
        public Currency parseValue(Object input) {
            //
        }

        @Override
        public Currency parseLiteral(Object input) {
          //
        }
    });

But at the moment there is no way to add that information to the type DSL. It would be nice to be able to do it, something like:

DSL.scalar('Currency', 'built in Currency') {
  /**
   * Called to convert a result of a DataFetcher to a valid runtime value.
   *
   * @param input is never null
   * @return null if not possible/invalid
   */
  serialize { input ->
    //
  }
  /**
   * Called to resolve a input from a variable.
   * Null if not possible.
   *
   * @param input is never null
   * @return null if not possible/invalid
   */
  parseValue { input ->
    //
  }
  /**
   * Called to convert a AST node
   *
   * @param input is never null
   * @return null if not possible/invalid
   */
  parseLiteral { input ->
    //
  }
}

Basically the Coercing interface defines three methods and they're grouped in those which return an output and those that return something used as input:

I'm not sure how the last two are triggered, I need to do some tests.