Code Monkey home page Code Monkey logo

bapi's Introduction

rust-lambda-graphql-upload-example

Initial source code from dbanty Dylan Anthony.

A super simple example GraphQL API that can be hosted on AWS Lambda. Has the file upload feature

What's in the box?

  1. src contains the code to integrate the async-graphql crate with the lambda crates for a serverless GraphQL experience. Both POST and GET queries are supported on any path.
  2. .github contains Renovate support for keeping dependencies up to date.
  3. .github/workflows contains a GitHub Actions workflow that will build and test the code on every push as well as deploy on pushes to main. There is also an integration test which runs after deploy.

This is set up for the most simple use-case: a function URL. You will need to make some tweaks to get this working with API Gateway.

To Set Up

  1. Install cargo-lambda
  2. Install Zig (needed by cargo-lambda)
  3. An AWS credentials file (for deploying only)

How to use?

  1. cargo lambda watch will start the service lazily (only compiles when the first request comes in), then watch and reload your code on changes. The URL will be http://localhost:9000/graphql (where graphql-example is the name of the binary once you change it).
  2. To deploy, first run cargo lambda build --release --arm64 (for graviton processors) then AWS_PROFILE=myprofile cargo lambda deploy.
  3. To get the GitHub Actions to deploy on push to main, you need to set two GitHub Secrets for AIM credentials: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
  4. For the integration tests to work, fill in the FUNCTION_URL secret with the URL of the deployed function. This assumes that the same AWS credentials which can deploy the function can invoke it—if this isn't true, you'll need to tweak the integration_test job in .github/workflows/release.yml. It also assumes that your function is secured with the AWS_IAM type of function security. You'll need to tweak this job as soon as you change the schema.
  5. For the graphql palygound cargo lambda watch will start the service lazily (only compiles when the first request comes in), then watch and reload your code on changes. The URL will be http://localhost:9000/playground.
  6. To send the file upload requests and test the API use https://altairgraphql.dev/#download

Here's an example IAM policy to use for GitHub secrets (both deploying and integration testing):

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "lambda:CreateFunction",
                "lambda:UpdateFunctionCode",
                "lambda:InvokeFunctionUrl",
                "lambda:GetFunction"
            ],
            "Resource": "<your function arn>"
        }
    ]
}

bapi's People

Contributors

linking-yasitha avatar

Watchers

 avatar

Forkers

asinduvg

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.