【问题标题】:How do I build a custom graphql query with support for Mongodb aggregations in Strapi?如何在 Strapi 中构建支持 Mongodb 聚合的自定义 graphql 查询?
【发布时间】:2020-09-15 20:16:33
【问题描述】:

我尝试使用 Strapi 构建自定义 graphql 查询,如下所示:

module.exports = {
  definition: `
    type flatOnts {
      site_name: String
      unit_no: String
      firstname: String
      lastName: String
      description: String
      isp_name: String
      serial_number: String
      status: Boolean
    }
  `,
  query: `
    flattenOntObj: [flatOnts]
  `,
  type: {},
  resolver: {
    Query: {
      flattenOntObj: {
        description: "Return a flat ont object",
        resolverOf: "application::onts.onts.aggregate",  
        resolver: async (obj, options, ctx) => {
          const res = await strapi.api.onts.services.onts.aggregate([
            {
              $lookup: {
                from: "onts",
                localField: "ont",
                foreignField: "_id",
                as: "ont_details",
              },
            },
            {
              $replaceRoot: {
                newRoot: {
                  $mergeObjects: [
                    {
                      $arrayElemAt: ["$ont_details", 0],
                    },
                    "$$ROOT",
                  ],
                },
              },
            },
          ]);
          console.log(res);
        },
      },
    },
  },
};

但是,在 Graphql 操场上运行此程序时,我遇到了“禁止”错误。

有什么想法或建议吗?

感谢任何帮助。

【问题讨论】:

    标签: graphql aggregation-framework strapi


    【解决方案1】:

    没关系,我猜对了。我错过了 Strapi 文档中解释它的部分:https://strapi.io/documentation/3.0.0-beta.x/concepts/queries.html#custom-queries

    文件夹中:api/model/services/model.js(我的情况:api/onts/services/ont.js

    module.exports = {
      aggregate: async (aggArray) => {
        const res = await strapi.query("ont").model.aggregate(aggArray);
        return res;
      },
    };
    

    然后在 api/onts/config/schema.graphql.js 中:

    module.exports = {
      definition: `
        type flatOnts {
          site_name: String
          unit_no: String
          firstname: String
          lastName: String
          description: String
          isp_name: String
          serial_number: String
          status: Boolean
        }
      `,
      query: `
        flattenOntObj: [flatOnts]
      `,
      type: {},
      resolver: {
        Query: {
          flattenOntObj: {
            description: "Return a flat ont object",
            // policies: ["plugins::users-permissions.isAuthenticated"],
            resolverOf: "application::onts.onts.find",
            resolver: async (obj, options, ctx) => {
              const aggregationArray = [
                {
                  $lookup: {
                    from: "onts",
                    localField: "ont",
                    foreignField: "_id",
                    as: "ont_details",
                  },
                },
                {
                  $replaceRoot: {
                    newRoot: {
                      $mergeObjects: [
                        {
                          $arrayElemAt: ["$ont_details", 0],
                        },
                        "$$ROOT",
                      ],
                    },
                  },
              ];
              const res = await strapi.api.onts.services.onts.aggregate(
                aggregationArray
              );
              return res;
            },
          },
        },
      },
    };
    

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 2018-03-15
      • 2018-06-04
      • 2020-03-09
      • 2020-10-01
      • 2016-05-04
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多