如何使用 GraphQl 查询 geojson 点?

How to query a geojson point with GraphQl?

我正在使用 nodejs、mongoose 和 graphql,我的数据库中有 geojson 2D 坐标,但我无法通过 graphql 始终查询它们 returns null

我尝试使用 https://github.com/ghengeveld/graphql-geojson 中的 PointObject 架构来替换我的 Geometry 架构,但同样的问题

我的代码:

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
          })
      }
    }
  },
});

一组数据:

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

我的 GraphQl 查询:

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

结果:

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

我还用我的完整模式和数据做了一个要点:https://gist.github.com/yadPe/cb397175a8c39021d0dab2208fe22a4d

我的猫鼬模式(根据@DanielRearden 的回答编辑):

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);

我对 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
    })
  });

得到如下结果:

{ type: 'Point',
  coordinates: [ 2.323333333333, 48.346666666667 ] }
object
undefined
undefined
Point
[ 2.323333333333, 48.346666666667 ]

docs 显示了以这种方式定义的点模式:

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

文档中的示例特别警告不要将其定义为 { type: String }。我怀疑这样做(就像在您当前的代码中一样)会导致整个子文档被序列化为一个字符串。这将解释您所看到的内容,因为您仍然可以将整个子文档打印到控制台。 GraphQL 会将 geometry 字段解析为一个字符串,在 JavaScript 中它在技术上是一个对象。但是,它无法解析 coordinatestype 字段,因为字符串中不存在这些属性,导致这些字段解析为 null。

修复你的 mongoose 模式,这也应该修复字段分辨率。

编辑:

此外,您应该在 antenneSchema 中定义 geometry,如下所示:

geometry: geoSchema

geometry: {
  type: geoSchema
}