如何在一个类型中添加多个解析器(Apollo-server)
How to add multiple resolvers in a type (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)
}
})
});
这里我有多个解析器,id and name
直接从结果中获取。类别和产品有自己的数据库操作。等等。
现在我正在研究 apollo-server
,但我找不到复制它的方法。
例如我有一个类型
type Test {
something: String
yo: String
comment: Comment
}
type Comment {
text: String
createdAt: String
author: User
}
在我的解析器中,我想将其拆分,例如像这样的东西
text: {
something: 'value',
yo: 'value',
comment: getComments();
}
注意:这只是我需要的表示。
您可以添加特定类型的解析器来处理特定字段。假设您有以下架构(基于您的示例):
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
}
我们还假设您有以下数据库方法:
getTest()
returns 具有字段 something
、yo
和
commentId
getComment(id)
returns 具有字段 id
、text
、createdAt
和 userId
的对象
getUser(id)
returns 具有字段 id
、name
和 email
的对象
您的解析器将类似于以下内容:
const resolver = {
// root Query resolver
Query: {
getTest: (root, args, ctx, info) => getTest()
},
// Test resolver
Test: {
// resolves field 'comment' on Test
// the 'parent' arg contains the result from the parent resolver (here, getTest on root)
comment: (parent, args, ctx, info) => getComment(parent.commentId)
},
// Comment resolver
Comment: {
// resolves field 'author' on Comment
// the 'parent' arg contains the result from the parent resolver (here, comment on Test)
author: (parent, args, ctx, info) => getUser(parent.userId)
},
}
希望这对您有所帮助。
我用过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)
}
})
});
这里我有多个解析器,id and name
直接从结果中获取。类别和产品有自己的数据库操作。等等。
现在我正在研究 apollo-server
,但我找不到复制它的方法。
例如我有一个类型
type Test {
something: String
yo: String
comment: Comment
}
type Comment {
text: String
createdAt: String
author: User
}
在我的解析器中,我想将其拆分,例如像这样的东西
text: {
something: 'value',
yo: 'value',
comment: getComments();
}
注意:这只是我需要的表示。
您可以添加特定类型的解析器来处理特定字段。假设您有以下架构(基于您的示例):
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
}
我们还假设您有以下数据库方法:
getTest()
returns 具有字段something
、yo
和commentId
getComment(id)
returns 具有字段id
、text
、createdAt
和userId
的对象
getUser(id)
returns 具有字段id
、name
和email
的对象
您的解析器将类似于以下内容:
const resolver = {
// root Query resolver
Query: {
getTest: (root, args, ctx, info) => getTest()
},
// Test resolver
Test: {
// resolves field 'comment' on Test
// the 'parent' arg contains the result from the parent resolver (here, getTest on root)
comment: (parent, args, ctx, info) => getComment(parent.commentId)
},
// Comment resolver
Comment: {
// resolves field 'author' on Comment
// the 'parent' arg contains the result from the parent resolver (here, comment on Test)
author: (parent, args, ctx, info) => getUser(parent.userId)
},
}
希望这对您有所帮助。