簡體   English   中英

AWS Appsync查詢解析器

[英]AWS appsync query resolver

目前,我將解析器作為lambda函數:

import boto3
from boto3.dynamodb.conditions import Key

def lambda_handler(event, context):

  list = []
  for device in event['source']['devices'] :
      dynamodb = boto3.resource('dynamodb')
      readings  = dynamodb.Table('readings')
      response = readings.query(
          KeyConditionExpression=Key('device').eq(device['device'])
      )
      items = response['Items']
      list.extend(items)
  return list

我希望能夠將其作為dynamodb上的VTL解析器。 我的問題是我的桌子有一個排序鍵 在此處輸入圖片說明

這意味着我不能使用批處理解析器來查詢一堆ID,因為我還需要提供排序鍵,而我只想通過主分區鍵來獲得所有結果。

如何使用VTL查詢一堆ID,基本上是在VTL中復制我的lambda函數。 這有可能嗎?

添加了架構,請原諒這是一個正在進行的工作,正在嘗試許多事情。 對graphQL還是很新的

type Device {
    id: String
    device: String!
}

input DeviceInput {
    id: String
   device: String!
}

type DeviceReadings {
   devices: [Device]
}

type Mutation {
    createDevice(input: DeviceInput): Device
}

type PaginatedDevices {
   devices: [Device]
   readings: [Reading]
   cows: [cow]
   nextToken: String
}

type Query {
    getAllUserDevices(nextToken: String, count: Int): PaginatedDevices
    getAllDeviceReadings: DeviceReadings
    getAllUserReadings: DeviceReadings
    getAllReadings(deviceId: String!): Readings  
    getCowReadings(cowId: String!): UserCowReadings
}

type Reading {
   device: String
   time: Int
   cow: Int
   battery: String
}

type Readings {
    items: [Reading]
}

type UserCowReadings {
    devices: [Device]
    readings: [Reading]
}

type cow {
    id: Int
    device: String
    nait: String
}  

schema {
    query: Query
    mutation: Mutation
}

是的,您可以執行此操作,但是您需要稍微調整一下架構。 在該lambda中,您實際上是在說“對每個設備執行DynamoDB查詢以獲取該設備的最新讀數”。 從概念上講,我會說設備有很多讀數 考慮到這一點,讓我們建立一個模式:

type Device {
  id: ID!
  name: String
  # Get the most recent readings for this device.
  # Do a Query where "device = $ctx.source.id"
  readings(limit: Int, nextToken: String): ReadingConnection 
}

type Reading {
  # Use the source's device id in the table to fetch the real device
  # GetItem where device = $ctx.source.device (and any sort key condition)
  device: Device 
  time: Int
  cow: Int
  battery: String
}

type ReadingConnection {
  items: [Reading]
  nextToken: String
}

type DeviceConnection {
  items: [Device]
  nextToken: String
}

type Query {
  getAllDevices(limit: Int, nextToken: String): DeviceConnection
}

然后,您可以分別瀏覽各個設備,並分別瀏覽各個設備的讀數:

query GetAllDevicesAndReadings {
  getAllDevices(first: 10) {
    items {
      id
      name
      readings(limit: 10) {
        time
        cow
        battery
      }
    }
  }
}

我建議使用AppSync控制台的解析器頁面中的下拉列表,以獲取有關可以使用解析器VTL來實現這些GetItem和Queries的更多信息。 這是一個很好的起點。 如果您在實施VTL時遇到問題,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM