【问题标题】:GraphQL with nested schema returning null带有嵌套模式的 GraphQL 返回 null
【发布时间】:2021-04-21 16:48:05
【问题描述】:

我试图在 graphql 查询时返回一个 mongodb 文档,但得到空值。没有显示错误。 mongodb 查询适用于 mongoshell 或 mongoose。

这是架构、类型定义和解析器:

const unionSchema = new Schema(
  {
    geometry: mongoose.Schema.Types.MultiPolygon,
    properties: {
      Divi_name: String,
      Dist_name: String,
      Upaz_name: String,
      Uni_namae: String,
    },
  },
  { collection: "unionbounds" }
);


const union = mongoose.model("Union", unionSchema);


const typeDefs = `
  type Query {  
    data: Union
  }
  type Union {
    properties: Props
  }
  type Props{
    Dist_name: String,
  }

`;

const resolvers = {
  Query: {
    data: () => {
      union.findOne(
        {
          geometry: {
            $geoIntersects: {
              $geometry: {
                type: "Point",
                coordinates: [90, 22],
              },
            },
          },
        },
        "properties"
      );
    },

  },
};

Mongoshell 查询返回文档:

{
  properties: {
    Div_ID: '10',
    Dist_ID: '04',
    Upz_ID: '28',
    Un_ID: '95',
    Un_UID: '10042895',
    Divi_name: 'Barisal',
    Dist_name: 'Barguna',
    Upaz_name: 'Barguna Sadar Upazila',
    Uni_name: 'Naltona',
    Area_SqKm: 45.7658667915
  },
  _id: 6001e54a51c6d49215322f94
}

我怀疑我在解析器函数中做错了什么。如有任何建议,我将不胜感激。

【问题讨论】:

    标签: node.js graphql graphql-schema


    【解决方案1】:

    问题确实是解析器功能。以下代码在从回调函数返回结果并使用 async .. await 后工作。

    const resolvers = {
      Query: {
        data: async () => {
          var value;
          await union.findOne(
            {
              geometry: {
                $geoIntersects: {
                  $geometry: {
                    type: "Point",
                    coordinates: [90, 22],
                  },
                },
              },
            },
            "properties",
            function (err, result) {
              value = result;
            }
          );
          return value;
        },   
    
      },
    };
    

    【讨论】:

      猜你喜欢
      • 2020-01-15
      • 1970-01-01
      • 2019-11-25
      • 2017-03-13
      • 2021-06-25
      • 2017-10-01
      • 2020-03-07
      • 2019-09-22
      相关资源
      最近更新 更多