S
S
Sergey Beloventsev2019-12-08 14:52:21
Node.js
Sergey Beloventsev, 2019-12-08 14:52:21

Connect graphql with Sequelize in node.js?

this is how i created the server

const express =require('express');
const  app = express();
const config = require('./config');
const models = require("./models");
const routes = require('./routers/router');
const graphqlHTTP = require('express-graphql');
const cors = require('cors');
const schema = require('./schema/schema');

app.use(cors());

app.use('/graphql',graphqlHTTP({
    schema,
    graphiql:true
}));

app.listen(3015,err=>{
    err ? console.log(error) : console.log('Server started')
});

here is the schematic
const graphql = require('graphql');
const categoryController= require('../controllers/categoryController');
const goodsController = require('../controllers/goodsController');
const GraphQLDate = require('graphql-date')
const { GraphQLObjectType, GraphQLString, GraphQLSchema, GraphQLID, GraphQLInt,GraphQLList,GraphQLNonNull,GraphQLBoolean} = graphql;

const CategoryType=new GraphQLObjectType({
    name:'Category',
    fields:()=>({
        id:{type:GraphQLID},
        name:{type: new GraphQLNonNull(GraphQLString)},
        description:{type: new GraphQLNonNull(GraphQLString)},
        goods:{
            type:GoodsType,
            resolve({id},args){
                return goodsController.goodsCategory({id:id},(err,goods)=>{
                    return goods
                })
            }
        }
    }),
});

const GoodsType = new GraphQLObjectType({
    name: 'Goods',
    fields: () => ({
        id: {type: GraphQLID},
        name: {type: new GraphQLNonNull(GraphQLString)},
        description: {type: new GraphQLNonNull(GraphQLString)},
        category: {
            type: CategoryType,
            resolve({category_id}, args) {
                return categoryController.singleCategory({id:category_id},(err,category)=>{
                   return category;
                });
            }
        }
    })
});

const Query = new GraphQLObjectType({
    name:'Query',
    fields:{
        oneGoods:{
            type:GoodsType,
            args:{id:{type:GraphQLID}},
            resolve(parent,args){
                return goodsController.goodsOne({id:id});
            }
        },
        category:{
            type:CategoryType,
            args:{id:{type:GraphQLID}},
            resolve(parent,{id}){
                return categoryController.singleCategory({id:id});
            }
        },
        categories:{
            type:CategoryType,
            resolve(parent,args){
                return categoryController.graphQLCategories()
            }
        }
    }
});

module.exports = new GraphQLSchema({
    query: Query,
});

here is a controller
const models= require('../models');
const Category= models.category;
const Goods= models.goods;

exports.graphQLCategories=(req,res)=>{
    try{
        cat=  Category.findAll({include:[Goods]}).then( async (categories)=>{
            console.log(1,categories)
            return categories;
        });
        console.log(2, cat);
        return cat;
    }catch (e) {
        throw console.log(e);
    }
};

but the following output order is first processed in console.log(2,cat)
then the query goes to the database, and then console.log(1,categories).
Why this is done, I understand, I do not understand how to do it right. To send the completed data.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question