npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@appsync/serverless-plugin

v0.3.5

Published

This plugin wraps AppSync and serverless-appsync-plugin into a new format which requires less configurations.

Downloads

17

Readme

Lambda GraphQL Plugin

This plugin wraps AppSync and serverless-appsync-plugin into a new format which requires less configurations.

How does it works?

This plugin will extract Function, Data Sources and Mapping Definitions using a custom GraphQL Directive, just flag any GraphQL field with @AWSLambda() and it will be deployed.

How to setup:

Install the plugin using NPM:

npm install --save-dev @appsync/serverless-plugin

Or Yarn:

yarn add --dev @appsync/serverless-plugin

Add it into your serverless.yml settings:

plugins:
  - '@appsync/serverless-plugin'

Note, if you were using serverless-appsync-plugin, remove it, since this plugins wraps it.

Set Settings:

  custom:
    appSync:
      schema: schema.graphql # If you're not using Schema stitching, you can specify a single graphql file here.
      prefix: 'some_prefix' # If you want to prefix aws lambda names, here you can add a prefix.
      schemaDir: Controllers/ # Directory to find out the .graphql schemas (Schema stitching), this overrides "schema" field.
      authenticationType: API_KEY #See serverless-appsync-plugin documentation.
      name: ${self:service.name}_${self:provider.stage} #See serverless-appsync-plugin documentation.
      serviceRole: 'AppSyncServiceRole' #See 'dataSources.config.serviceRoleArn' serverless-appsync-plugin documentation.

Create your first function: Create any .graphql file under schemaDir directory like this:

  type Author {
    name: String
  }

  type Todo {
    id: String!
    text: String!
    title: String!
    author: Author!
    @AWSLambda(handler: "Controllers/Todo/Author/index.default")
  }

  type Query {
    GetTodo(id: String!): Todo!
    @AWSLambda(handler: "Controllers/Todo/GetTodo.default")
    SearchTodo(search: String!): [Todo!]
    @AWSLambda(handler: "Controllers/Todo/SearchTodo.default")
  }

  schema {
    query: Query
  }

Replace schema and types accordingly, "handler" should point to the function you want to bind to this lambda.

Event Shape:

Request Event:

When your lambdas are called, the following request is sent on the lambda's first parameter:

{
    // Arguments Received from the call (see `GetTodo` as example).
    "args": {
      "id": "6w4d3xpnevfrdemm7qmpo375fq"        
    },
    // If this is a query as was within a field, you'll receive his parent value here.
    // (see `author` field as example, on `parent` you'll receive the `Todo`)
    "parent": null,
    // Headers from the request, in a key-value object.
    "headers": {
        "authorization": "Bearer eyJh.......90",
        "sec-fetch-dest": "empty",
        "cloudfront-is-desktop-viewer": "true",
        "sec-fetch-site": "same-origin",
        "x-forwarded-port": "443"
    }
}
Successful Response:

If everything worked well, you can return data to the client by returning:

{
  // Remember, `data` should match the response type of your graphql field, this
  // is the example of `GetTodo`
  "data": {
    "id": "6w4d3xpnevfrdemm7qmpo375fq",
    "text": "This is your note body, you can put things here",
    "title": "An ordinary note"
  }
}
Failed Response:

If an error ocurred, we'll format it accordingly if error property is returned:

{
  "error": {
    "name": "NotFound",
    "message": "Todo not found",
    "other_field": "any other field that you like to return to the client"
  }
}

For proper formatting, please return name and message on those errors.

API:

Commands:

  • serverless validate: Will Validate your schema/schemas, throwing an error if an issue is found.
  • serverless generate-types: Will generate an example of the output serverless type.

Config Overrides:

  • custom.appSync.schema: If custom.appSync.schemaDir is found, it will override custom.appSync.schema.

Config Shape:

Please refer to Serverless Appsync Plugin for documentation on original fields. Extra fields includes:

  appSync:
    schema: schema.graphql # Reference a single graphql schema field to use as definition.
    schemaDir: Controllers/ # Directory to find out the .graphql schemas using schema stitching. This overrides "schema" field.
    serviceRole: 'AppSyncServiceRole' #See 'dataSources.config.serviceRoleArn' serverless-appsync-plugin documentation.
  • schemaDir: Setting this field the plugin will set schema stitching by picking any .graphql file under this directory.
  • request / response: Points to a Mapping Template File, see Configuring the plugin for more info.
  • serviceRole: Where AppSyncDynamoDBServiceRole is an IAM role defined in Resources, see "Example ServiceRole"

Example ServiceRole:

resources:
  Resources:
    AppSyncServiceRole:
      Type: "AWS::IAM::Role"
      Properties:
        RoleName: ${self:custom.appSync.name}-Lambda-AppSyncServiceRole
        AssumeRolePolicyDocument:
          Version: "2012-10-17"
          Statement:
            - Effect: "Allow"
              Principal:
                Service:
                  - "appsync.amazonaws.com"
              Action:
                - "sts:AssumeRole"

        Policies:
          - PolicyName: ${self:custom.appSync.name}-Lambda-AppSyncServiceRole-Policy
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Effect: "Allow"
                  Action:
                    - "lambda:invokeFunction"
                  Resource:
                    - "*"

The @AWSLambda Directive:

Usage

@AWSLambda flags the following field to be resolved with a function:

# It can be used on a root type: 
type Query {
  GetTodo(id: String!): Todo!
  @AWSLambda(handler: "Controllers/Todo/GetTodo.default")
}

# Or within another type:
type Todo {
  id: String!
  text: String!
  title: String!
  author: Author!
  @AWSLambda(handler: "Controllers/Todo/Author/index.default")
}

Parameters:

  • handler: Handler receives a string with the path of where the function is located, use dot notation to define which exported method should be called.