如何在本地sam中尝试AWS Appsync

时间:2019-10-09 18:57:44

标签: amazon-web-services graphql amazon-cloudformation aws-appsync aws-serverless

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

因为我读过这些示例,所有这些都是在具有dynamodb和Assembly的aws帐户上进行的,我想在没有dynamo且没有汇编的sam local上尝试。

所以我在这个仓库enter link description here上找到的template.yaml尝试了这种基本配置

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" 

        }),
    }

所以我感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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

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

SAM不支持。