stackery / demo-appsync-stock-trades

Demo AWS AppSync app built with Stackery

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setup

  1. Add GraphQL Api
    1. Update name to Trades Api
    2. Update schema location to trades/schema.graphql
    3. Paste the following schema:
      type Mutation {
        createTrade(type: TransactionType!, symbol: String!): Trade
      }
      
      type Price {
        symbol: String!
        price: Float!
      }
      
      type Query {
        getPrice(symbol: String!): Price
        getTrade(id: ID!): [Trade]
        listTrades(limit: Int, nextToken: String): TradesConnection
      }
      
      type Subscription {
        onCreateTrade(
          id: ID,
          symbol: String,
          timestamp: AWSDateTime,
          price: Float,
          type: String
        ): Trade
          @aws_subscribe(mutations: ["createTrade"])
      }
      
      type Trade {
        id: ID!
        type: TransactionType!
        symbol: String!
        timestamp: AWSDateTime!
      }
      
      type TradesConnection {
        items: [Trade]
        nextToken: String
      }
      
      enum TransactionType {
        BUY
        SELL
      }
    4. Attach resolvers to:
      • Query getPrice
      • Query getTrade
      • Query listTrades
      • Mutation createTrade
  2. Add an HTTP Proxy Endpoint
  3. Connect the Query getPrice resolver to the HTTP Proxy Endpoint
  4. Update the HTTP Proxy Endpoint Host to https://www.alphavantage.co
  5. Add a DynamoDB Table
  6. Connect the rest of the resolvers to it
  7. Update the DynamoDB Table Logical ID to Trades
  8. Edit Query getPrice resolver
    • Request Template:
      {
        "version": "2018-05-29",
        "method": "GET",
        "resourcePath": "/query",
        "params":{
          "query": {
            "function": "GLOBAL_QUOTE",
            "symbol": $util.toJson($ctx.args.symbol),
            "apikey": "demo"
          }
        }
      }
    • Response Template:
      #if($ctx.error)
        $util.error($ctx.error.message, $ctx.error.type)
      #end
      #if($ctx.result.statusCode == 200)
        {
          "symbol": $util.toJson($ctx.args.symbol),
          "price": $util.parseJson($ctx.result.body)["Global Quote"]["05. price"]
        }
      #else
        $utils.appendError($ctx.result.body, $ctx.result.statusCode)
      #end
  9. Edit Mutation createTrade
    • Request Template:
      {
        "version" : "2017-02-28",
        "operation" : "PutItem",
        "key" : {
          ## If object "id" should come from GraphQL arguments, change to $util.dynamodb.toDynamoDBJson($ctx.args.id)
          "id": $util.dynamodb.toDynamoDBJson($util.autoId()),
        },
        "attributeValues" : {
          "type": $util.dynamodb.toDynamoDBJson($ctx.args.type),
          "symbol": $util.dynamodb.toDynamoDBJson($context.arguments.symbol),
          "timestamp": $util.dynamodb.toDynamoDBJson($util.time.nowISO8601())
        }
      }
  10. Edit Query getTrade
    • Request Template:
      {
        "version": "2017-02-28",
        "operation": "GetItem",
        "key": {
          "id": $util.dynamodb.toDynamoDBJson($ctx.args.id),
        }
      }
  11. Edit Query listTrades
    • Request Template:
      {
        "version": "2017-02-28",
        "operation": "Scan",
        "limit": $util.defaultIfNull($ctx.args.limit, 20),
        "nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.nextToken, null)),
      }
  12. Deploy!

Queries

  • Get Price
    query getPrice {
      getPrice(symbol: "AMZN") {
        symbol
        price
      }
    }
  • Trade Subscription (in separate window)
    subscription onCreateTrade {
      onCreateTrade {
        id
        timestamp
        type
        symbol
      }
    }
  • Create Trade
    mutation createTrade {
      createTrade(type: BUY, symbol: "AMZN") {
        id
        timestamp
        type
        symbol
      }
    }
  • List Trades
    query listTrades {
      listTrades {
        items {
          id
          timestamp
          type
          symbol
        }
      }
    }

About

Demo AWS AppSync app built with Stackery