2016-08-22 82 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