Search code examples
amazon-web-servicesgraphqlaws-cloudformationaws-appsyncaws-serverless

How to try AWS Appsync in local sam


so i have a doubt, about to AppsSync and SAM, and is if i can run the api graphQl in local sam? if is so, what i need to do it, but, without dynamodb?, else i would like know which is the best practices to do a test or approach, on appSync.

because the examples that i read, all it's on aws account with dynamodb and assembly, and i want to try it on sam local without dynamo and without assembly.

so i try this basic configuration at the template.yaml, that i found, on this 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 ]

this is the schema.graphql

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

    friends: [Person!]!
}

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

schema {
    query: Query
}

and this is the lambda code:

 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" 
             
        }),
    }

so i thanks you, for your help.


Solution

  • There's only one way to test AppSync locally:

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

    SAM does not support it.