Skip to content

cubonetwork/serverless-graphql-rest-api

Repository files navigation

serverless-graphql-rest-api

The main porpuse of this repo is be a sample of how to integrate Apollo Server + GraphQL + Serverless Architecture. For this we have created a search API thought a schema like following:

Model

To summarize we have 3 entities: User, Company and Contacts. So, if I wish retrieve all the contacts of a specific company, how could I do it? This question will be answered by this code.

Dependencies

To understand the concepts and code implemented in this repo it's necessary a basic knowledge of GraphQL, Serverless Architecture and Node.js programming language.

Running

After clone this repo in your computer, go to project folder and install dependencies using the following command:

npm i

After all dependencies have installed you can run the project throught the command:

npm run offline

If everything was fine you could see a screen like this:

Running Offline

Implementation

This implementation was based on the post Running a scalable & reliable GraphQL endpoint with Serverless.

Basically, the project contains two handlers, as you can see at serverless.yml file:

  • graphql: whose implementation is in src/handler.js file, more specifically at line 11. This handler is responsible to process requests throught GraphQL using apollo-server-lambda implementation. In other words is the Backend API of our project.
  • graphiql: whose implementation is in src/handler.js file, more specifically at line 25. This handler returns a GraphQL Client implementation called graphiql. Where you can write your queries and see the results.

Other important files are:

  • graphql: The graphql folder contains the graphql schema used for this project. In this folder you will find all schema's types, resolvers and queries.
    • schema.js: Where we define the GraphQL types, queries, mutations and subscriptions, which makes up the GraphQL Schema.
    • resolvers.js: The implementation of GraphQL resolvers based on types and queries defined at schema.js.
    • types.js: Just an array of types which will by used at schema.js.
  • src/core: The core folder contains the code to integrate with data layer. We have used DynamoDB to persist data.
  • dynamodb/seed-data: The seed-data folder contains sample data used on offline mode. See serverless-dynamodb-local for more information.

See it in action

Just open the link http://localhost:3000/graphiql in your browser.

In action

Next steps

  • Compares Apollo Engine and AppSync to manage cache and metrics about the GraphQL API
  • Reimplement this project using some GraphDB
    • Amazon Neptune: until now (04/19/2018) neptune is only available for whitelisted users and there is no integration from Lambda functions. To request a preview for Neptune click here.
    • Neo4j: It's a good option, but, AWS there is no a managed service for neo4j. So you have to install and mantain it in a EC2 instance.

Neo4j

To run neo4j using docker run the following command:

npm run neo4j:start

Not implemented yet

  • neo4j/seed-data.js: the porpuse of this file is to create data on neo4j docker instance.

Test

We are using jest for testing.

Running tests

npm run test

Coverage

npm run coverage

Documentation

npm run generate-docs

After see generated site in ./docs/index.html.

Deploy

First of all you need to configure your AWS credentials:

sls config credentials --provider aws --key <your_aws_access_key> --secret <your_aws_secret_key>

After that:

npm run deploy

About

About

The porpuse of this repo is to create a REST API using GraphQL (Apollo Server) and Serverless framework.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published