question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Scalar Support

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
michaelstaibcommented, Jan 7, 2019

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

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

Changelog

  • Update the change log ([Unreleased] section)

Documentation

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom scalars - Apollo GraphQL Docs
Although these scalars cover the majority of use cases, some applications need to support other atomic data types (such as Date ) or...
Read more >
Custom scalars - Apollo GraphQL Docs
The GraphQL specification includes default scalar types Int , Float , String , Boolean , and ID . Although these scalars cover the...
Read more >
Support for custom scalars · Issue #585 - GitHub
After speaking with @glasser, I noticed that we don't currently have great support for custom scalar types on Apollo Client.
Read more >
Custom Scalars and Enums – GraphQL Tools
Add custom scalar and enum types to your graphql-tools generated schema. ... often you need to support custom atomic data types (e.g. Date), ......
Read more >
How to use GraphQL Custom Scalar Types in schema design
GraphQL supports some predefined scalar types, including Boolean, ID, Int, Float, and String, which can be directly used in the schema. However, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found