-1

I am trying to return a mongodb document upon a graphql query but getting null value. No error is being shown. The mongodb query works fine with mongoshell or mongoose.

Here is the schema, typedef and resolver:

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 query returns the document:

{
  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
}

My suspicion is that I am doing something wrong in the resolver function. I would appreciate any suggestion.

1 Answers1

0

The problem was indeed the resolver function. Following code worked after returning the result from the callback function and using 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;
    },   

  },
};