Jos*_*los 3 mysql node.js sequelize.js graphql
我是 GraphQL 和 Sequelize 的新手,但我开发了一个测试,可以使用 Sequalize 的函数进行查询并从 Graphiql 获取结果,但我有兴趣通过多个表的查询来进行更复杂的查询。
现在,这段代码可以正常工作:
架构.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;
Run Code Online (Sandbox Code Playgroud)
因此,我想做一个简单的测试,了解如何将原始查询中的数据返回到 GraphQL。我读过,解析方法返回一个承诺,并且我尝试使用查询结果返回一个承诺,但它不起作用。
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);
});
}
},
Run Code Online (Sandbox Code Playgroud)
因此,如何将 Sequelize 查询中的数据返回到 GraphQL?
返回您从续集获得的承诺。你还做了很多承诺后不需要的工作。也许在继续之前阅读更多有关承诺的内容:)
resolve(root, args){
return DB.db.query(
"select * from tbl003_player where id = 14",
{ raw: true, type: DB.db.QueryTypes.SELECT }
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4472 次 |
| 最近记录: |