如何在本地 sam 中试用 AWS Appsync

How to try AWS Appsync in local sam

所以我对 AppsSync 和 SAM 有疑问,我是否可以 运行 本地 sam 中的 api graphQl?如果是这样,我需要做什么,但是,没有 dynamodb?,否则我想知道哪些是在 appSync 上进行测试或方法的最佳实践。

因为我阅读的示例,所有这些都是在 aws 帐户上使用 dynamodb 和程序集,我想在没有 dynamo 和程序集的情况下在 sam local 上尝试它。

所以我在 template.yaml 尝试了这个基本配置,我发现,在这个 repo enter link description here

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  graphql

  Sample SAM Template for graphql

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
plugins:
  - serverless-appsync-plugin
  - serverless-webpack
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.7
  
  Role: 
    Type: AWS::IAM::Role
    Properties: 
      AssumeRolePolicyDocument: 
        Version: 2012-10-17
        Statement: 
          - Effect: Allow
            Principal:
              Service: appsync.amazonaws.com
            Action:
              - sts:AssumeRole
      Policies: 
        - PolicyName: allow-access-to-lambda-from-appsync
          PolicyDocument: 
            Version: 2012-10-17
            Statement: 
              - Effect: Allow
                Action: lambda:invokeFunction
                Resource:
                  - !GetAtt [ HelloWorldFunction, Arn ]
                  - !Join [ '', [ !GetAtt [ HelloWorldFunction, Arn ], ':*' ] ]

  AppSyncAPI:
    Type: AWS::AppSync::GraphQLApi
    Properties:
      Name: !Join [ -, [ !Ref ParamProjectName, !Ref ParamENV ] ]
      AuthenticationType: API_KEY

  AppSyncSchema:
    Type: AWS::AppSync::GraphQLSchema
    Properties:
      ApiId: !GetAtt [ AppSyncAPI, ApiId ]
      DefinitionS3Location: schema.graphql

  AppSyncDataSource:
    Type: AWS::AppSync::DataSource
    Properties:
      ApiId: !GetAtt [ AppSyncAPI, ApiId ]
      Name: handler
      Type: AWS_LAMBDA
      LambdaConfig:
        LambdaFunctionArn: !GetAtt [ HelloWorldFunction, Arn ]
      ServiceRoleArn: !GetAtt [ Role, Arn ]



  AppSyncReolverPeople:
    Type: AWS::AppSync::reolver
    properties: 
      ApiId: !GetAtt [AppSyncAPI, ApiId]
      TypeName: Query
      FieldName: !GetAtt [AppSyncDataSource, Name]
      RequestMappingTemplate:
        '
        {
          "version":"2017-02-28",
          "operation": "Invoke",
          "payload":{
            "resolve":"query.people",
            "context":"$utils.toJson($context)"
          }
        }
        '
    ResponseMappingTemplate: $util.toJson($context.result)

  AppSyncAPIKey:
      Type: AWS::AppSync::ApiKey
      Properties:
        ApiId: !GetAtt [ AppSyncAPI, ApiId ]
        Expires: !Ref ParamKeyExpiration

Parameters:

  ParamProjectName:
    Type: String
  ParamENV:
    Type: String
  ParamKeyExpiration:
    Type: Number
     

Outputs:
  APIKey:
      Description: API Key
      Value: !GetAtt [ AppSyncAPIKey, ApiKey ]

  GraphQL:
    Description: GraphQL URL
    Value: !GetAtt [ AppSyncAPI, GraphQLUrl ]

这是schema.graphql

type Person {
    id: Int!
    name: String!
    age: Int!

    friends: [Person!]!
}

type Query {
    people: [Person!]!
    person(id: Int): Person!
}

schema {
    query: Query
}

这是 lambda 代码:

 import json




def lambda_handler(event, context):

    data={
        "id":1
        "name":"milse",
        "age":12
    }
    if event.name==data.name:
        return {
        "statusCode": 200,
        "body": json.dumps({
            "message": " hi :" + data.name
             
        }),
    }
    else:
        return {
        "statusCode": 200,
        "body": json.dumps({
            "message": " the name is not here" 
             
        }),
    }

谢谢你的帮助。

只有一种方法可以在本地测试 AppSync:

https://aws.amazon.com/blogs/mobile/amplify-framework-local-mocking/

SAM 不支持。