簡體   English   中英

如何使用GraphQl查詢geojson點?

[英]How to query a geojson point with GraphQl?

我正在使用nodejs,貓鼬和graphql,並且我的數據庫中具有geojson 2D坐標,但是我無法通過graphql查詢它們,它始終返回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);

我對貓鼬返回的數據做了一些控制台記錄:

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 ]

文檔顯示了以這種方式定義的點方案:

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

文檔中的示例特別警告不要將其定義為{ type: String } 我懷疑這樣做(就像您當前的代碼一樣)會導致整個子文檔被序列化為String。 這將解釋您所看到的內容,因為您仍然可以將整個子文檔打印到控制台。 GraphQL會將geometry字段解析為字符串,從技術上講,它是一個對象。 但是,它將無法解析coordinatestype字段,因為這些屬性在String上不存在,從而導致這些字段解析為null。

修復您的貓鼬模式,這也應該修復字段分辨率。

編輯:

另外,您應該像這樣在antenneSchema內部定義geometry

geometry: geoSchema

要么

geometry: {
  type: geoSchema
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM