Skip to content

Instantly share code, notes, and snippets.

View ZaninAndrea's full-sized avatar
👨‍💻

Zanin Andrea ZaninAndrea

👨‍💻
View GitHub Profile
const typeDefs = `
type Query {
books: [Book]
}
type Book {
title: String
author: String
fullContent:String
}
type Mutation {
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",
// 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")
}
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)
}
})
});
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")
}
})
});
mongodb.MongoClient.connect(uri, function(err, db) {
const collection = db.collection('users')
// ...
}
@ZaninAndrea
ZaninAndrea / server.js
Created November 1, 2017 15:37
server
// 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"
});
@ZaninAndrea
ZaninAndrea / server.js
Last active November 1, 2017 15:39
server.js
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)
});
@ZaninAndrea
ZaninAndrea / .env
Created October 29, 2017 19:10
.env
URI=mongodb://admin:PASSWORD@ds111885.mlab.com:11885/medium
@ZaninAndrea
ZaninAndrea / server.js
Created October 29, 2017 19:04
server.js
const mongodb = require('mongodb'); // load mongodb
const uri = process.env.URI;