Подключите Knex к БД в (db / db.js):
const config = require('../../config/config');
const knex = require('knex')(config.config);
module.exports = knex;
Требуется GraphQL и Knex (БД):
const graphql = require('graphql');
const knex = require('./db/db');
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull
} = graphql;
Допустим, у вас есть тип книги:
// Your book type
const BookType = new GraphQLObjectType({
name: 'Book',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
genre: { type: GraphQLString },
})
});
// Prepare query to get all books, I used Knex query builder with MySQL
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
books: {
type: new GraphQLList(BookType),
async resolve(parent, args){
const books = await knex('books');
return books;
}
},
}
});