nyphur
nyphur

Reputation: 2866

In my flutter project, how do I get my code-generated graphql hooks synchronized with my generated freezed models?

I'm working with Flutter and using the following libraries:

I've successfully set up schema generation using flutter-freezed following this guide. When I run build_runner, it generates queries and mutations that can be used as hooks.

However, I'm facing an issue where the queries and mutations are not synchronized with the models generated by the schema.

Here’s an example of my setup:

GraphQL Query:

query FetchChallenges($filters: [[FilterInput!]!]) {
  challenges(filters: $filters) {
    data {
      ...ChallengeFragment
      userChallenges {
        id
        enrolledAt
        completedAt
        challenge {
          id
        }
      }
    }
  }
}

Generated Hook

final fetchChallengesQuery = useQuery$FetchChallenges(
      Options$Query$FetchChallenges(
        fetchPolicy: FetchPolicy.networkOnly,
      ),
    );

final challenges = fetchChallengesQuery.result.parsedData?.challenges?.data;

and the Model is

class Query$FetchChallenges$challenges$data implements Fragment$ChallengeFragment {
  // Fields and methods...
}

Generated freezed model

@Freezed(
  copyWith: true,
  makeCollectionsUnmodifiable: true,
)
class Challenge with _$Challenge {
  // Fields and methods...
}

Problem

I want to convert the list of Query$FetchChallenges$challenges$data into a list of Challenge instances. I tried doing this with the following code:

final response = fetchChallengesQuery.result.parsedData?.challenges?.data;

final challenges = response?.map((queryChallenge) => Challenge.fromJson(queryChallenge.toJson())).toList();

However, I’m encountering issues with enum compatibility and other type mismatches.

How can I synchronize the Query$FetchChallenges$challenges$data with the freezed Challenge model? Specifically, how can I properly convert or map the generated query types to the Freezed models, given that there are issues with enum values and other type mismatches?

OR is there anything I can do in my build runner that can properly map the queries to the freezed models?

Any help or suggestions would be greatly appreciated!

Upvotes: 0

Views: 18

Answers (0)

Related Questions