GraphQl로 geojson 포인트를 쿼리하는 방법은 무엇입니까? (How to query a geojson point with GraphQl?)


문제 설명

GraphQl로 geojson 포인트를 쿼리하는 방법은 무엇입니까? (How to query a geojson point with GraphQl?)

저는 nodejs, mongoose 및 graphql로 작업하고 있으며 내 db에 geojson 2D 좌표가 있지만 graphql을 통해 쿼리할 수 없으며 항상 null을 반환합니다.

https://github.com/의 PointObject 스키마로 시도했습니다. ghengeveld/graphql‑geojson을 사용하여 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에 따르면 /p>

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 ]

참조 솔루션

방법 1:

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
}

(by YadPeDaniel Rearden)

참조 문서

  1. How to query a geojson point with GraphQl? (CC BY‑SA 2.5/3.0/4.0)

#geojson #Mongoose #javascript #node.js #graphql






관련 질문

Geodjango GeoJSON 직렬 변환기 기하학은 항상 'null'입니다. (Geodjango GeoJSON Serializer geometry always 'null')

전단지에서 레이어 켜기/끄기(더 복잡한 시나리오) (Toggle layers on and off in Leaflet (more complex scenario))

RethinkDB r.polygon() - GeoJSON LinearRing에는 최소 4개의 위치가 있어야 합니까? (RethinkDB r.polygon() - GeoJSON LinearRing must have at least four positions?)

Leaflet : GeoJSON 속성에서 GeoJSON 레이어 설정 아이콘 (Leaflet : setting icon for GeoJSON layer from GeoJSON property)

'GeoJsonLayer' 기호를 확인할 수 없습니다. (Cannot resolve symbol 'GeoJsonLayer ')

스키마의 mongoose geojson, "지역 키를 추출할 수 없습니다" 오류 (mongoose geojson in schema, "Can't extract geo keys" error)

Android Google 지도는 GeoJSON을 사용하여 마커를 설정합니다. (Android Google Maps set marker using GeoJSON)

GraphQl로 geojson 포인트를 쿼리하는 방법은 무엇입니까? (How to query a geojson point with GraphQl?)

geojson 포인트 데이터 마커가 전단지 맵에서 클러스터링되지 않습니다. (The geojson point data markers are not clustering in leaflet map)

전단지 geoJSON.onEachFeature는 함수가 아닌가요? (leaflet geoJSON.onEachFeature is not a function?)

Folium에서 특정 국가 강조 표시 (Highlight one specific country in Folium)

RGeo 및 Geojson으로 면적 계산 (Calculating area with RGeo and Geojson)







코멘트