1

I'm working with nodejs, mongoose and graphql and I have geojson 2D coordinates in my db but I'm unable to query them throught graphql it always returns null

I've tried with the PointObject schema from https://github.com/ghengeveld/graphql-geojson to replace my Geometry schema but same issue

My code :

const Geometry = new GraphQLObjectType({
  name: 'Geometry',
  fields: () => ({
    type: {
      type: GraphQLString
    },
    coordinates: {
      type: new GraphQLList(GraphQLFloat)
    },
  }),
})

const AntenneType = new GraphQLObjectType({
  name: 'AntenneType',
  fields: () => ({
    _id: {
      type: GraphQLID
    },
    type: {
      type: GraphQLString
    },
    geometry: {
      type: Geometry
    },
    properties: {
      type: Properties
    }
  }),
});

const query = new GraphQLObjectType({
  name: 'Query',
  fields: {
    antennes: {
      type: new GraphQLList(AntenneType),
      resolve: (obj, args, context, info) => {
        return Antenne.find().limit(2) //Antenne is my mongoose model that return same data as in the data.json file
          .then(antennes => {
            console.log(antennes)
            return antennes
          })
      }
    }
  },
});

One set of data :

 {
    "properties": {
      ...
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        2.231666666667,
        49.223611111111
      ]
    },
    "_id": "5cf1901b228293201fe248dc",
    "type": "Feature"
  }

My GraphQl query:

query{
  antennes{
    _id
    properties{
      generation
      adm_lb_nom
    }
    geometry{
      coordinates 
    }
  }
}

And the result :

{
  "data": {
    "antennes": [
      {
        "_id": "5cf1901b228293201fe248dc",
        "properties": {
          "generation": "2G",
          "adm_lb_nom": "SFR"
        },
        "geometry": {
          "coordinates": null
        }
      }
    ]
  }
}

I've also made a gist with my full schemas and data : https://gist.github.com/yadPe/cb397175a8c39021d0dab2208fe22a4d

My mongoose schema (edited according to @DanielRearden answer):

const geoSchema = new Schema({
    type: {
        type: String,
        enum: ['Point'],
        required: true
    },
    coordinates: {
        type: [Number],
        required: true
    }
});

const antenneSchema = new Schema({
    type: String,
    properties: {
        sup_id: Number,
        tpo_id: Number,
        sta_nm_dpt: String,
        adr_nm_cp: Number,
        generation: String,
        coordonnees: String,
        sup_nm_haut: Number,
        adm_lb_nom: String,
        emr_lb_systeme: String,
        coord: String,
        emr_dt_service: String,
        date_maj: String,
        code_insee: String,
        nat_id: Number,
        _id: Number,
        com_cd_insee: String,
        id: Number,
        total_de_adm_lb_nom: String,
        sta_nm_anfr: String
    },
    geometry: {
        geoSchema
    }
}, { collection: '2G_France' });

module.exports = mongoose.model('Antenne', antenneSchema);

I did some console logging of the data returned by mongoose:

Antenne.find().limit(1)
  .then(antennes => {
    //return antennes
    return antennes.map(antenne => {
      console.log(antenne.geometry)
      console.log(typeof antenne.geometry)
      console.log(antenne.geometry.type)
      console.log(antenne.geometry.coordinates)
      const test = JSON.parse(JSON.stringify(antenne.geometry)) // idk why I need to do that here
      console.log(test.type)
      console.log(test.coordinates)
      return antenne
    })
  });

And got the following result:

{ type: 'Point',
  coordinates: [ 2.323333333333, 48.346666666667 ] }
object
undefined
undefined
Point
[ 2.323333333333, 48.346666666667 ]
YadPe
  • 88
  • 1
  • 6
  • Hmm. The issue isn't with your schema. Your code [works as expected](https://codesandbox.io/s/howtoqueryageojsonpointwithgraphql-j0efv) when returning just the sample data you provided. I suspect mongoose is not returning the data in the shape we expect, even though it prints correctly to the console. Can you update the question to include your mongoose schema? – Daniel Rearden Jun 02 '19 at 12:25
  • Thank you @DanielRearden for helping me. I've added my mongoose schema to the question – YadPe Jun 02 '19 at 12:44

1 Answers1

0

The docs show a point schema being defined this way:

const geoSchema = new mongoose.Schema({
  type: {
    type: String,
    enum: ['Point'],
    required: true
  },
  coordinates: {
    type: [Number],
    required: true
  }
});

The example in the docs specifically cautions against defining it as { type: String }. I suspect doing so (like in your current code) causes the entire subdocument to be serialized as a String. This would explain what you're seeing because you'd still be able to print the whole subdocument to the console. GraphQL would resolve the geometry field to a String, which in JavaScript is technically an Object. However, it wouldn't be able to resolve the coordinates or type fields because those properties don't exist on a String, causing those fields to resolve to null.

Fix your mongoose schema and that should also fix the field resolution.

EDIT:

Also, you should define geometry inside antenneSchema like this:

geometry: geoSchema

or

geometry: {
  type: geoSchema
}
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • 1
    I think you pointed out the right issue, but I updated my mongoose schema accordingly to your answer and I still have the same problem. Except that if I do a `JSON.stringify` then a `JSON.parse` to my `antenne.geometry` `object`. I'm able to get a result. I added more information in my question – YadPe Jun 02 '19 at 15:18
  • Yes it works ! Thank you very much for your help and time. Have a great day – YadPe Jun 02 '19 at 18:44