JSON fails to parse GraphQL response after flutter libs upgrade

Issue

I upgraded packages in pubspec.yaml and it looks like JSON-parsing for web responses is broken now. I migrated from graphql: ^3.1.0 to graphql: ^4.0.1 and I also use json_annotation: ^3.1.0.

Here is a good example of the response I get from http client and what I see when I try to get it as a string:
enter image description here

As you can see roles array is not empty and it’s 0 value is not null, but since I upgraded my libraries that’s what I get without changing any other code.

Here is my GraphQL query file:

query($pushCredential: TwilioPushCredential!) {
  user {
    id
    info {
      ...UserInfoFragment
    }
    roles {
      ... on Client {
        ...RoleUserFragment
        myFitnessPalId
      }
      ... on Coach {
        ...RoleUserFragment
        maxActiveClients
        inviteCode
      }
    }
    chatJwt(pushCredential: $pushCredential)
  }
}

fragment UserInfoFragment on UserInfo {
  email
  firstName
  lastName
  avatar
  phone
}

fragment RoleUserFragment on RoleUser {
  id
  role
}

Here is how I handle the response:

    final options = QueryOptions(
      document: get_user_info.document,
      variables: {'pushCredential': Platform.isIOS ? 'apn' : 'fcm'},
    );

    final result = await client.query(options);

    Logger.debug("getUser response: ${result.data['user']}");

It looks like this ... on Client and ... on Coach logic doesn’t work in the new GraphQL version. How to fix it and what am I doing wrong?

Solution

It is a bug related to caching, we found a workaround: https://github.com/zino-hofmann/graphql-flutter/issues/994#issuecomment-1000748910

This probably will be fixed soon, but I hope this answer will save some people from issues with fragments.

Answered By – asmodeoux

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published