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

Can not map enums when using GraphQLFragment, no errors when building query #99

Closed
everscope opened this issue Apr 9, 2024 · 6 comments · Fixed by #101
Closed

Can not map enums when using GraphQLFragment, no errors when building query #99

everscope opened this issue Apr 9, 2024 · 6 comments · Fixed by #101
Labels
bug Something isn't working

Comments

@everscope
Copy link

everscope commented Apr 9, 2024

Describe the bug

When using GraphQLFragment, it is unclear how Enums should be mapped
Also when doing this, there is no exception when sending or building query - query will be build, but won't select any fields that will lead to exception on GraphQL server.
Am I missing something or this is not possible?

How to Reproduce

Code example, simple query towards GraphQL api and mapping result to read model using fragments (see graphQL schema below)

    public class Person 
    {
         ... other fileds ...
        public MyOwnStateEnum MyOwnEnumType { get; set; }
    }

    public enum MyOwnStateEnum 
    {
      STATE1
      STATE2
      ...
    }

    [GraphQLFragment]
    public static PersonReadModel ToPersonReadModel(
        this Person p) => new()
        {
              ... other fileds ...
              //this one does not work
              State = (MyOwnStateEnum)p.State,
              // this aslo does not work
              //State = (MyOwnStateEnum) Enum.Parse(typeof(MyOwnStateEnum), p.State.Tostring());,
        };


    var response = await _myGraphQlService
        .Query(variables, static (i, q) =>
            q.Persons(
                where: i.Where,
                selector: n => n.ToPersonReadModel()
            )
        );

Query: query ($where: PersonFilterInput!) { persons(where: $where) { } } } (query does not select any fields)
After removing Enum convertion everything works fine (all other fields are selected)
Then this query obviously returns error when running it (as you can not have query which is not selecting any fields)

Expected behavior

Ability to map enum and/or exception message saying that query is impossible to build
Today, if you use same query (with type casting) inside selector, it will be recognized as error.

The GraphqQL schema that fails

If applicable, there is no need to post the whole schema, but having the small part that causes the issue would be excellent.

type Person {
  state: PersonStateDto
...other fields...
}

enum PersonStateDto {
  STATE1
  STATE2
  ...
  ...
}

Environment (please complete the following information):

  • Nuget package version [6.1.1]
  • IDE: [VS]
  • .Net Version [8]
@everscope everscope added the bug Something isn't working label Apr 9, 2024
@byme8
Copy link
Owner

byme8 commented Apr 10, 2024

I have tried to reproduce the issue with ZeroQL 6.1.1 and for me works as expected
image

@byme8
Copy link
Owner

byme8 commented Apr 10, 2024

I am using Microsoft Visual Studio Enterprise 2022 (64-bit) Version 17.9.5

@everscope
Copy link
Author

everscope commented Apr 10, 2024

I am using Visual Studio Community 2022 17.9.6 64-bit

I checked again, it looks like it works perfectly fine if you have only one invalid cast
image

but if you add another fields, it is not highlighted as error
image

and same happens if you have 2 casts: only first one will be counted as error
image
image

And do I understand correctly, there is no way to cast enums inside fragments, right?

@everscope
Copy link
Author

everscope commented Apr 11, 2024

Tested in Microsoft Visual Studio Enterprise 2022 (64-bit) (v. 17.9.6) with same result

@byme8
Copy link
Owner

byme8 commented Apr 12, 2024

if you add another fields, it is not highlighted as error

I managed to reproduce this case. It should report the error.

And do I understand correctly, there is no way to cast enums inside fragments, right?

The fragment should not be treated as a C# function. It is more like GraphQL query declaration powered by C# syntax. It is analyzed at compile time to bake everything into proper raw GraphQL query.

At the moment, the source generator supports the limited C# syntax tree to simplify the implementation and improve performance.
I am think that it would be fine to support syntax like (UserKind2)user.UserKind. However Enum.Parse it is too much.

@byme8
Copy link
Owner

byme8 commented Apr 14, 2024

Checkout v6.2.1-preview.1.

The missing error message has been fixed in v6.2.0. The v6.2.1-preview.1 adds support for casting enums.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants