is09-souzou / AppSync-Resolver-Mapping-Lambda

AWS Lambda
0 stars 0 forks source link

Migration to Lambda of listWorks query #13

Closed NozomiSugiyama closed 6 years ago

NozomiSugiyama commented 6 years ago

Migration to Lambda of listWorks query

Overview

検索エンジン実装のため listWorks query の構成を変更. 既存の実装であるAppSyncからResolver MappingにてDynamoDBから直接データを取得する方式から、AppSync-Resolver-Mapping-Lambdaをデータソースとするよう変更する必要がある.

Changes

AWS AppSync Resolver Mappingの変更. LambaにlistWorksの処理を追加.

NozomiSugiyama commented 6 years ago

AppSyncの変更完了

screen shot 2018-07-05 at 17 56 42
NozomiSugiyama commented 6 years ago

GraphQL schemaの変更.

listWorks(limit: Int, nextToken: ID, option: WorkQueryOption): WorkConnection

input WorkQueryOption {
    tags: [String]
    word: String
    userId: ID
}
NozomiSugiyama commented 6 years ago

WorkとUserモデル共に一覧取得にバグがありそう.

NozomiSugiyama commented 6 years ago

AppSyncのサンプルでPaginationを行ってるので見直したが

{
    "version": "2017-02-28",
    "operation": "Query",
    "index": "LSI-AppSyncCommentTable-by-eventId-createdAt",
    "query": {
        "expression": "eventId = :eventId",
        "expressionValues": {
            ":eventId": {
                "S": "$context.source.id"
            }
        }
    },
    "limit": #if($context.arguments.limit) $context.arguments.limit #else 10 #end,
    "nextToken": #if($context.arguments.nextToken) "$context.arguments.nextToken" #else null #end
}

こんな感じ.

NozomiSugiyama commented 6 years ago

https://docs.aws.amazon.com/cli/latest/reference/dynamodb/query.html

これを使ってみる?

NozomiSugiyama commented 6 years ago

https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Query.html#Query.Pagination

NozomiSugiyama commented 6 years ago

こんな記述を見つけた

// QueryRequest generates a "aws/request.Request" representing the
// client's request for the Query operation. The "output" return
// value will be populated with the request's response once the request completes
// successfuly.
//
// Use "Send" method on the returned Request to send the API call to the service.
// the "output" return value is not valid until after Send returns without error.
//
// See Query for more information on using the Query
// API call, and error handling.
//
// This method is useful when you want to inject custom logic or configuration
// into the SDK's request lifecycle. Such as custom headers, or retry logic.
//
//
//    // Example sending a request using the QueryRequest method.
//    req, resp := client.QueryRequest(params)
//
//    err := req.Send()
//    if err == nil { // resp is now filled
//        fmt.Println(resp)
//    }
//
// See also, https://docs.aws.amazon.com/goto/WebAPI/dynamodb-2012-08-10/Query
func (c *DynamoDB) QueryRequest(input *QueryInput) (req *request.Request, output *QueryOutput) {
    op := &request.Operation{
        Name:       opQuery,
        HTTPMethod: "POST",
        HTTPPath:   "/",
        Paginator: &request.Paginator{
            InputTokens:     []string{"ExclusiveStartKey"},
            OutputTokens:    []string{"LastEvaluatedKey"},
            LimitToken:      "Limit",
            TruncationToken: "",
        },
    }

    if input == nil {
        input = &QueryInput{}
    }

    output = &QueryOutput{}
    req = c.newRequest(op, input, output)
    return
}
NozomiSugiyama commented 6 years ago

Scanで行けそう

https://github.com/is09-souzou/AppSync-Resolver-Mapping-Lambda/commit/2d5bef03dab1a1c012a8657a91fedb64b704b287

screen shot 2018-07-07 at 9 26 46

現状nextTokenの仕組みが若干違くなってしまっているのでそこの修正ができれば...

NozomiSugiyama commented 6 years ago

この変更は破棄

    // var queryInput = &dynamodb.QueryInput{
    //  Limit:     &limit,
    //  TableName: aws.String(WorkTableName),
    //  KeyConditions: map[string]*dynamodb.Condition{
    //      "id": {
    //          // EQ | LE | LT | GE | GT | BEGINS_WITH | BETWEEN
    //          // ComparisonOperator: aws.String("EQ"),
    //          // AttributeValueList: []*dynamodb.AttributeValue{
    //          //  {
    //          //      S: aws.String("7cc9ebcc-79b2-11e8-b93a-0228c0ad61bd"),
    //          //  },
    //          // },
    //          ComparisonOperator: aws.String("NOT_NULL"),
    //      },
    //  },
    // }

    // if nextToken != nil {
    //  queryInput.ExclusiveStartKey = map[string]*dynamodb.AttributeValue{
    //      "id": {
    //          S: aws.String(*nextToken),
    //      },
    //  }
    // }

    // req, resp := svc.QueryRequest(queryInput)

    // err := req.Send()
NozomiSugiyama commented 6 years ago

limit nextTokenの形はやめて、limit exclusiveStartKeyにします。

NozomiSugiyama commented 6 years ago

↑ AppSyncの設定完了.

同時に全ての動作確認完了