【问题标题】:How to return the result from a raw query (Sequelize) to GraphQL如何将原始查询(Sequelize)的结果返回到 GraphQL
【发布时间】:2018-06-04 18:59:38
【问题描述】:

我是 GraphQL 和 Sequelize 的新手,但我开发了一个测试,我可以在其中使用 Sequalize 的功能进行查询并从 Graphiql 获取结果,但我有兴趣使用带有多个表的查询进行更复杂的查询。

现在,这段代码可以正常工作了:

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;

所以,我想做一个简单的测试来了解如何将原始查询中的数据返回到 GraphQL。我已经读过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);
            });
        }
    },

因此,如何将使用 Sequelize 的查询中的数据返回到 GraphQL?

【问题讨论】:

    标签: mysql node.js sequelize.js graphql


    【解决方案1】:

    返回你从 sequelize 中得到的承诺。你还做了很多承诺之后不需要的工作。也许在继续之前阅读更多关于承诺的信息:)

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

    【讨论】:

    • 天啊!!!有用!!!非常感谢您的帮助@Herku!!!!
    • 我的荣幸!现在由你来理解它为什么起作用:)
    猜你喜欢
    • 1970-01-01
    • 2019-04-16
    • 2015-01-08
    • 1970-01-01
    • 2020-05-20
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多