Scalars

Specification

Scalars are created as instances of ScalarType.

Built-in

These are provided as static properties on ScalarType.

  • ScalarType.Boolean
  • ScalarType.Int
  • ScalarType.ID
  • ScalarType.Float
  • ScalarType.String

In addition following non-null instances are provided for convenience.

  • ScalarType.NonNullBoolean
  • ScalarType.NonNullInt
  • ScalarType.NonNullID
  • ScalarType.NonNullFloat
  • ScalarType.NonNullString

Also standard collection is provided

Standard = new[]
        {
            String,
            Int,
            Float,
            Boolean,
            ID
        }

Custom scalars

Create instance of ScalarType and provide name, value converter and metadata.

Example:

Scalar:

ID = new ScalarType(
            "ID",
            new IdConverter(),
            "The ID scalar type represents a unique identifier, often used to refetch an object" +
            " or as the key for a cache. The ID type is serialized in the same way as a String; " +
            "however, it is not intended to be human‐readable. While it is often numeric, it " +
            "should always serialize as a String.")

Converter:

    public class IdConverter : IValueConverter
    {
        public object Serialize(object value)
        {
            if (value == null)
                return null;

            return Convert.ToString(value, CultureInfo.InvariantCulture);
        }

        public object ParseValue(object input)
        {
            if (input == null)
                return null;

            return Convert.ToString(input, CultureInfo.InvariantCulture);
        }

        public object ParseLiteral(GraphQLScalarValue input)
        {
            if (input.Kind == ASTNodeKind.NullValue)
            {
                return null;
            }

            if (input.Kind == ASTNodeKind.StringValue) 
                return input.Value;

            throw new FormatException(
                $"Cannot coerce Long value from '{input.Kind}'");
        }
    }