带有嵌套模式的 GraphQL 返回 null

问题描述

我正在尝试根据 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
}

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

解决方法

问题确实是解析器功能。以下代码在从回调函数返回结果并使用 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;
    },};