2016-08-22 171 views
0

我试图用graphql获取一个坐标数组。我使用的继电器,猫鼬,和涂鸦猫鼬产生graphql类型从这些猫鼬模型:Graphql查询数组

// Geometry 
const GeometrySchema = new mongoose.Schema({ 
type:   { type: String }, 
coordinates: [ Number ] 
}, { _id: false }); 

// Country 
const CountrySchema = new mongoose.Schema({ 
code:   String, 
name:   String, 
dictionary:  [ { _id: false, value: String } ], 
language:  [ { _id: false, code: String, name: String } ], 
loc:   { type: { type: String }, geometries: [ GeometrySchema ] }, 
creationDate: Date, 
modifiedDate: Date 
}); 

const Country = mongoose.model('Country', CountrySchema); 

这里的产生graphql(graphql /公共事业):

type Country implements Node { 
    code: String 
    name: String 
    dictionary: [CountryDictionary] 
    language: [CountryLanguage] 
    loc: [CountryLoc] 
    creationDate: Date 
    modifiedDate: Date 
    _id: ID 
    id: ID! 
} 

type CountryConnection { 
    pageInfo: PageInfo! 
    edges: [CountryEdge] 
    count: Float 
} 

type CountryDictionary { 
    _id: Generic 
    value: String 
} 

input CountryDictionaryInput { 
    _id: Generic 
    value: String 
} 

type CountryEdge { 
    node: Country 
    cursor: String! 
} 

type CountryLanguage { 
    _id: Generic 
    code: String 
    name: String 
} 

input CountryLanguageInput { 
    _id: Generic 
    code: String 
    name: String 
} 

type CountryLoc { 
    type: String 
    coordinates: [Float] 
} 

input CountryLocInput { 
    type: String 
    coordinates: [Float] 
} 

使用graphiql我能得到这个国家的名称:

{ 
    country(id:"57b48b73f6d183802aa06fe8"){ 
    name 

    } 
} 

我该如何检索loc信息?

+0

你尝试了'{ 国家(ID: “57b48b73f6d183802aa06fe8”){ 名 禄{ 坐标 }} } ' ? –

+0

是的我尝试,我得到这个回应: { “statusCode”:400, “error”:“Bad Request”, “message”:“期望的Iterable,但没有找到一个字段Country.loc。” } – ric

+0

这意味着,问题与服务器端实现有关。检查是否在服务器端正确返回了一组坐标。 –

回答

0

根据您的模式,查询将看起来像这样

{ 
    country(id:"57b48b73f6d183802aa06fe8") { 
    name 
    loc { 
     type 
     coordinates 
    } 
    } 
} 
+0

嗨,感谢您的回复。使用该查询,我得到了以下结果: { “statusCode”:400, “error”:“Bad Request”, “message”:“期望的Iterable,但没有为字段Country.loc找到一个。 } – ric

+0

您的猫鼬'CountrySchema'将'loc'属性定义为单个对象,而不是数组。 而你的类型模式有loc域定义为列表'[CountryLoc]'。 你的模型根本不返回数组 – LordDave

+0

是的,你是对的。猫鼬模型没问题,但用于生成graphql类型的实用程序graffiti-mongoose对子文档有限制。谢谢。 – ric