graphql-client
graphql-client copied to clipboard
GraphQL AppSync support example.
Hi, is there any valid example of AppSync subscription??
I saw some closed issues regarding AppSync support but looks like the subscription is not working. Could you please provide working example for subscription web socket? The query and mutation works perfectly, I also do AWS Cognito auth by adding token in headers and this one is also working. The problem is only on subscription web socket. I made custom web socket connection from scratch and I was able to make it work but I would love to use one module for all my graphql operations on client side.
This one is not working:
var userJoinedRequest = new GraphQLRequest {
Query = @"
subscription {
userJoined{
displayName
id
}
}"
};
IObservable<GraphQLResponse<UserJoinedSubscriptionResult>> subscriptionStream
= client.CreateSubscriptionStream<UserJoinedSubscriptionResult>(userJoinedRequest);
var subscription = subscriptionStream.Subscribe(response =>
{
Console.WriteLine($"user '{response.Data.UserJoined.DisplayName}' joined")
});
Are you talking about graphq-client? This repo for graphql server bits.
https://github.com/bjorg/GraphQlAppSyncTest
https://github.com/bjorg/GraphQlAppSyncTest
This example is not working, the preprocessing method for web socket is not in the lib

The current version runs the PreprocessRequest method for queries, mutations AND subscriptions.
I'm no AppSync specialist (GraphQL != AppSync), but it appears that you have to add the authorization extension field to your subscription query (see the bottom of your screenshot)
Since a GraphQLRequest is a dictionary (see source) you can just add that like @bjorg did in his test project.
There are multiple ways to achieve this consistently, i.e.
- use a factory method for subscription requests
- use the
PreprocessRequestmethod, which adds this extension for queries and mutations, too (which is propably not intended) - ...
@MikePapinski I managed to get it to work only with a custom domain TBH not sure it would have worked before - as the header needs base64 encoded header inside the extensions but using the host, but before custom domains were available there is no way to make the domains the same. I suspect the error returned was a bit misleading.
If you enable custom domain add your API to it, then it should work. I still used the Preprocess method on the options as you do need to change the request format for WSS endpoint.
also add graphql/realtime to your websocket endpoint on the options passed to the client. If you follow this you should be OK
I will test it today later on, and provide results, thank you. It would be nice to have AppSync extension class of graphql client
PRs are welcome...
I mean, strictly speaking I'm not sure what could be added to the lib itself as this is a specific situation for a certain implementation. Perhaps a readme update is sufficient.
There'd no need to be an extension to the graphqlrequest and additional properties on the options to satisfy each of the auth scenarios to app sync.
Edit: no need for an extension