Skip to content

Latest commit

 

History

History
89 lines (73 loc) · 4.15 KB

part-2-send-mutations-queries-subscriptions.md

File metadata and controls

89 lines (73 loc) · 4.15 KB

Testing the Cart service

Here we will be learning how to interact with the Cart service we created in the previous part of the example. Although we will be using the commands, entities, events, and read models created there, you can follow these steps with any other service you have created just by adapting the names and fields used in the requests.

After deploying your service successfully, Booster will have autogenerated a full-fledged GraphQL API based on your code. When the boost deploy command finishes, it will print useful information to interact with your system under the "Outputs" section. You will see something like this:

Outputs:
<application name>.httpURL = https://<API ID>.execute-api.<region>.amazonaws.com/<environment name>
<application-name>.websocketURL = wss://<API ID>.execute-api.<region>.amazonaws.com/<environment name>
# ... other outputs we are not interested in right now...

We will use the httpURL to send GraphQL queries and mutations (commands), while the websocketURL will be needed for subscriptions (as we need a two-way communication)

To do the following manual tests, we recommend you to use one of the several clients with full support for GraphQL, like Postwoman However, you can use any HTTP client capable of sending POST requests with a JSON body (like Postman) for queries and mutations, and any websocket client (like wscat) for subscriptions

Sending commands

The way we send a command to our Booster app is by using a GraphQL "mutation". The following GraphQL request is sending the command ChangeCart with all its required payload:

URL: <httpURL>/graphql GraphQL body:

mutation {
  ChangeCart(input: {
      cartId: "demo"
      sku: "ABC_01"
      quantity: 2
  })
}

In case you are using an HTTP client without support for GraphQL, you can do exactly the same by sending an HTTP request with method "POST" and include a JSON body with a field named "query" with the GraphQL mutation: URL: <httpURL>/graphql Method: POST Body:

{
   "query":"mutation { ChangeCart(input: { cartId: \"demo\" sku: \"ABC_01\" quantity: 2 }) }"
}

Reading read models data

To read a read model, you send a GraphQL "query" operation. Let's get the read model the previous command modified: URL: <httpURL>/graphql GraphQL body:

query {
  CartReadModel(id: "demo") {
      id
      items
  }
}

This will return the read model data associated to ID "demo". You can also do searches to filter by any property your read model has. Check the documentation to see all the available filters.

Subscribing to read models

Finally, you can also subscribe to your read models, allowing you to receive them in real time every time they are modified. To do that, you need to use a GraphQL "subscription" operation sent through the websocket API. We highly recommend using subscriptions to get your read models data and, thus, build your client application with a reactive approach.

Before sending any subscription, you first need to connect to the websocket.

If you are using "Postwoman" (the process is the pretty similar with the command line tool "wscat"), just go to the "Realtime" section, write your websocketURL, and click connect:

Connecting to the websocket using Postwoman

Once you are connected you can send the following subscription by writing the following in the input text box:

{"query": "subscription { CartReadModel(id:\"demo\") { id items } }" }

If you receive nothing as a response, then everything went good.

Now, to see this subscription in action, send some more commands like the one we sent before. After each command is processed, you should receive the updated read model in your Postwoman client:

Receiving read model in real time using Postwoman

Now, you are ready to add some event handler to build more complex use cases. Go to "Part 3: Adding event handlers"