Code Monkey home page Code Monkey logo

api-gql-apisync's Introduction

GraphQL AppSync with Lambda Data Sources

This repository provides an example of how to build an AWS AppSync API with Lambda data sources using the AWS CDK.

Architecture

The architecture of this example is based on the following components:

  • AWS AppSync API: This is the GraphQL API that we will be building using the AWS AppSync service.
  • AWS Lambda functions: These are the data sources for the AppSync API. We have two Lambda functions in this example:
  • weather: This function returns the weather for a specified location by making a REST API call to a weather service.
  • album: This function returns a list of albums for a specified artist by making a REST API call to the iTunes API.
  • AWS CDK: This is the infrastructure as code tool we use to deploy our architecture. We use CDK to define the API, the Lambda functions, and the necessary permissions and mappings between them.

The following diagram shows the high-level architecture of this example:

      ┌─────────────┐
      │   AppSync   │
      │     API     │
      └──────┬──────┘
             
    ┌────────┴─────────┐
    │ AWS Lambda:      
    │  - weather       │
    │  - album         │
    └────────┬─────────┘
             
    ┌────────┴─────────┐
    │   REST APIs:     
    │ - Weather API    │
    │ - iTunes API     │
    └──────────────────┘

Getting Started

Prerequisites

Installation

  1. Clone the repository:

git clone https://github.com/skenklok/api-gql-apisync.git

  1. Navigate to the project directory:

cd api-gql-apisync

  1. Install the dependencies:

npm install

Repository Overview

This repository contains the source code for a serverless GraphQL API built with AWS AppSync and AWS Lambda. The API provides two main features: retrieving weather data and retrieving albums from iTunes. The API is secured with an API key and includes two resolvers that leverage AWS Lambda as the data source.

The repository contains the following directories and files:

- .gitignore
- README.md
- appsync/
  - schema.graphql
- cdk/
  - bin/
    - cdk.ts
  - lib/
    - api-gql-appsync-stack.ts
  - package.json
  - tsconfig.json
- lambda-adapters/
  - album/
    - index.js
  - weather/
    - index.js
- package.json

Repository Structure

The repository contains the following structure:

  • appsync/: A directory containing the schema definition file for the AppSync API.

  • cdk/: A directory containing the AWS CDK stack definition for deploying the AppSync API and Lambda functions.

  • bin/: A directory containing the entry point file for the AWS CDK app.

  • lib/: A directory containing the stack definition file for the AppSync API and Lambda functions.

  • package.json: A file containing the dependencies and scripts for the AWS CDK app.

  • tsconfig.json: A file containing the TypeScript configuration for the AWS CDK app.

  • lambda-adapters/: A directory containing the Lambda function code for fetching album and weather data.

  • album/: A directory containing the Lambda function code for fetching album data from the iTunes API.

  • weather/: A directory containing the Lambda function code for fetching weather data from the OpenWeatherMap API.

  • package.json: A file containing the dependencies and scripts for the Lambda functions.

Deployment

  1. Configure your AWS CLI by running:

aws configure

This command will prompt you to enter your AWS access key ID, secret access key, default region, and output format.

  1. Deploy the project using AWS CDK:

cdk bootstrap # Only required once per account and region cdk deploy

After the deployment is complete, you will see the outputs of the stack in your terminal, including the GraphQL API endpoint, API key, and other useful information.

Testing

You can test the GraphQL API using a tool like Postman or GraphQL Playground.

  1. In the testing tool, create a new POST request with the following headers:

Content-Type: application/json x-api-key: Your_ApiGqlAppsyncStack_GraphQLAPI_KEY

  1. Set the request URL to ApiGqlAppsyncStack.GraphQLAPI_URL.

  2. To test the API, you can use the AWS AppSync console or a GraphQL client like GraphiQL or Apollo Studio. You will need to provide the API key in the x-api-key header for authentication.

Here are some example queries you can run:

query {
  getWeather(location: "Seattle") {
    temperature
    description
  }
}

query {
  album(artist: "Metallica") {
    id
    name
    artist
    artworkUrl
    releaseDate
  }
}
  1. Send the request. The response will contain the weather data for the specified city.

License

This project is licensed under the MIT License - see the LICENSE file for details.

api-gql-apisync's People

Contributors

skenklok avatar

Watchers

 avatar

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.