2017-05-27 81 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