Custom scalar for JObject is not parsed correctly
See original GitHub issueDescribe the bug
I want to write a custom scalar for the JSON object of any shape (I know I know about strong typing and GraphQL but…) and when I do this hotchocolate incorrectly parses the input object.
All works fine if you use this scalar type as mutation argument but doesn’t work when it’s a field of InputObjectType.
To Reproduce Steps to reproduce the behavior:
- Create custom scalar for JSON object
Scalar Implementation
public class JsonScalarType : HotChocolate.Types.ScalarType
{
public JsonScalarType() : base("JsonScalar")
{
}
protected JsonScalarType(string name) : base(name)
{
}
public override Type ClrType => typeof(object);
public override bool IsInstanceOfType(IValueNode literal)
{
if (literal == null) throw new ArgumentNullException(nameof(literal));
return literal is NullValueNode || literal is ObjectValueNode;
}
public override object ParseLiteral(IValueNode literal)
{
if (literal is NullValueNode) // NullValueNode is present after TryDeserialize call
return null;
throw new NotImplementedException();
}
public override IValueNode ParseValue(object value)
{
throw new NotImplementedException();
}
public override object Serialize(object value)
{
if (!(value is JObject jo))
throw new ArgumentException(nameof(value));
return jo;
}
public override bool TryDeserialize(object serialized, out object value)
{
if (serialized is Dictionary<string, object>)
{
value = JObject.FromObject(serialized);
return true;
}
if (serialized is JObject)
{
value = serialized;
return true;
}
value = null;
return false;
}
}
Input Object Type
public class ComplexInput
{
[GraphQLType(typeof(JsonScalarType))]
public JObject Inner { get; set; }
}
Mutation
public class Mutation {
public string ProcessJsonScalar([GraphQLType(typeof(JsonScalarType))] JObject input)
{
return $"ok: {input}";
}
public string ProcessComplexJsonScalar(ComplexInput input)
{
return $"ok: {input.Inner}";
}
}
- send mutation
mutation send($input: ComplexInput) {
processComplexJsonScalar(input: $input)
}
Variables:
{
"input": {
"inner": {
"someCustomProp": 1,
"someCustomProp2": 2
}
}
}
Expected behavior
Custom scalar is correctly parsed into JObject
Actual behavior:
null
is returned
Additional context .NET Core 2.2 hotchocolate version 11.0.0-preview.2
Issue Analytics
- State:
- Created 4 years ago
- Comments:21 (14 by maintainers)
Top Results From Across the Web
Custom scalar for JObject is not parsed correctly #1051
Describe the bug I want to write a custom scalar for the JSON object of any shape (I know I know about strong...
Read more >JObject Parse not able to parse String with JSON type
In this method, a JSON String will be returned from GetDataSetColumns() which called API from a Signage System(Xibo) to get dataset. And I...
Read more >Parsing JSONObject custom scalar with Apollo iOS v1 - Help
We have a custom scalar named JSONObject: The JSONObject scalar type represents JSON objects as specified by ECMA-404. Working with Apollo iOS ...
Read more >Custom scalars - Apollo GraphQL Docs
Defining a custom scalar You can now use MyCustomScalar in your schema anywhere you can use a default scalar (e.g., as the type...
Read more >How to parse custom scalars with graphql-codegen?
I'm trying to use the custom scalar BigInt from graphql-scalars but I can't figure out what the correct value should be for the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have talked to Ivan about this (the graphql-js maintainer) and it is not forbidding to do that. I had interpreted some sections of the spec differently but I was wrong here.
We will create the new AnyType so that it really can be anything (object, int, float, string, bool).
I will describe the AnyType in more detail in a separate issue that I will link to this issue.
We will put this in 10.1.0 so expect it in the next two weeks.
So, we will build in a proper any type with 10.1