Created
July 20, 2017 10:55
-
-
Save hardfire/3ff69d3a602bf3c30dcbbca71d67d605 to your computer and use it in GitHub Desktop.
knex+bookshelf+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
/* i created the database manually, but here are the queries to create the db schema in POSTGRES */ | |
CREATE TABLE students (id SERIAL PRIMARY KEY, name TEXT ); | |
CREATE TABLE homeworks (id SERIAL PRIMARY KEY, student_id INT NOT NULL REFERENCES students, subject TEXT); | |
CREATE TABLE pages (id SERIAL PRIMARY KEY, homework_id INT NOT NULL REFERENCES homeworks, content TEXT); |
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
module.exports = require('knex')({ | |
client: 'postgresql', | |
connection: "postgres://postgres:password@db:5432/test", | |
debug: true | |
}); |
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 bookshelfFactory = require('bookshelf'); | |
const knex = require('./knex'); | |
const bookshelf = bookshelfFactory(knex); | |
const Student = bookshelf.Model.extend({ | |
tableName: 'students', | |
homeworks: function() { | |
return this.hasMany(Homework, 'student_id'); | |
}, | |
}); | |
const Homework = bookshelf.Model.extend({ | |
tableName: 'homeworks', | |
student: function() { | |
return this.belongsTo(Student, 'student_id'); | |
}, | |
pages: function() { | |
return this.hasMany(Page, 'homework_id'); | |
}, | |
}); | |
const Page = bookshelf.Model.extend({ | |
tableName: 'pages', | |
homework: function() { | |
return this.belongsTo(Homework, 'homework_id'); | |
}, | |
}); | |
module.exports = { Student, Homework, Page } |
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
{ | |
"name": "test", | |
"private": true, | |
"scripts": { | |
"start": "node server.js" | |
}, | |
"dependencies": { | |
"bookshelf": "^0.10.2", | |
"express": "^4.14.0", | |
"express-graphql": "^0.6.1", | |
"graphql": "^0.8.2", | |
"graphql-bookshelf": "^0.0.8", | |
"graphql-relay": "^0.4.4", | |
"knex": "^0.12.6", | |
"pg": "^6.1.0", | |
"lodash": "^4.17.2" | |
}, | |
"devDependencies": { | |
"nodemon": "^1.11.0" | |
}, | |
"engines": { | |
"node": "7.6.0" | |
} | |
} |
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 { | |
GraphQLInt, | |
GraphQLObjectType, | |
GraphQLSchema, | |
GraphQLList, | |
GraphQLNonNull, | |
GraphQLString, | |
graphql, | |
} = require('graphql') | |
const {Student} = require('./models.js') | |
const BookshelfType = require('graphql-bookshelf').default; | |
var PageType = new GraphQLObjectType(BookshelfType({ | |
name: 'Page', | |
description: 'Pages of a homework', | |
fields: (model) => ({ | |
id: model.attr({ | |
type: new GraphQLNonNull(GraphQLInt), | |
description: 'The id of the page', | |
}), | |
content: model.attr({ | |
type: GraphQLString, | |
description: 'The body of the page.', | |
}) | |
}) | |
})); | |
var HomeworkType = new GraphQLObjectType(BookshelfType({ | |
name: 'Homework', | |
description: 'Homework submitted by the student.', | |
fields: (model) => ({ | |
id: model.attr({ | |
type: new GraphQLNonNull(GraphQLInt), | |
description: 'The id of the homework.', | |
}), | |
pages: model.hasMany({ | |
type: new GraphQLList(PageType), | |
description: 'pages in the homework', | |
}), | |
subject: model.attr({ | |
type: GraphQLString, | |
description: 'subject of the homework', | |
}) | |
}) | |
})); | |
var StudentType = new GraphQLObjectType(BookshelfType({ | |
name: 'Student', | |
description: 'A humble student.', | |
fields: (model) => ({ | |
id: model.attr({ | |
type: new GraphQLNonNull(GraphQLInt), | |
description: 'The id of the student.', | |
}), | |
name: model.attr({ | |
type: GraphQLString, | |
description: 'The id of the student.', | |
}), | |
homeworks: model.hasMany({ | |
type: new GraphQLList(HomeworkType), | |
description: 'All the homework the student has submitted.', | |
}) | |
}) | |
})); | |
var schema = new GraphQLSchema({ | |
query: new GraphQLObjectType({ | |
name: 'RootQueryType', | |
fields: () => ({ | |
viewer: { | |
type: StudentType, | |
args: { | |
id: { | |
type: GraphQLInt, | |
description: 'ID of the current student.' | |
} | |
}, | |
description: 'The current student.', | |
resolve: (source, {id}) => { | |
return Student.where({id}).fetch(); | |
} | |
} | |
}) | |
}) | |
}); | |
module.exports = schema |
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 graphqlHTTP = require('express-graphql') | |
const GraphQLSchema = require('./schema.js') | |
const app = express() | |
app.use('/graphql', graphqlHTTP({ | |
schema:GraphQLSchema, | |
graphiql: true | |
})) | |
app.listen(4000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment