GithubHelp home page GithubHelp logo

wecodefest-graphql's Introduction

GraphQL using AWS App Sync

This tutorial explains how to take advantage of the GraphQL features of AWS App Sync.

DynamoDB Setup

Create two tables in AWS DynamoDB:

  • Movies - Primary key: 'id' (number)
  • Reviews - Primary key: 'id' (number)

Lambda Functions

Import in Lambda the following six functions (Node.js 8.x):

  • MovieList - Used to list all the entries in the table Movies
  • MovieAdd - Used to add a new entry in the table Movies
  • MovieGet - Used to retrieve a single element in the table Movies
  • ReviewList - Used to list all the entries in the table Reviews
  • ReviewAdd - Used to add a new entry in the table Reviews
  • ReviewGet - Used to retrieve a single element in the table Reviews

During the creation of the first lambda, you will need to create a new custom role. Edit that role in the IAM console and attach to it the policy AmazonDynamoDBFullAccess.

API Gateway

Create in AWS API Gateway the Movies API with the following resources and methods:

  • /
    • /movies
      • GET - Associate it with MovieList lambda
      • POST - Associate it with MovieAdd lambda
      • /{id}
        • GET - Associate it with MovieGet lambda

Be careful to always select "Use Lambda Proxy integration". Create as well the Reviews API with this list of resources and methods:

  • /
    • /reviews
      • GET - Associate it with ReviewList lambda. Add a URL query string parameter named 'idMovie'
      • POST - Associate it with ReviewAdd lambda
      • /{id}
        • GET - Associate it with ReviewGet lambda

Deploy both APIs in a new stage named 'beta'.

Populate data

Use this Postman collection to interact with the APIs and create an initial dataset. You will need to create a new Postman environment with these variables:

  • movies - link to the Movies API in the beta stage created in previous step.
  • reviews - link to the Reviews API

Use the different requests in the collection to verify that all the endpoints are working properly.

App Sync

Create a new AWS App Sync API named Movies. Use the file schema.graphql to create the Schema.

Create two datasources of type HTTP:

  • movies - Linking to the URL in API Gateway of the Movies API
  • reviews - Linking to the URL in API Gateway of the Reviews API

Define resolvers for the following types, queries and mutations:

  • Movie.reviews - Data Source: reviews. Configure the request mapping template using this file
  • Mutation.newMovie - Data Source: movies.Configure the request mapping template using this file
  • Query.getMovie - Data Source: movies. Configure the request mapping template using this file
  • Query.getMovies - Data Source: movies. Configure the request mapping template using this file
  • Query.getReviews - Data Source: reviews. Configure the request mapping template using this file

Test GraphQL API

Use the file queries.graphql to verify the GraphQL API using the Queries tab provided by AWS App Sync

Alternatively you can use the GraphQL Playground of Prisma ( https://github.com/prisma/graphql-playground ) to test the GraphQL API. You will need to get the API Key from the Settings tab and pass it as a x-api-key header.

wecodefest-graphql's People

Contributors

jrhuerga avatar

Stargazers

Gabriel GalanCasillas avatar Jorge Jimenez avatar Jesús Miguel Benito Calzada avatar Jorge Maroto 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.