Skip to content

sohan-fueled/graphql-rest-api-demo

 
 

Repository files navigation

GraphQL/REST API Demo

A demo of what an equivalent REST API and GraphQL API look like. This code is used in the first chapter of The GraphQL Guide by John Resig and Loren Sands-Ramshaw. Here's the GraphQL server's GraphiQL:

ch1.graphql.guide/graphql

Installation

To install you'll want to run npm install or yarn to install all of the Node modules.

Additionally, you'll need to have a copy of MongoDB running. If you're on OSX you can use Homebrew to install MongoDB by running: brew install mongodb. That should also start the database server in the background, as well. If it's not running you can run brew services start mongodb to start it.

You'll also want to populate the database with some data to test your queries. You can do this by running the mongo testdb command on the command-line and executing the following commands to create a new database, some collections, and the data inside of them:

db.users.insert({_id: "123", username: "jeresig", groupId: "dev"})
db.users.insert({_id: "456", username: "lorensr", groupId: "author"})
db.groups.insert({_id: "dev", name: "Developers"})
db.groups.insert({_id: "author", name: "Authors"})

By default the servers are expecting to find data on your local computer in a database named "testdb". You can configure this by changing the settings in models.js.

Data Models

Our data models representing the MongoDB database are in models.js. We use Mongoose to do the object modeling and provide a convenient way of accessing and mutating the data in the MongoDB collections.

REST API

The REST API is implemented using Node Express and provides a couple endpoints for accessing user data.

You can run it using:

node rest-server.js

You can access the REST API by opening your browser and visiting either of the following URLs:

GraphQL Server

The GraphQL server is implemented using Node Express, GraphQL.js, and GraphQL Express. It provides access to both the User and Group type.

You can run it using:

node graphql-server.js

You can access the GraphQL data by opening your browser and visiting the GraphiQL view at:

You should see a console interface into which you can run GraphQL queries and see their results. You should also be able to browse the full schema and see all of the types that are available to you and what data they provide.

About

A demo of what an equivalent REST API and GraphQL API look like.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%