Last active
February 23, 2018 11:30
-
-
Save phpsmarter/157569650ccceefa3d8cded6b958c707 to your computer and use it in GitHub Desktop.
Apollo-Mongodb-Server
This file contains hidden or 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 Mongoose = require('mongoose'); | |
const PORT = 8080; | |
const app = express(); | |
const { apolloExpress, graphiqlExpress } = require('apollo-server'); | |
const { makeExecutableSchema } = require('graphql-tools'); | |
Mongoose.Promise = global.Promise; | |
Mongoose.connect('mongodb://localhost/apollo', (err) => { | |
if (err) { | |
return err; | |
} | |
return true; | |
}); | |
const seed = require('./seed'); | |
seed(); | |
const Schema = require('./schema'); | |
const Resolvers = require('./resolvers'); | |
const Connectors = require('./connectors'); | |
const executableSchema = makeExecutableSchema({ | |
typeDefs: Schema, | |
resolvers: Resolvers, | |
}); | |
app.use('/graphql', bodyParser.json(), apolloExpress({ | |
schema: executableSchema, | |
context: { | |
constructor: Connectors, | |
}, | |
})); | |
app.use('/graphiql', graphiqlExpress({ | |
endpointURL: '/graphql', | |
})); | |
app.listen(PORT, () => console.log( | |
`GraphQL Server is now running on http://localhost:${PORT}/graphql` | |
)); |
This file contains hidden or 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 PresidentModel = require('./model'); | |
class President { | |
constructor() { | |
this.findPresident = (name) => { | |
const person = PresidentModel.findOne({ name }, (error, data) => { | |
return data; | |
}); | |
return person; | |
}; | |
} | |
} | |
module.exports = { President }; | |
view raw |
This file contains hidden or 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 Mongoose = require('mongoose'); | |
const PresidentSchema = Mongoose.Schema({ | |
name: String, | |
party: String, | |
term: String, | |
}); | |
const President = Mongoose.model('President', PresidentSchema); | |
module.exports = President; |
This file contains hidden or 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 resolveFunctions = { | |
RootQuery: { | |
president(_, { name }, ctx) { | |
const president = new ctx.constructor.President(); | |
return president.findPresident(name); | |
}, | |
}, | |
}; | |
module.exports = resolveFunctions; |
This file contains hidden or 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 typeDefinitions = ` | |
type President { | |
name: String | |
party: String | |
term: String | |
} | |
type RootQuery { | |
president(name: String, party: String, term: String): President | |
} | |
schema { | |
query: RootQuery | |
} | |
`; | |
module.exports = [typeDefinitions]; |
This file contains hidden or 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 request = require('request-promise'); | |
const President = require('./model'); | |
const seed = () => { | |
request('https://mysafeinfo.com/api/data?list=presidents&format=json') | |
.then(res => JSON.parse(res)) | |
.then((res) => { | |
const data = res.map((r) => { | |
const obj = {}; | |
obj.name = r.nm; | |
obj.party = r.pp; | |
obj.term = r.tm; | |
return obj; | |
}); | |
data.forEach((d) => { | |
const president = new President(d); | |
president.save((err, item) => { | |
console.log('saved:', item); | |
}); | |
}); | |
}) | |
.catch((err) => { | |
console.log('err:', err); | |
}); | |
}; | |
module.exports = seed; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment