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

[Feature Request] Add __typename to generated classes #151

Closed
kaioduarte opened this issue Nov 14, 2023 · 3 comments
Closed

[Feature Request] Add __typename to generated classes #151

kaioduarte opened this issue Nov 14, 2023 · 3 comments

Comments

@kaioduarte
Copy link

It would be great if the generated classes had the __typename field, which is needed when dealing with unions.

@gao-artur
Copy link
Contributor

Alternatively, there is a proposal (2) in the #126. For example, for the union called Assignee with two entries: Team and Employee generate an empty interface IAssignee and "implement" it on the Team and Employee classes. Then, the user can use pattern matching to find out what the real type behind the union. For example

public interface IAssignee { }

public class Team : IAssignee
{
    public string Name { get; set; }
}

public class Employee : IAssignee
{
    public string FullName { get; set; }
}

Usage:

var assignee = QueryAssignee();
var greeting = assignee switch
{
    Team team => $"Hello {team.Name}!",
    Employee employee => $"Hello {employee.FullName}!",
    _ => throw new ArgumentException($"Unexpected assignee type {assignee.GetType().Name}", nameof(assignee))
};

@Husqvik
Copy link
Owner

Husqvik commented Nov 19, 2023

I think it should deserialize into appropriate type just as interfaces do.

@Husqvik
Copy link
Owner

Husqvik commented Nov 20, 2023

I'll implement it during this week.

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

No branches or pull requests

3 participants