4

So I want to implement my GraphQL client so it can use multiple endpoints using graphql_flutter package. I somehow made it work by passing operationName parameter in QueryOptions of Query widget

Query(
          options:
              QueryOptions(document: gql(queryName), operationName: 'apiUrl'))

and created method called getLink() in my GraphQL config class that returns httpLink with given condition (if operationName is 'apiUrl' returns httpLink if not return anotherHttpLink):

class Config {
  final HttpLink httpLink = HttpLink(
    apiUrl,
  );

  final HttpLink anotherHttpLink = HttpLink(
    anotherApiUrl,
  );
  Link getLink() {
    return Link.split((request) {
      return request.operation.operationName == 'apiUrl';
    }, httpLink, anotherHttpLink);
  }

  ValueNotifier<GraphQLClient> initializeClient() {
    ValueNotifier<GraphQLClient> client = ValueNotifier(
      GraphQLClient(
        cache: GraphQLCache(store: HiveStore()),
        link: getLink(),
      ),
    );
    return client;
  }
}

Is there any other solution to this problem? Because this kind of looks like a hack to me and yes sorry for the naming of variables it is just for this example.

Karlo123
  • 41
  • 2

0 Answers0