This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const typeDefs = ` | |
type Query { | |
books: [Book] | |
} | |
type Book { | |
title: String | |
author: String | |
fullContent:String | |
} | |
type Mutation { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express") | |
const bodyParser = require("body-parser") | |
const {graphqlExpress, graphiqlExpress} = require("apollo-server-express") | |
const {makeExecutableSchema} = require("graphql-tools") | |
// Some fake data | |
const books = [ | |
{ | |
id: 0, | |
title: "Harry Potter and the Sorcerer's stone", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// base route | |
app.put("/:user", function (request, response) { | |
collection.updateOne({ user : request.params.user }, | |
{$set:{ ...request.body, user : request.params.user }}, | |
function (err, r) { | |
if (err){ | |
response.send("An error occured") | |
}else{ | |
response.send("All well") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get("/:user", function (request, response) { | |
collection.find({ user : request.params.user }).toArray(function (err, docs) { | |
if (err){ | |
response.send("An error occured") | |
}else{ | |
response.send(docs) | |
} | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post("/:user", function (request, response) { | |
// inserts a new document on the server | |
collection.insertOne({ ...request.body, user : request.params.user }, function (err, r) { | |
if (err){ | |
response.send("An error occured") | |
}else{ | |
response.send("All well") | |
} | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mongodb.MongoClient.connect(uri, function(err, db) { | |
const collection = db.collection('users') | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// init project | |
const express = require('express'); // the library we will use to handle requests | |
const app = express(); // instantiate express | |
app.use(require("cors")()) // allow Cross-domain requests | |
// base route | |
app.get("/", function (request, response) { | |
response.send("TODO") // always responds with the string "TODO" | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mongodb.MongoClient.connect(uri, function(err, db) { | |
// base route | |
app.get("/:user", function (request, response) { | |
response.send(request.params.user) | |
}); | |
// base route | |
app.post("/:user", function (request, response) { | |
response.send(request.params.user) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
URI=mongodb://admin:PASSWORD@ds111885.mlab.com:11885/medium |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongodb = require('mongodb'); // load mongodb | |
const uri = process.env.URI; |