Custom Scalar Support
See original GitHub issueHi, 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!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:9 (9 by maintainers)
Top GitHub Comments
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
andRegisterSpecScalarTypes
).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 inSchemaConfigurationExtensions
calledRegisterExtendedScalarTypes
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
onISchemaConfiguration
RegisterExtendedScalarTypes
onISchemaConfiguration
is called.Changelog
Documentation
We also have a slack channel for questions etc …
https://join.slack.com/t/hotchocolategraphql/shared_invite/enQtNTA4NjA0ODYwOTQ0LTBkZjNjZWIzMmNlZjQ5MDQyNDNjMmY3NzYzZjgyYTVmZDU2YjVmNDlhNjNlNTk2ZWRiYzIxMTkwYzA4ODA5Yzg