문제 설명
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 YadPe、Daniel Rearden)