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 623 forks source link

Unauthorized #1226

Closed MagedAlNaamani closed 1 year ago

MagedAlNaamani commented 2 years ago

Hi

I am trying to add access token but I am not sure what is the right way using this package?

Here is my code. Can someone tell me where the mistake?

First way:

  static AuthLink authLink = AuthLink(getToken: () async {
    return 'Bearer ${accessToken}';
  });

static WebSocketLink realtimeWebsocketLink = WebSocketLink(
    'wss://pdns-realtime-app.company.xyz/graphql',
    config: SocketClientConfig(
      inactivityTimeout: const Duration(seconds: 25),
    ),
    subProtocol: GraphQLProtocol.graphqlTransportWs,
  );

  static Link realtimeLink = authLink.concat(realtimeWebsocketLink);

  final realtimeClient = ValueNotifier<GraphQLClient>(
    GraphQLClient(
      cache: GraphQLCache(),
      link: realtimeLink,
    ),
  );

Second way:

  static WebSocketLink realtimeWebsocketLink = WebSocketLink(
    'wss://pdns-realtime-app.company.xyz/graphql',
    config: SocketClientConfig(
      initialPayload: () async {
        return {
          "headers": {"Authorization": 'Bearer ${accessToken}'},
        };
      },
      inactivityTimeout: const Duration(seconds: 25),
    ),
    subProtocol: GraphQLProtocol.graphqlTransportWs,
  );

  final realtimeClient = ValueNotifier<GraphQLClient>(
    GraphQLClient(
      cache: GraphQLCache(),
      link: realtimeWebsocketLink,
    ),
  );

Third way:

  static AuthLink authLink = AuthLink(getToken: () async {
    return 'Bearer ${accessToken}';
  });

static WebSocketLink realtimeWebsocketLink = WebSocketLink(
    'wss://pdns-realtime-app.company.xyz/graphql',
    config: SocketClientConfig(
      initialPayload: () async {
        return {
          "headers": {"Authorization": 'Bearer ${accessToken}'},
        };
      },
      inactivityTimeout: const Duration(seconds: 25),
    ),
    subProtocol: GraphQLProtocol.graphqlTransportWs,
  );

static Link realtimeLink = authLink.concat(realtimeWebsocketLink);

  final realtimeClient = ValueNotifier<GraphQLClient>(
    GraphQLClient(
      cache: GraphQLCache(),
      link: realtimeLink,
    ),
  );

All of these way not work. I debug the token is reach to all the ways.

vincenzopalazzo commented 2 years ago

Where you are receiving the error, in the was or in the HTTP client?

But in all your example I see a wrong usage of the link, or a usage that I did not know.

I use somethings like

  final Link link = Link.split(
    (request) => request.isSubscription,
    wsLink,
    httpLink,
  );
vincenzopalazzo commented 1 year ago

closing for lack of reproducible, feel free to reopen it if you find a good example