简体   繁体   English

如何将结果从原始查询(Sequelize)返回到GraphQL

[英]How to return the result from a raw query (Sequelize) to GraphQL

I'm newbie with GraphQL and Sequelize but I have developed a test where I can make querys and get results from Graphiql using the functions of Sequalize, but I'm interested in making more complex querys with querys with several tables. 我是GraphQL和Sequelize的新手,但是我开发了一个测试,可以使用Sequalize的功能进行查询并从Graphiql获取结果,但是我对使用多个表进行查询来进行更复杂的查询感兴趣。

Now, this code works fine: 现在,此代码可以正常工作:

schema.js schema.js

import {
    GraphQLObjectType,
    GraphQLNonNull,
    GraphQLID,
    GraphQLInt,
    GraphQLString,
    GraphQLFloat,
    GraphQLList,
    GraphQLSchema
} from "graphql";
import { DB } from "../db";
import {DateTime} from "../scalar/dateTime";
import {Player} from "./Player";
import {League} from "./League";
import {Group} from "./Group";
import {Season} from "./Season";

const Query = new GraphQLObjectType({
    name: "Query",
    description: "This is root query",
    fields: () => {
        return {
            players: {
                type: GraphQLList(Player),
                args: {
                    id: {
                        type: GraphQLID
                    }
                },
                resolve(root, args){
                    return DB.db.models.tbl003_player.findAll({where: args});
                }
            },
            leagues: {
                type: GraphQLList(League),
                args: {
                    id: {
                        type: GraphQLID
                    }
                },
                resolve(root, args){
                    return DB.db.models.tbl001_league.findAll({where: args});
                }
            },
            groups: {
                type: GraphQLList(Group),
                args: {
                    id: {
                        type: GraphQLID
                    }
                },
                resolve(root, args){
                    return DB.db.models.tbl024_group.findAll({where: args});
                }
            },
            seasons: {
                type:GraphQLList(Season),
                args: {
                    id: {
                        type: GraphQLID
                    } 
                },
                resolve(root, args){
                    return DB.db.models.tbl015_seasons.findAll({where: args})
                }
            }
        }
    }
});

const Schema = new GraphQLSchema({
    query: Query
});

module.exports.Schema = Schema;

So, I would like to make an easy test to know how to return the data from a raw query to GraphQL. 因此,我想进行一次简单的测试,以了解如何将数据从原始查询返回到GraphQL。 I have read that resolve method returns a promise, and I have tried to return a promise with the result of the query, but it doesn't work. 我已经读过resolve方法会返回一个Promise,并且我试图返回一个带有查询结果的Promise,但是它不起作用。

    players: {
        type: GraphQLList(Player),
        args: {
            id: {
                type: GraphQLID
            }
        },
        resolve(root, args){
            DB.db.query("select * from tbl003_player where id = 14",
            {raw: true, type: DB.db.QueryTypes.SELECT}).then((players)=>{
                let myPromise = new Promise((resolve, reject)=>{
                    resolve(players);
                });
                return myPromise;
            }).catch((reject)=>{
                console.log("Error: " + reject);
            });
        }
    },

Therefore, how can I return data from a query with Sequelize to GraphQL? 因此,如何将带有Sequelize的查询的数据返回给GraphQL?

Return the promise you get from sequelize. 归还您从续集获得的承诺。 You are also doing a lot of work that is not required after your promise. 您也做了很多您的诺言之后不需要的工作。 Maybe read more about promises before moving on :) 也许在继续之前先阅读有关诺言的更多信息:)

resolve(root, args){
  return DB.db.query(
    "select * from tbl003_player where id = 14",
    { raw: true, type: DB.db.QueryTypes.SELECT }
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM