https://github.com/davidbuck/mongodb-node-graphql-example
Simple GraphQL server example using graphql-yoga, Mongoose and MongoDB
https://github.com/davidbuck/mongodb-node-graphql-example
graphql-yoga mongoose-js
Last synced: 3 months ago
JSON representation
Simple GraphQL server example using graphql-yoga, Mongoose and MongoDB
- Host: GitHub
- URL: https://github.com/davidbuck/mongodb-node-graphql-example
- Owner: DavidBuck
- License: mit
- Created: 2020-05-07T05:05:18.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T05:40:37.000Z (over 2 years ago)
- Last Synced: 2025-01-10T18:35:23.816Z (5 months ago)
- Topics: graphql-yoga, mongoose-js
- Language: JavaScript
- Homepage:
- Size: 1.21 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# GraphQL server example
This is a simple GraphQL server example using [graphql-yoga](https://github.com/prisma-labs/graphql-yoga), [Mongoose](https://www.npmjs.com/package/mongoose) and MongoDB.
To start the server:
- start MongoDB on your local machine
- `npm run start`Go to [http://localhost:3000/playground](http://localhost:3000/playground) to launch the GraphQL Playground.
# Queries
**All sensors:**
```graphql
{
sensors {
sensorName
type
location
_id
}
}
```**All sensorValues:**
```graphql
{
sensorValues {
temp
humidity
time
_id
sensor {
sensorName
type
location
_id
}
}
}
```**A sensor:**
```graphql
{
sensor(_id: "5eba09502ba39cfe8fd331cd") {
sensorName
type
location
_id
}
}
```**A sensorValue:**
```graphql
{
sensorValue(_id: "5ebb20231b0fc015e75c0e0f") {
temp
humidity
time
_id
sensor {
sensorName
type
location
_id
}
}
}
```## Mutations
**Create a sensor:**
```graphql
mutation {
createSensor(
sensorName: "Freezer Sensor 1"
type: "Arduino Uno"
location: "Main freezer"
) {
sensorName
type
location
_id
}
}
```**Create a sensorValue:**
```graphql
mutation {
createSensorValue(
temp: 25.4
humidity: 82.2
time: "2020-05-12T03:29:10.040Z"
sensorId: "5eba1b5315de6001af4c8614"
) {
temp
humidity
time
_id
sensor {
sensorName
type
location
_id
}
}
}
```**Update a sensor:**
```graphql
mutation {
updateSensor(
_id: "5eba1ff3f550ab0288d69922"
sensorName: "New Freezer Sensor"
type: "Raspberry Pi"
location: "Main freezer"
) {
sensorName
type
location
_id
}
}
```**Update a sensorValue:**
```graphql
mutation {
updateSensorValue(
_id: "5eba1ff3f550ab0288d69929"
temp: 2.4
humidity: 82.2
time: "2020-05-12T03:29:10.040Z"
sensorId: "5eba1ff3f550ab0288d69923"
) {
temp
humidity
time
_id
sensor {
sensorName
type
location
_id
}
}
}
```**Delete a sensor:**
```graphql
mutation {
deleteSensor(_id: "5eba1b5315de6001af4c8614")
}
```**Delete a sensorValue:**
```graphql
mutation {
deleteSensorValue(_id: "5eba1bb198fbcd01cdb25acb")
}
```## Tools
- [graphql-yoga](https://github.com/prisma-labs/graphql-yoga)
- [Mongoose](https://www.npmjs.com/package/mongoose)