Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GraphQL] Error when trying to resolve fields (DateField, DateTimeField) #15718

Closed
lampersky opened this issue Apr 10, 2024 · 4 comments · Fixed by #15763
Closed

[GraphQL] Error when trying to resolve fields (DateField, DateTimeField) #15718

lampersky opened this issue Apr 10, 2024 · 4 comments · Fixed by #15763
Labels
Milestone

Comments

@lampersky
Copy link
Contributor

With latest version from main, I can't get value from DateField nor DateTimeField via GraphiQL editor.

repro steps:

  • create content type,
  • add DateField,
  • add DateTimeField,
  • try to query via GraphQL.

image

{
  "errors": [
    {
      "message": "Error trying to resolve field 'dateField'.",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "asdfasdfasdf",
        0,
        "dateField"
      ],
      "extensions": {
        "code": "INVALID_OPERATION",
        "codes": [
          "INVALID_OPERATION"
        ],
        "details": "GraphQL.Execution.UnhandledError: Error trying to resolve field 'dateField'.\r\n ---> System.InvalidOperationException: Unable to serialize '2024-04-10T00:00:00' value of type 'String' to the scalar type 'Date'.\r\n   at GraphQL.Types.ScalarGraphType.ThrowSerializationError(Object value) in /_/src/GraphQL/Types/Scalars/ScalarGraphType.cs:line 259\r\n   at GraphQL.Types.DateGraphType.Serialize(Object value) in /_/src/GraphQL/Types/Scalars/DateGraphType.cs:line 43\r\n   at GraphQL.Execution.ExecutionStrategy.CompleteNodeAsync(ExecutionContext context, ExecutionNode node)\r\n   --- End of inner exception stack trace ---"
      }
    },
    {
      "message": "Error trying to resolve field 'dateTimeField'.",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ],
      "path": [
        "asdfasdfasdf",
        0,
        "dateTimeField"
      ],
      "extensions": {
        "code": "INVALID_OPERATION",
        "codes": [
          "INVALID_OPERATION"
        ],
        "details": "GraphQL.Execution.UnhandledError: Error trying to resolve field 'dateTimeField'.\r\n ---> System.InvalidOperationException: Unable to serialize '2024-04-10T13:50:00Z' value of type 'String' to the scalar type 'DateTime'.\r\n   at GraphQL.Types.ScalarGraphType.ThrowSerializationError(Object value) in /_/src/GraphQL/Types/Scalars/ScalarGraphType.cs:line 259\r\n   at GraphQL.Types.DateTimeGraphType.Serialize(Object value) in /_/src/GraphQL/Types/Scalars/DateTimeGraphType.cs:line 65\r\n   at GraphQL.Execution.ExecutionStrategy.CompleteNodeAsync(ExecutionContext context, ExecutionNode node)\r\n   --- End of inner exception stack trace ---"
      }
    }
  ],
  "data": {
    "asdfasdfasdf": [
      {
        "dateField": null,
        "dateTimeField": null,
        "boolField": true,
        "textField": "hello world",
        "numericField": 1234
      }
    ]
  }
}
@lampersky
Copy link
Contributor Author

lampersky commented Apr 10, 2024

As a workaround two additional GraphTypes could be added and registered. But it would be better to find what has changed since the Newtonsoft era ;)
It looks like some additional serialization is made somewhere.

image

and DateField, DateTimeField is working again:

image

//cc: @sebastienros

@lampersky
Copy link
Contributor Author

The copyable version:

using GraphQL.Types;

namespace OrchardCore.Apis.GraphQL.Services;

public class MyDateTimeGraphType : DateTimeGraphType
{
    public MyDateTimeGraphType() : base()
    {
        Name = "DateTime";
    }

#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
    public override object? Serialize(object? value)
#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
    {
        if (value is string)
        {
            return value;
        }

        return base.Serialize(value);
    }
}

public class MyDateGraphType : DateGraphType
{
    public MyDateGraphType() : base()
    {
        Name = "Date";
    }

#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
    public override object? Serialize(object? value)
#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
    {
        if (value is string)
        {
            return value;
        }

        return base.Serialize(value);
    }
}


@gvkries
Copy link
Contributor

gvkries commented Apr 10, 2024

This happens because the ContentFieldsProvider uses dynamic typing. It looks like DateTime fields are deserialized correctly, but the dynamic value returned from the Content.Value property is now a string.

I think this can use static typing by casting the fields to the underlying type:

FieldAccessor = field => ((DateField)field).Value,

@lampersky
Copy link
Contributor Author

@gvkries I've just checked your suggestion and it works

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants