gpt4 book ai didi

javascript - 将 Apollo GraphQL 查询解析为包含和属性字段的 Sequelize 查询

转载 作者:行者123 更新时间:2023-12-03 22:52:52 25 4
gpt4 key购买 nike

我正在与 Apollo 合作,为我的 GraphQL 请求构建解析器。

为了提高效率,我想获取请求的模型列表(具有相应的嵌套)以及每个模型请求的字段。这样我可以将此信息传递给 sequelize仅在需要时加入模型 - 并且仅提取必要的字段。

解析器会在 info 中传递此信息。目的。

(obj, args, { models }, info) => ...

来自 info对象字段、嵌套模型及其各自的选定字段通过此路径公开:
info.fieldNodes[0].selectionSet.selections

我的问题是将这个结构(以我想象的某种递归方式)解析成一个合理的结构,以便我传递给 sequelize查询。

一个示例 GraphQL 查询:
{
getCompany(id: 1) {
id
name
companyOffices {
id
users {
id
title
userLinks {
id
linkUrl
}
}
}
}
}

info.fieldNodes[0].selectionSet.selections 上生成以下内容(为简洁起见, trim 一些字段):
[
{
"kind":"Field",
"name":{
"kind":"Name",
"value":"id"
}
},
{
"kind":"Field",
"name":{
"kind":"Name",
"value":"name"
}
},
{
"kind":"Field",
"name":{
"kind":"Name",
"value":"companyOffices"
},
"selectionSet":{
"kind":"SelectionSet",
"selections":[
{
"kind":"Field",
"name":{
"kind":"Name",
"value":"id"
}
},
{
"kind":"Field",
"name":{
"kind":"Name",
"value":"users"
},
"selectionSet":{
"kind":"SelectionSet",
"selections":[
{
"kind":"Field",
"name":{
"kind":"Name",
"value":"id"
}
},
{
"kind":"Field",
"name":{
"kind":"Name",
"value":"title"
}
},
{
"kind":"Field",
"name":{
"kind":"Name",
"value":"userLinks"
},
"selectionSet":{
"kind":"SelectionSet",
"selections":[
{
"kind":"Field",
"name":{
"kind":"Name",
"value":"id"
}
},
{
"kind":"Field",
"name":{
"kind":"Name",
"value":"linkUrl"
}
}
]
}
}
]
}
}
]
}
}
]

使用此信息,我想生成如下查询:
  const company = await models.Company.findOne({
where: { id: args.id },
attributes: // DYNAMIC BASED ON QUERY
include: // DYNAMIC BASED ON QUERY
});

所以我需要从上面的 info 中将上面的 GraphQL 查询解析为类似这样的结构目的:
{
attributes: ["id", "name"],
include: [
{
model: "companyOffices",
attributes: ["id"],
include: [
{
model: users,
attributes: ["id", "title"],
include: [{ model: "userLinks", attributes: ["id", "linkUrl"] }]
}
]
}
]
};

但我不清楚如何通过递归来实现这一点,而不会让事情变得困惑。如果有更简单的方法来实现这种动态 include/ attributes我也对此持开放态度。

tl;dr - 我如何将 Apollo GraphQL 查询的模型和字段转移到 includeattributessequelize询问?

最佳答案

它可能会绕过这个问题,但我想知道是否像 graphql-sequelize可以帮助解决这样的事情。如果没有,我用过 this strategy完成您问题的属性部分。

const mapAttributes = (model, { fieldNodes }) => {
// get the fields of the Model (columns of the table)
const columns = new Set(Object.keys(model.rawAttributes));
const requested_attributes = fieldNodes[0].selectionSet.selections
.map(({ name: { value } }) => value);
// filter the attributes against the columns
return requested_attributes.filter(attribute => columns.has(attribute));
};
User: async (
_, // instance (not used in Type resolver)
{ username, ... }, // arguments
{ models: { User }, ... }, // context
info,
) => {
if (username) {
// (only requested columns queried)
return User.findOne({
where: { username },
attributes: mapAttributes(User, info),
});
} ...
}

关于javascript - 将 Apollo GraphQL 查询解析为包含和属性字段的 Sequelize 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56147436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com