Know if value is defined as Nullable or NotNullable when resolving custom scalar type

169 views Asked by At

I'm creating custom scalar types that override the built in Int and Float. I'd like to know whether the schema defines the value as nullable or not. I'd like to resolve an Int differently than Int! in my custom scalar type

What I want to do is within the resolver of the custom scalar type know whether the value is defined as Nullable or not in the schema so that I can resolve them differently and throw a custom error.

const resolvers = {
  // ...
  Int: new GraphQL.GraphQLScalarType ({
    name: 'Int',
    description: 'Custom Int type',
    serialize (val, isNullable) {

    },
    parseValue (val, isNullable) {
      // assume this parses the value
      const value = transit.read (val);

      // this does some type checking (using a library called sanctuary)
      if (S.is ($.Integer) (value)) {
        // if Nullable we want to return a maybe type
        if (isNullable) return S.Just (value);
        return value;
      }
      return isNullable ? S.Nothing : new Error ();
    },
    parseLiteral (ast) {

    },
  }),
}

The result for an Int (Nullable) would be of type Maybe Integer and the result of the Int! (Non-Nullable) would be of type Integer

0

There are 0 answers