Sar*_*hah 5 graphql apollo-server
我曾经使用过express-graphql并且我曾经做过这样的事情。
const SubCategoryType = new ObjectType({
name: 'SubCategory',
fields: () => ({
id: { type: IDType },
name: { type: StringType },
category: {
type: CategoryType,
resolve: parentValue => getCategoryBySubCategory(parentValue.id)
},
products: {
type: List(ProductType),
resolve: parentValue => getProductsBySubCategory(parentValue.id)
}
})
});
Run Code Online (Sandbox Code Playgroud)
这里我有多个解析器,id and name直接从结果中获取。并且类别和产品有自己的数据库操作。等等。现在我正在努力apollo-server,我找不到复制它的方法。
例如我有一个类型
type Test {
something: String
yo: String
comment: Comment
}
type Comment {
text: String
createdAt: String
author: User
}
Run Code Online (Sandbox Code Playgroud)
在我的解析器中,我想将其拆分,例如这样的
text: {
something: 'value',
yo: 'value',
comment: getComments();
}
Run Code Online (Sandbox Code Playgroud)
注意:这只是我需要的代表。
您可以添加特定于类型的解析器来处理特定字段。假设您有以下架构(基于您的示例):
type Query {
getTest: Test
}
type Test {
id: Int!
something: String
yo: String
comment: Comment
}
type Comment {
id: Int!
text: String
createdAt: String
author: User
}
type User {
id: Int!
name: String
email: String
}
Run Code Online (Sandbox Code Playgroud)
我们还假设您有以下数据库方法:
getTest()返回一个带有字段的对象something,yo并且
commentIdgetComment(id)返回与字段的对象id,text,createdAt和userIdgetUser(id)返回一个带有字段的对象id,name并且email您的解析器将类似于以下内容:
type Query {
getTest: Test
}
type Test {
id: Int!
something: String
yo: String
comment: Comment
}
type Comment {
id: Int!
text: String
createdAt: String
author: User
}
type User {
id: Int!
name: String
email: String
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
2992 次 |
| 最近记录: |