Commit 0012a135 authored by Mikkel Marstein's avatar Mikkel Marstein
Browse files

Merge branch 'master' of...

Merge branch 'master' of https://gitlab.stud.idi.ntnu.no/it2810-h21/team-28/project-3 into komponent-for-display-av-søkeresultater
parents 5a0d6dac 24c2dda4
node_modules
package-lock.json
\ No newline at end of file
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "nodemon ./src/app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"express-graphql": "^0.12.0",
"graphql": "^15.7.0",
"mongoose": "^6.0.12",
"nodemon": "^2.0.14"
}
}
const express = require('express');
const mongoose = require('mongoose');
const graphqlResolver = require('./resolver.js');
const graphqlSchema = require('./schema');
const graphqlHTTP = require('express-graphql').graphqlHTTP;
const app = express();
app.use(
'/graphql',
graphqlHTTP({
schema: graphqlSchema,
rootValue: graphqlResolver,
graphiql: true,
})
);
const uri = 'mongodb://bruker1:gruppe28@it2810-28.idi.ntnu.no:27017/it2810?authSource=it2810';
const options = { useNewUrlParser: true, useUnifiedTopology: true };
mongoose.connect(uri, options)
.then(() => app.listen(3000, console.log("Server is active")))
.catch((error) => {
throw error
});
//Code fetched from https://www.youtube.com/watch?v=9OPP_1eAENg&list=PL4cUxeGkcC9jpvoYriLI0bY8DOgWZfi6u&index=1
import mongoose from 'mongoose';
//mongoose.Promise = global.Promise;
function connectDB(done:Function){
// Connect to MongoDB
mongoose.connect('mongodb://bruker1:gruppe28@it2810-28.idi.ntnu.no:27017/it2810?authSource=it2810');
mongoose.connection.once('open', function() {
console.log('Connection has been made')
done();
}).on('error', function(error) {
console.log('Connection error:', error);
});
};
export default connectDB;
/*beforeEach(function(done){
mongoose.connection.collections.countries.drop(function(){
done();
});
})*/
\ No newline at end of file
//import mongoose from 'mongoose';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema and Model
const CountrySchema = new Schema({
name: String,
native: String,
continent: String,
capital: String,
currency: String,
languages: Array
});
const Country = mongoose.model('country', CountrySchema);
module.exports = Country;
\ No newline at end of file
//import Country from './country';
const Country = require('./country');
module.exports = {
country: async () => {
try {
const countryFetched = await Country.find();
return countryFetched.map((country) => {
return {
...country._doc,
_id: country._id,
};
});
} catch (error) {
throw error;
}
}
}
//import { buildSchema } from 'graphql';
const { buildSchema } = require('graphql');
module.exports = buildSchema(`
type Country {
_id: ID!
name: String!
native: String
continent: String!
capital: String!
currency: String
languages: [String]
}
input CountryInput {
navn: String!
capital: String!
}
type Query {
country:[Country!]
}
type Mutation {
createCountry(country:CountryInput): Country
}
schema {
query: Query
mutation: Mutation
}
`);
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment