2017-05-27 83 views
0

我正在構建一個graphql應用程序,其中User可以有一堆Entries。這是一個n到m的關係,中間表/邊緣持有關於關係的附加信息。 我graphql模式看起來是這樣的:建模繼電器光標連接

type User { 
    id: ID!, 
    entries(…): [UserEntry] 
} 

type UserEntry { 
    id: ID!, 
    user: User, 
    entry: Entry, 
    someOtherAttribute: String, 
    yetAnotherAttribute: String 
} 

type Entry {...} 

type Query { 
    me: User! 
    userEntry(userEntryId: ID!): UserEntry! 
} 

我想光標樣式分頁添加到entries領域,繼Relay Cursor Connections Specification。 所以我想UserEntry會變成這樣的事情:

type UserEntryEdge { 
    node: Entry, 
    cursor: String, 
    someOtherAttribute: String, 
    yetAnotherEdgeAttribute: String 
} 

但我想還是能夠直接查詢UserEntry/UserEntryEdge,並在這方面例如cursor領域將是無關緊要的。

設計我的graphql模式以便能夠直接查詢邊緣數據的最佳方式是什麼?

(FYI:我使用的是和的NodeJS阿波羅框架套件上都服務器和客戶端)

+0

所以你想直接查詢'Entry'?你直接**下的含義是什麼?你能否提供你想要的示例查詢? – RomanHotsiy

+1

我可以在這裏查詢'Entry'就好了。我想能夠仍然查詢UserEntry!我添加了當前查詢的定義。 謝謝 – kombucha

回答

1

你實際上是建模您的模式是這樣

[User] hasAndBelongsToMany [Entry] 

但是你可以想想它像

[User] hasMany [UserEntry] hasOne [Entry] 
    and 
[Entry] hasMany [UserEntry] hasOne [User] 

所以,去返回到您的GraphQL架構:

type User { 
    id: ID!, 
    userEntriesConnection(…): UserEntriesConnection! 
} 

type UserEntriesConnection { 
    edges: [UserEntryEdge]!, 
    pageInfo: ... 
} 

type UserEntryEdge { 
    cursor: String!, 
    node: UserEntry, 
} 

type UserEntry { 
    id: ID!, 
    user: User, 
    entry: Entry, 
    someOtherAttribute: String, 
    yetAnotherAttribute: String 
} 

type Entry { ... } 

type Query { 
    me: User! 
    userEntry(userEntryId: ID!): UserEntry! 
} 

這是否符合您的需求?查詢會更詳細,因爲有更多的深度,但它更完整。

+0

這似乎是一個很好的解決方法。謝謝 ! – kombucha

0

如果您仍然需要直接查詢UserEntry那麼我想你應該把它作爲一個獨立的類型您的架構,而不是將其轉換爲Edge類型。

所以只要保持UserEntryUserEntryEdge

生成的模式可能看起來像:

type User { 
    id: ID!, 
    entries(…): [UserEntryConnection] 
} 

type UserEntryConnection { 
    edges: [UserEntryEdge] 
    nodes: [Entry] # shortcut (GitHub does like that) 
    pageInfo: PageInfo! 
} 

type UserEntryEdge { 
    node: Entry, 
    cursor: String, 
    info: UserEntry # To not duplicate attributes, you can use UserEntry type here 
} 

type UserEntry { 
    id: ID!, 
    user: User, 
    entry: Foo, 
    someOtherAttribute: String, 
    yetAnotherAttribute: String 
} 

type Entry {...} 

type Query { 
    me: User! 
    userEntry(userEntryId: ID!): UserEntry! # keep userEntry field as is 
} 
+0

我一直在想這樣做,但對我來說,'Entry'可以從'UserEntryEdge'的字段'node'和'UserEntry'的字段'entry'都可以訪問,這讓我覺得有點尷尬。 也許我應該在api中公開一個'Entry'和'UserEntry'的合併版本... 感謝您的幫助 – kombucha