graphql-flutter icon indicating copy to clipboard operation
graphql-flutter copied to clipboard

Unauthorized

Open MagedAlNaamani opened this issue 2 years ago • 1 comments

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.

MagedAlNaamani avatar Sep 07 '22 08:09 MagedAlNaamani

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 avatar Sep 07 '22 21:09 vincenzopalazzo

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

vincenzopalazzo avatar Jan 05 '23 08:01 vincenzopalazzo