使用包含和属性字段将 Apollo GraphQL 查询解析为 Sequelize 查询

BML*_*L91 6 javascript recursion sequelize.js apollo graphql

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

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

解析器确实在info对象中传递此信息。

(obj, args, { models }, info) => ...
Run Code Online (Sandbox Code Playgroud)

info对象中,通过以下路径公开字段、嵌套模型及其各自选定的字段:

info.fieldNodes[0].selectionSet.selections
Run Code Online (Sandbox Code Playgroud)

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

GraphQL 查询示例:

{
  getCompany(id: 1) {
    id
    name
    companyOffices {
      id
      users {
        id
        title
        userLinks {
          id
          linkUrl
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

它生成以下内容info.fieldNodes[0].selectionSet.selections(为简洁起见,修剪一些字段):

[
   {
      "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"
                                 }
                              }
                           ]
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
]
Run Code Online (Sandbox Code Playgroud)

使用此信息,我想生成如下查询:

  const company = await models.Company.findOne({
    where: { id: args.id },
    attributes: // DYNAMIC BASED ON QUERY
    include: // DYNAMIC BASED ON QUERY
  });
Run Code Online (Sandbox Code Playgroud)

所以我需要将上面的 GraphQL 查询解析为上面info对象中的这种结构:

{
  attributes: ["id", "name"],
  include: [
    {
      model: "companyOffices",
      attributes: ["id"],
      include: [
        {
          model: users,
          attributes: ["id", "title"],
          include: [{ model: "userLinks", attributes: ["id", "linkUrl"] }]
        }
      ]
    }
  ]
};
Run Code Online (Sandbox Code Playgroud)

但我不清楚如何通过递归实现这一点,而不会让事情变得混乱。如果有更简单的方法来实现这种动态include/attributes我也愿意。

文艺青年最爱的-我怎么能阿波罗GraphQL查询的模型和领域转移到includeattributes一个的sequelize查询?

Don*_*and 1

它可能回避了这个问题,但我想知道像graphql-sequelize这样的东西是否可以帮助解决这样的问题。如果没有,我已经使用此策略来完成您问题的属性部分。

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));
};
Run Code Online (Sandbox Code Playgroud)
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),
      });
    } ... 
  }
Run Code Online (Sandbox Code Playgroud)