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

Duplicate generated classes #337

Closed
SharbelOkzan opened this issue Feb 6, 2024 · 3 comments
Closed

Duplicate generated classes #337

SharbelOkzan opened this issue Feb 6, 2024 · 3 comments

Comments

@SharbelOkzan
Copy link

A type in my schema looks like this

type Tournament {
  id: ID!
  // ...
  first_prize: Prize
  second_prize: Prize
  third_prize: Prize
  fourth_prize: Prize
// ... 
}

type Prize {
  id: ID!
  amount: Int
  currency: String
}

And the query is

query HomePageTournaments {
    tournaments {
        id
        first_prize {
            amount
            currency
        }
        second_prize {
            amount
            currency
        }
        third_prize {
            amount
            currency
        }
        fourth_prize {
            amount
            currency
        }
    }
}

The generated Dart class has the following fields:

  final String id;

  final Query$HomePageTournaments$tournaments$first_prize? first_prize;

  final Query$HomePageTournaments$tournaments$second_prize? second_prize;

  final Query$HomePageTournaments$tournaments$third_prize? third_prize;

  final Query$HomePageTournaments$tournaments$fourth_prize? fourth_prize;

While what's desired is

final String id;
Query$HomePageTournaments$tournaments$Prize? firstPrize;
Query$HomePageTournaments$tournaments$Prize? secondPrize;
Query$HomePageTournaments$tournaments$Prize? thridPrize;
Query$HomePageTournaments$tournaments$Prize? forthPrize;

In other words, the generated classes count should be the same as the types count in the schema.
Query$HomePageTournaments$tournaments$first_prize and Query$HomePageTournaments$tournaments$second_prize are the same thing.

Am I missing something or this is by design? Thanks in advance :)

Copy link

github-actions bot commented Feb 6, 2024

👋 @SharbelOkzan
Thank you for raising an issue. I will investigate the issue and get back to you as soon as possible.
Please make sure you have provided enough context.

This library is created and maintained by me, @budde377. Please consider supporting my work and ensure our survival by donating here.

@budde377
Copy link
Contributor

budde377 commented Feb 6, 2024

Thanks for raising this @SharbelOkzan. This is by design the subclasses are not optimised based on the type they are on or the field they select. If you want a single type for each of these, you can define a fragment

fragment Prize on Prize {
  amount
  currency
}

And then spread this in the sub selections

first_price { ... Prize }
...

This will generate a common dart type for each prize.

@SharbelOkzan
Copy link
Author

That makes sense.

And thanks for the quick response! Truly appreciate that.
Closing as completed.

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

2 participants