Giter Site home page Giter Site logo

sbstjn / appsync-resolvers Goto Github PK

View Code? Open in Web Editor NEW
42.0 4.0 13.0 99 KB

AWS AppSync Resolvers for GraphQL using AWS Lambda functions in Go.

Home Page: https://sbstjn.com/serverless-graphql-with-appsync-and-lambda.html

License: MIT License

Makefile 2.79% Go 97.21%
aws appsync graphql lambda router go golang appsync-router

appsync-resolvers's Introduction

AppSync GraphQL Resolvers w/ Go in AWS Lambda

Current Release MIT License Read Tutorial Code Example

Easily create AWS AppSync resolvers with AWS Lambda using Go. See appsync-resolvers-example for an example project with custon Field and Query resolvers and how to set up, maintain, and deploy a working GraphQL API using the Serverless Application Model and without any third-party frameworks.

See Serverless GraphQL with AWS AppSync and Lambda on sbstjn.com for a detailed guide how to set up and configure this project. Or just run make configure build package deploy and you are ready to go โ€ฆ

Usage

Installation

$ > go get github.com/sbstjn/appsync-resolvers

Resolvers

import (
    "github.com/sbstjn/appsync-resolvers"
)

type personArguments struct {
    ID int `json:"id"`
}

func resolvePeople() (people, error) {
    return dataPeople, nil
}

func resolvePerson(p personArguments) (*person, error) {
    return dataPeople.byID(p.ID)
}

func resolveFriends(p person) (people, error) {
    return p.getFriends()
}

var (
    r = resolvers.New()
)

func init() {
    r.Add("query.people", resolvePeople)
    r.Add("query.person", resolvePerson)
    r.Add("field.person.friends", resolveFriends)
}

func main() {
    lambda.Start(r.Handle)
}

AppSync Configuration

Resolver lookup is based on a resolve property in your RequestMappingTemplate, which can be configured using the AWS Console or CloudFormation as well. This approach works fine with the recommended AWS setup for multiple custom resolvers and AWS Lambda data sources:

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

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

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

AppSyncResolverFriends:
  Type: AWS::AppSync::Resolver
  Properties:
    ApiId: !GetAtt [ AppSyncAPI, ApiId ]
    TypeName: Person
    FieldName: friends
    DataSourceName: !GetAtt [ AppSyncDataSource, Name ]
    RequestMappingTemplate: '{ "version" : "2017-02-28", "operation": "Invoke", "payload": { "resolve": "field.person.friends", "context": $utils.toJson($context) } }'
    ResponseMappingTemplate: $util.toJson($context.result)

Head over to appsync-resolvers-example for an example project and how simple it can be to set up, maintain, and deploy a serverless GraphQL API with AWS AppSync using the Serverless Application Model.

License

Feel free to use the code, it's released using the MIT license.

Contribution

You are welcome to contribute to this project! ๐Ÿ˜˜

To make sure you have a pleasant experience, please read the code of conduct. It outlines core values and beliefs and will make working together a happier experience.

appsync-resolvers's People

Contributors

sbstjn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

appsync-resolvers's Issues

How do we handle errors?

How do we properly pass errors? Right now, all errors are returning as "Lambda:Unhandled".

Any way to customize errorType, errorInfo?

{
  "data": null,
  "errors": [
    {
      "path": [
        "updateProject"
      ],
      "data": null,
      "errorType": "Lambda:Unhandled",
      "errorInfo": null,
      "locations": [
        {
          "line": 15,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Name required"
    }
  ]
}

Thanks!

Make context object available to resolver func

Currently, resolver functions are invoked only with the GraphQL arguments or the resolved field. The identity field or any other (possibly user-defined) field of the $context object is dropped.

return handler.call(in.payload())

This makes it impossible to perform resource-based authorization based on $context.indentity.username or $context.identity.cognitoIdentityId (see AWS AppSync Developer Guide) in Lambda resolvers.

Nested type fields with arguments don't seem to work

Ran into this case with a graphql schema I'm currently working on and have racked my brain for a bit to think up a good solution. Thought I'd post an issue here to see if there's any ideas on how to solve this problem without creating a custom fork ๐Ÿ˜„

Say you have this GraphQL schema (not the schema I'm using, but it's enough to show the problem):

type Query {
  users: [User]
}

type User {
  id ID!
  name String!
  role(company: ID!): String!
}

type Role {
  id: ID!
  name: String!
}

The users field on the Query type returns a list of User types. However, the User type also has a role field that can take a company arg. Currently, the role field cannot be mapped to a lambda function using this library, which is due to (as best I can tell) these lines: https://github.com/sbstjn/appsync-resolvers/blob/master/invocation.go#L15-L25 . The payload function appears to only return either the Source field or the Arguments field exclusively, meaning that the role resolver wouldn't be able to access the company arg at all.

I could solve this by making a more custom appsync resolver, i.e.

{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload": {
    "resolve": "query.users.role",
    "context": {
      "source": null,
      "arguments": $utils.toJson($context)
    }
  }
}

and then making my event structure parse out those properties, i.e.

type User struct {
  ID   string `json:"id"`
  Name string `json:"name"`
}

type roleArgs struct {
  Company string `json:"company"`
}

type event struct {
  Source    User     `json:"source"`
  Arguments roleArgs `json:"arguments"`
}

However, this does seem a bit more complicated than it needs to be, and I was wondering if there's a better way to solve combining the source & arguments together. I'm totally willing to work on implementing whatever solution is decided on (if any) ๐Ÿ˜„

Does AppSync support TransactWriteItems resolver?

Hi guys. I have a question: Does AppSync support TransactWriteItems resolver?
Because I have such error on eu-central-1:

{
  "data": {
    "createMessage": null
  },
  "errors": [
    {
      "path": [
        "createMessage"
      ],
      "data": null,
      "errorType": "MappingTemplate",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Unsupported operation 'TransactWriteItems'."
    }
  ]
}

with the following request template:

## [Start] Determine request authentication mode **
#if( $util.isNullOrEmpty($authMode) && !$util.isNull($ctx.identity) && !$util.isNull($ctx.identity.sub) && !$util.isNull($ctx.identity.issuer) && !$util.isNull($ctx.identity.username) && !$util.isNull($ctx.identity.claims) && !$util.isNull($ctx.identity.sourceIp) && !$util.isNull($ctx.identity.defaultAuthStrategy) )
  #set( $authMode = "userPools" )
#end
## [End] Determine request authentication mode **
## [Start] Check authMode and execute owner/group checks **
#if( $authMode == "userPools" )
  ## No Static Group Authorization Rules **


  ## No Dynamic Group Authorization Rules **


  ## [Start] Owner Authorization Checks **
  #set( $isOwnerAuthorized = false )
  ## Authorization rule: { allow: owner, ownerField: "owner", identityClaim: "cognito:username" } **
  #set( $allowedOwners0 = $util.defaultIfNull($ctx.args.input.owner, null) )
  #set( $identityValue = $util.defaultIfNull($ctx.identity.claims.get("username"), $util.defaultIfNull($ctx.identity.claims.get("cognito:username"), "___xamznone____")) )
  #if( $util.isList($allowedOwners0) )
    #foreach( $allowedOwner in $allowedOwners0 )
      #if( $allowedOwner == $identityValue )
        #set( $isOwnerAuthorized = true )
      #end
    #end
  #end
  #if( $util.isString($allowedOwners0) )
    #if( $allowedOwners0 == $identityValue )
      #set( $isOwnerAuthorized = true )
    #end
  #end
  #if( $util.isNull($allowedOwners0) && (! $ctx.args.input.containsKey("owner")) )
    $util.qr($ctx.args.input.put("owner", $identityValue))
    #set( $isOwnerAuthorized = true )
  #end
  ## [End] Owner Authorization Checks **


  ## [Start] Throw if unauthorized **
  #if( !($isStaticGroupAuthorized == true || $isDynamicGroupAuthorized == true || $isOwnerAuthorized == true) )
    $util.unauthorized()
  #end
  ## [End] Throw if unauthorized **
#end
## [End] Check authMode and execute owner/group checks **

#set($dateTime = $util.time.nowISO8601())
## [Start] Set the primary @key. **
#set( $modelObjectKey = {
  "pk": $util.dynamodb.toDynamoDB($ctx.args.input.pk),
  "sk": $util.dynamodb.toDynamoDB("M#${dateTime}#${identityValue}")
} )
## [End] Set the primary @key. **

## [Start] Prepare DynamoDB PutItem Request. **
$util.qr($context.args.input.put("createdAt", $util.defaultIfNull($ctx.args.input.createdAt, $util.time.nowISO8601())))
$util.qr($context.args.input.put("updatedAt", $util.defaultIfNull($ctx.args.input.updatedAt, $util.time.nowISO8601())))
$util.qr($context.args.input.put("__typename", "EntityItem"))
$util.qr($context.args.input.put("target", "Chat"))
#set( $condition = {
  "expression": "attribute_not_exists(#pk)",
  "expressionNames": {
      "#pk": "pk"
  }
} )
#if( $context.args.condition )
  #set( $condition.expressionValues = {} )
  #set( $conditionFilterExpressions = $util.parseJson($util.transform.toDynamoDBConditionExpression($context.args.condition)) )
  $util.qr($condition.put("expression", "($condition.expression) AND $conditionFilterExpressions.expression"))
  $util.qr($condition.expressionNames.putAll($conditionFilterExpressions.expressionNames))
  $util.qr($condition.expressionValues.putAll($conditionFilterExpressions.expressionValues))
#end
#if( $condition.expressionValues && $condition.expressionValues.size() == 0 )
  #set( $condition = {
  "expression": $condition.expression,
  "expressionNames": $condition.expressionNames
} )
#end
{
  "version": "2018-05-29",
  "operation": "TransactWriteItems",
  "transactItems": [
    {
      "table": "EntityItem-bwwoag6fybal3d5y3wfirf7zru-dev",
      "operation": "PutItem",
      "key": $util.toJson($modelObjectKey),
      "attributeValues": $util.dynamodb.toMapValuesJson($context.args.input),
      "condition": $util.toJson({
         "expression": $condition.expression,
         "expressionNames": $condition.expressionNames,
         "expressionValues": $condition.expressionValues,
         "returnValuesOnConditionCheckFailure": false
      })
    }
  ]
}
## [End] Prepare DynamoDB PutItem Request. **

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.