Last active
August 16, 2016 21:25
-
-
Save JoeKarlsson/aa7e3be854609f462ac22a85d68b5f57 to your computer and use it in GitHub Desktop.
working demo of sequelize and graphql
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 graphqlHTTP = require('express-graphql') | |
const app = express(); | |
const PORT = process.env.PORT || 3000; | |
const bodyParser = require('body-parser'); | |
const db = require('./models'); | |
const User = db.User; | |
const pool = db.sequelize.connectionManager.pool; | |
// function, the actual executor of the schema | |
const { graphql } = require('graphql'); | |
// app.use(bodyParser.urlencoded({ extended : false })); | |
// I used this route to get users in the DB | |
// app.post('/users', function (req, res) { | |
// User.create({ username: req.body.username }) | |
// .then(function (user) { | |
// res.json(user); | |
// }); | |
// }); | |
// the schema to execute | |
const mySchema = require('./schema'); | |
app.use('/graphql', (req, res) => { | |
return graphqlHTTP({ | |
schema: mySchema, | |
graphiql: true, | |
context: { pool }, // available to all graphql resolve as the third argument | |
})(req, res); | |
}); | |
app.listen(PORT, function() { | |
db.sequelize.sync(); | |
}); |
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 { | |
GraphQLSchema, | |
GraphQLObjectType, | |
GraphQLString, | |
GraphQLInt, | |
GraphQLList, | |
GraphQLID, | |
GraphQLNonNull | |
} = require('graphql'); | |
const {resolver} = require('graphql-sequelize'); | |
const User = require('./models').User; | |
let userType = new GraphQLObjectType({ | |
name: 'User', | |
description: 'A user', | |
fields: { | |
id: { | |
type: new GraphQLNonNull(GraphQLInt), | |
description: 'The id of the user.', | |
}, | |
username: { | |
type: GraphQLString, | |
description: 'The name of the user.', | |
} | |
} | |
}); | |
let mySchema = new GraphQLSchema({ | |
query: new GraphQLObjectType({ | |
name: 'RootQueryType', | |
fields: { | |
user: { | |
type: userType, | |
// args will automatically be mapped to `where` | |
args: { | |
id: { | |
description: 'id of the user', | |
type: new GraphQLNonNull(GraphQLInt) | |
} | |
}, | |
resolve: resolver(User, { | |
include: false // disable auto including of associations based on AST - default: true | |
}) | |
} | |
} | |
}) | |
}); | |
module.exports = mySchema; |
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
module.exports = function(sequelize, DataTypes) { | |
var User = sequelize.define("User", { | |
username: DataTypes.STRING | |
}); | |
return User; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment