Hotchocolate: Custom Scalar Support

Created on 27 Dec 2018  路  9Comments  路  Source: ChilliCream/hotchocolate

Hi, I've been using the HotChocolate library for some time now and am getting around to trying out custom scalar types. When a scalar type is given a unique name such as "CustomLong", it can be registered just fine and used within my other types as input/output.

public class CustomLongType : ScalarType
    {
        public CustomLongType() : base("CustomLong")
        {

        }
        ...
    }

Via your documentation, it mentions the ability to swap out the built-in base Scalar types. I have tried to swap out your guys' long scalar type to be able to add my own custom behavior, but have not been able to do so. Defining my CustomLongType like such:

public class CustomLongType : ScalarType
    {
        public CustomLongType() : base("Long")
        {

        }
        ...
    }

This seems to fail to register the type correctly in TypeRegistry as the _namedTypes dictionary will pull back the built-in ScalarType for long {HotChocolate.Types.LongType}

private void TryUpdateNamedType(INamedType namedType)
{
    INamedType namedTypeRef = namedType;

    if (!_namedTypes.TryGetValue(namedType.Name, out namedTypeRef))
    {
        namedTypeRef = namedType;
        _namedTypes[namedTypeRef.Name] = namedTypeRef;
    }
        ...
}

Should there be something here to check for IsScalarType to swap with?

Is there a better way to do such swapping? Is this type of swap allowed? Let me know if I am doing something completely wrong!

bug

Most helpful comment

The scalar types are registered by the SchemaContextFactory. The schema context is the internal initialization context for the schema.

The factory has two functions that preregister scalar types (RegisterSpecScalarTypes and RegisterSpecScalarTypes).

All types that are preregistered by the SchemaContextFactory cannot be changed. This behavior is correct since we do not want people to meddle with the spec scalars or the introspection types.

What is wrong is to register the extended scalars here.

So my suggestion is to remove the RegisterExtendedScalarTypes from that class and create a new extensions method in SchemaConfigurationExtensions called RegisterExtendedScalarTypes that registers those types.

With that users of the API could register the package with one line of code and use HC like before or the could register just some of those types by manually registering them.

Tests

  • [x] Test that shows that the extended types are not registered without calling RegisterExtendedScalarTypes on ISchemaConfiguration
  • [x] Test that proves that custom scalars for can be registered.
  • [x] Test that proves that the spec scalars cannot be swapped out.
  • [x] Test that shows that all extended types are correctly registered when RegisterExtendedScalarTypes on ISchemaConfiguration is called.

Changelog

  • [x] Update the change log ([Unreleased] section)

Documentation

All 9 comments

Hi Scott,

I will have a look at that sometime tonight.

OK, got it already. This is a design flaw actually since we bundled the extended scalar types with the core scalar types.

The core scalar types are the ones that are defined by the spec and cannot be changed.

So, we will fix with the next version 0.7.0 which should arrive beginning of January. I will add this item to the 0.7.0 backlog.

We will make a preview build that includes this fix an notify you once it is ready.

Awesome thanks Michael for the quick response!

Hi Scott @scottrabara,

I will leave this issue open and close it once the fix is included in a preview build.

Hey Scott,

I will move this one to Version 0.8.0. In version 0.8.0 we have a lot of schema enhancements and I think this is a better fit for that release. 0.7.0 is more about middleware, execution engine and co.

The scalar types are registered by the SchemaContextFactory. The schema context is the internal initialization context for the schema.

The factory has two functions that preregister scalar types (RegisterSpecScalarTypes and RegisterSpecScalarTypes).

All types that are preregistered by the SchemaContextFactory cannot be changed. This behavior is correct since we do not want people to meddle with the spec scalars or the introspection types.

What is wrong is to register the extended scalars here.

So my suggestion is to remove the RegisterExtendedScalarTypes from that class and create a new extensions method in SchemaConfigurationExtensions called RegisterExtendedScalarTypes that registers those types.

With that users of the API could register the package with one line of code and use HC like before or the could register just some of those types by manually registering them.

Tests

  • [x] Test that shows that the extended types are not registered without calling RegisterExtendedScalarTypes on ISchemaConfiguration
  • [x] Test that proves that custom scalars for can be registered.
  • [x] Test that proves that the spec scalars cannot be swapped out.
  • [x] Test that shows that all extended types are correctly registered when RegisterExtendedScalarTypes on ISchemaConfiguration is called.

Changelog

  • [x] Update the change log ([Unreleased] section)

Documentation

@michaelstaib Documentation and changelog have been updated, please see PR's referencing this issue above.

@scottrabara I will close this one since everything is implemented.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

louisjrdev picture louisjrdev  路  3Comments

RohrerF picture RohrerF  路  3Comments

jbray1982 picture jbray1982  路  5Comments

IKolosynskyi picture IKolosynskyi  路  3Comments

PascalSenn picture PascalSenn  路  5Comments