zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.25k stars 620 forks source link

there is a error when upgrade the package to the 5.1.1 #1109

Closed Refaat2020 closed 2 years ago

Refaat2020 commented 2 years ago

my previous version 3.10 was working fine but when updated to the current version it returns an error

OperationException(linkException: ServerException(originalException: null, parsedResponse: Response(data: null, errors: null, context: Context({ResponseExtensions: Instance of 'ResponseExtensions'}), response {error: Unauthorized, message: Access token invalid})), graphqlErrors: [])

budde377 commented 2 years ago

I would expect upgrading two major versions may break your app. It looks like the error is pretty obvious from your error message, try and have a look at how your links are set up and if the authorisation tokens are properly passed.

Refaat2020 commented 2 years ago

Please I couldn't find the error so please have a look at my code and tell me where the error could be found https://i.ibb.co/mcvVMPc/screen-1.png

https://i.ibb.co/Y36sZgf/screen2.png

Refaat2020 commented 2 years ago

@budde377 class GraphQLService { late GraphQLClient _client; GraphQLService() { initClient(); } // Setup the apollo client on initialize initClient() { try {

  String serverUrl = AppUrls.getServerUrl(appFlavor);
  Logger().i("Server Url: $serverUrl");

  final HttpLink _httpLink = HttpLink("$serverUrl/graphql");
  final token = Preference.getString(PrefKeys.accessToken) ?? "";
  Logger().i("graph token: $token");
  //
  final AuthLink _authLink =
      AuthLink(getToken: () async => Preference.getString(PrefKeys.accessToken));
  Link _link = _authLink.concat(_httpLink);
  final GraphQLClient client = GraphQLClient(cache: GraphQLCache(store: InMemoryStore()), link: _link,);
  // readRepositories(client);
  return this._client = client;
} catch (e) {
  print(e);
  print('error initializing graphql');
}

}

/// Use this for cached queries Future query({required String query, Map<String, dynamic> ?variables}) async { final QueryOptions options = QueryOptions( document: gql(query), variables: variables??{}, fetchPolicy: FetchPolicy.networkOnly, );

final QueryResult result = await _client.query(options);

return result.handleResponse();

}

/// Use this for mutations Future mutate({required String mutation, required Map<String, dynamic> variables}) async { final MutationOptions options = MutationOptions( fetchPolicy: FetchPolicy.networkOnly, document: gql(mutation), variables: variables, ); final QueryResult result = await _client.mutate(options);

return result.handleResponse();

}

}

budde377 commented 2 years ago

I don't know what kind of token your API requires. Maybe you'll need to prefix your token with Bearer?

I.e.,

      AuthLink(getToken: () async => "Bearer ${Preference.getString(PrefKeys.accessToken)}");
vincenzopalazzo commented 2 years ago

Please I couldn't find the error so please have a look at my code and tell me where the error could be found

@Refaat2020 You are 2 years late, that with software is history! What we suggest is try to follow https://github.com/zino-hofmann/graphql-flutter/blob/main/changelog-v3-v4.md that is easily found in the repository.

If the error persists, take an empty project and start a graph_flutter project from scratch. debugging your code with difference between the new and old version is an easy job more than you are asking to us

Davete0302 commented 2 years ago

Have you found a solution?