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!
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.
We also have a slack channel for questions etc ...
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
RegisterExtendedScalarTypes on ISchemaConfigurationRegisterExtendedScalarTypes on ISchemaConfiguration is called.Changelog
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.
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 (
RegisterSpecScalarTypesandRegisterSpecScalarTypes).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
RegisterExtendedScalarTypesfrom that class and create a new extensions method inSchemaConfigurationExtensionscalledRegisterExtendedScalarTypesthat 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
RegisterExtendedScalarTypesonISchemaConfigurationRegisterExtendedScalarTypesonISchemaConfigurationis called.Changelog
Documentation
Maybe we should rename that section to scalar types and then state what are the spec types (table) show what extended types we provide and how they can be registered.
https://github.com/ChilliCream/hotchocolate-docs