Little graph server demo. Apollo server running in an Express app.
Install packages and seed the data:
yarn
node populate.jsThe "database" here is just flat JSON files and a simple module to query and modify their data.
yarn start will start an Express server with an Apollo GraphQL server attached. There are no Express routes, but 2 graph routes.
| Endpoint | Description |
|---|---|
/graph |
The first version of the API, it only exposes a books query and author data is just part of the book document |
/v2/graph |
The second version of the API, exposing book, books, and author queries, as well as mutations to add books and authors. |
The goal here was to expose two different graphs from a single server, mostly to prove that it could be done. I was also trying to learn how to write resolvers that dealt with mapping, so I could join two (or more) documents into the same query. The "v2" graph was a nice way to add that while still having the basic use case available in "v1".
This only exposes books, which you can query like so:
query {
books {
id
title
author
}
}This exposes book, books, and authors, and joins books and authors together. Here's an extensive query to show how they work:
query {
books {
id
title
author {
id
name
}
}
book(id: "x7174v9cof") {
title
author {
name
}
}
authors {
id
name
}
author(id: "ls5xcds8ln") {
id
name
books {
id
title
author {
name
}
}
}
}You can also use a mutation to add new books and authors. Here's a valid way to use the addBook mutation:
mutation {
addBook(title: "Ender's Game", author: "mlqnnsc8rq") {
id
title
author {
id
name
}
}
}MIT © w33ble