Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL subscription error missing header #662

Open
kiennt300599 opened this issue Oct 25, 2024 · 3 comments
Open

GraphQL subscription error missing header #662

kiennt300599 opened this issue Oct 25, 2024 · 3 comments

Comments

@kiennt300599
Copy link

I am trying to use AppSync for my project. Although I have handled everything according to the design documentation, I keep encountering the error:
{"payload":{"errors":[{"message":"Required headers are missing.","errorCode":400}]},"type":"connection_error"}
every time I create a subscription to listen. Is this a bug, or am I missing something? Please help me resolve this issue.

public static class AppSyncService
{
private static GraphQLHttpClient _client;
private static IDisposable _subscription;
private static string _apiUrl;
private static string _wssUrl;
private static string _apiKey;
private static string _serverID;
public static event Action OnNewItemReceived;

  public static void Initialize(string apiUrl, string wssUrl, string apiKey, string serverID)
  {
      _apiUrl = apiUrl;
      _wssUrl = wssUrl;
      _apiKey = apiKey;
      _serverID = serverID;

      ConnectToAppSync();
  }

  private static void ConnectToAppSync()
  {
      var options = new GraphQLHttpClientOptions
      {
          EndPoint = new Uri(_apiUrl),
          WebSocketEndPoint = new Uri(_wssUrl),
          WebSocketProtocol = "graphql-ws",
          MediaType = "application/json",
      };

      _client = new GraphQLHttpClient(options, new NewtonsoftJsonSerializer());
      _client.HttpClient.DefaultRequestHeaders.Add("x-api-key", _apiKey);

      Subscribe(_serverID);
  }

  private static void Subscribe(object variables)
  {
      try
      {
          var request = new GraphQLRequest
          {
              Query = @"
              subscription MySubscription($room_id: String!) {
                  onCreateMyChatMessageModel(room_id: $room_id) {
                      display_name
                      message_body
                      message_type
                      msg_id
                      room_id
                      timestamp
                      timestamp_msg_id
                      user_id
                  }
              }",
              Variables = variables
          };

          var subscriptionStream = _client.CreateSubscriptionStream<string>(request);

          _subscription = subscriptionStream.Subscribe(
          source =>
          {
              var newItem = source.Data;
              Debug.Log($"AppSync New item created: {newItem}");

              OnNewItemReceived?.Invoke(newItem);
          },
          error => Debug.Log($"AppSync error: {error.Message}"),

          () => Debug.Log("AppSync subscription completed."));
      }
      catch (Exception e)
      {
          Debug.Log($"AppSync subscribe error {e}");
      }
  }
  private static void Dispose()
  {
      _subscription?.Dispose();
      _client.Dispose();
      Debug.Log("AppSync subscription and client disposed.");
  }

}

@rose-a
Copy link
Collaborator

rose-a commented Oct 25, 2024

To add headers to the websocket connection, you need to configure them via GraphQLHttpClientOptions.ConfigureWebsocketOptions

@kiennt300599
Copy link
Author

@rose-a
Thanks! I configured it following ConfigureWebsocketOptions, and now there are no more errors. However, I'm still not receiving any data from the websocket. So, I logged _subscription after Subscribe, and I received the following error:

System.Reactive.Linq.ObservableImpl.SelectMany2+ObservableSelector+_[System.Tuple2[GraphQL.GraphQLResponse1[System.String],System.Exception],GraphQL.GraphQLResponse1[System.String]]

Do you know what might be causing this? Please give me some ideas to resolve it.

@rose-a
Copy link
Collaborator

rose-a commented Oct 29, 2024

I've got no experience using AppSync myself, but there are people who got it working...

Perhaps this thread can help you: #377

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants