|
1 |
| -# graphql-rest-api-demo |
2 |
| -A demo of what an equivalent REST API and GraphQL interface look like. |
| 1 | +# GraphQL/REST API Demo |
| 2 | + |
| 3 | +A demo of what an equivalent REST API and GraphQL interface look like. This code is used in the first chapter of the forthcoming [The GraphQL Guide](https://graphql.guide/) by [John Resig](https://johnresig/) and [Loren Sands-Ramshaw](http://lorensr.me/). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To install you'll want to run `npm install` or `yarn` to install all of the Node modules. |
| 8 | + |
| 9 | +Additionally, you'll need to have a copy of [MongoDB](https://www.mongodb.com/) running. If you're on OSX you can use [Homebrew](https://brew.sh/) 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 stop mongodb` to start it. |
| 10 | + |
| 11 | +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: |
| 12 | + |
| 13 | +``` |
| 14 | +db.users.insert({_id: "123", username: "jeresig", groupId: "dev"}) |
| 15 | +db.groups.insert({_id: "dev", name: "Developers"}) |
| 16 | +``` |
| 17 | + |
| 18 | +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`. |
| 19 | + |
| 20 | +## Data Models |
| 21 | + |
| 22 | + |
| 23 | +Our data models representing the MongoDB database are in `models.js`. We use [Mongoose](http://mongoosejs.com/) to do the object modeling and provide a convenient way of accessing and mutating the data in the MongoDB collections. |
| 24 | + |
| 25 | + |
| 26 | +## REST API |
| 27 | + |
| 28 | +The REST API is implemented using [Node Express](https://expressjs.com/) and provides a couple endpoints for accessing user data. |
| 29 | + |
| 30 | +You can run it using: |
| 31 | + |
| 32 | +``` |
| 33 | +node rest-server.js |
| 34 | +``` |
| 35 | + |
| 36 | +You can access the REST API by opening your browser and visiting either of the following URLs: |
| 37 | + |
| 38 | +* [http://localhost:3000/users](http://localhost:3000/users) - All users. |
| 39 | +* [http://localhost:3000/users/123](http://localhost:3000/users/123) - An individual user record. |
| 40 | + |
| 41 | +## GraphQL Server |
| 42 | + |
| 43 | +The GraphQL server is implemented using [Node Express](https://expressjs.com/), [GraphQL.js](https://github.com/graphql/graphql-js), and [GraphQL Express](https://github.com/graphql/express-graphql). It provides access to both the User and Group type. |
| 44 | + |
| 45 | +You can run it using: |
| 46 | + |
| 47 | +``` |
| 48 | +node graphql-server.js |
| 49 | +``` |
| 50 | + |
| 51 | +You can access the GraphQL data by opening your browser and visiting the [GraphiQL](https://github.com/graphql/graphiql) view at: |
| 52 | + |
| 53 | +* [http://localhost:3000/graphql](http://localhost:3000/graphql) |
| 54 | + |
| 55 | +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. |
0 commit comments