2016-12-01 68 views
8

鑑於以下apollo服務器graphql架構 我想將它們拆分爲單獨的模塊,因此我不希望根查詢架構下的作者查詢..並希望它分離。所以我將它添加到根查詢如何在apollo graphql服務器中創建嵌套解析器

type Author { 
    id: Int, 
    firstName: String, 
    lastName: String 
} 
type authorQueries { 
    author(firstName: String, lastName: String): Author 
} 

type Query { 
    authorQueries: authorQueries 
} 

schema { 
    query: Query 
} 

之前增加了一個叫做authorQueries另一層我嘗試以下..你可以看到authorQueries添加另一層指定筆者功能之前。

Query: { 
    authorQueries :{ 
     author (root, args) { 
      return {} 
     } 
    } 
} 

當Graphiql查詢,我還添加了額外的一層..

{ 
    authorQueries { 
     author(firstName: "Stephen") { 
      id 
     } 
    } 
} 

我碰到下面的錯誤。

"message": "Resolve function for \"Query.authorQueries\" returned undefined",

回答

12

要創建一個「嵌套」分解器,簡單地定義父字段的返回類型的解析器。在這種情況下,您的authorQueries場返回類型authorQueries,因此您也可以把您的解析器:

{ 
    Query: { authorQueries:() => ({}) }, 
    authorQueries: { 
    author(root, args) { 
     return "Hello, world!"; 
    } 
    } 
} 

所以在技術意義上說,沒有這樣的事,作爲一個嵌套分解 - 每個對象類型都有一個平面列表的字段,並且這些字段具有返回類型。 GraphQL查詢的嵌套是嵌套的結果。

+0

難道沒有更好的方法,比如爲作者類型定義一個解析器,它使用了一些overgiven根參數的值? 當我不喜歡這樣,我有做了一個每次被auther在其他類型的 – user7776232

+0

沒錯使用的解析器,你需要定義一個解析器爲每個返回一個作者,至少在目前的GraphQL實現現場。未來可能會更好。 – stubailo