Search code examples
websocketpostmanaws-appsync

Use Postman to test Appsync Subscription


I have been able to successfully execute Appsync GraphQL queries and mutations from Postman. However, i'm struggling to connect to subscriptions which are websocket urls.

How can I achieve the same ?


Solution

  • Since Postman supports WebSockets testing GraphQL subscriptions is achievable as well. Such a testing requires two steps:

    • connection to a server,
    • sending a start message.

    Establishing a connection:

    1. Create a new WebSocket request.
    2. Put your server URL ws:// or wss://.
    3. Add custom header parameter Sec-WebSocket-Protocol: graphql-ws. Other headers may depend on your server configuration.
    4. Press the "Connect" button.

    enter image description here


    When the connection is established we may start a subscription.

    1. In the "New message" field put the command.
    2. Press the "Send" button.

    The start message should look like this:

    {
      "id":"1",
      "payload": {
          "operationName": "MySubscription",
          "query": "subscription MySubscription {
              someSubscription {
                  __typename
                  someField1
                  someField2 {
                      __typename
                      someField21
                      someField22
                  }
              }
          }",
          "variables": null
      },
      "type": "start"
    }
    

    operationName is just the name of your subscription, I guess it's optional. And someSubscription must be a subscription type from your schema.

    query reminds regular GraphQL syntax with one difference:

    • __typename keyword precedes every field list.

    For example, the query from the payload in regular syntax looks like the following:

    subscription MySubscription {
      someSubscription {
            someField1
            someField2 {
                  someField21
                  someField22
            }
      }
    }
    

    Example message with parameters (variables):

    {
          "id":"1",
          "payload": {
               "operationName": "MySubscription",
               "query": "subscription MySubscription($param1: String!) {
                      someSubscription((param1: $param1)) {
                          __typename
                          someField
                      }
                }",
                "variables": {
                      "param1": "MyValue"
                }
          },
          "type": "start"
    }
    

    It also reminds regular GraphQL syntax as described above.

    variables is an object with your parameters.