gpt4 book ai didi

javascript - GraphQL 排序自定义类型

转载 作者:行者123 更新时间:2023-11-28 05:53:25 25 4
gpt4 key购买 nike

我有一个投票应用程序的graphql环境,有用户、民意调查和投票。我在架构中的主查询中有对应用程序中所有民意调查的查询(带有限制选项等...),我还为每个用户提供了一个民意调查字段,我通常希望所有民意调查都是可排序的使用我自己的业务逻辑(例如大多数投票、最近投票等),我不想为民意调查的每个字段定义排序逻辑,并使类型总体上可排序。如何做到这一点?

这是我的代码,我目前仅使用模拟数据库进行测试并确定我需要什么模式。现在就到 GQL。

userType = new GraphQLObjectType({
name: 'User',
description: 'Registered user',
fields: (() => ({
id: {
type: new GraphQLNonNull(GraphQLID)
},
email: {
type: GraphQLString
},
password: {
type: GraphQLString
},
username: {
type: GraphQLString
},
polls: { // this should be sortable
type: new GraphQLList(pollType),
resolve: user => db.getUserPolls(user.id)
},
votes: {
type: new GraphQLList(voteType),
resolve: user => db.getUserVotes(user.id)
}
})),
resolve: id => db.getUsers()[id]
});

pollType = new GraphQLObjectType({
name: 'Poll',
description: 'Poll which can be voted by registered users',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLID)
},
title: {
type: GraphQLString
},
options: {
type: new GraphQLList(GraphQLString),
},
votes: {
type: new GraphQLList(voteType),
resolve: poll => db.getPollVotes(poll.id)
},
author: {
type: userType,
resolve: poll => db.getPollAuthor(poll.id)
},
timestamp: {
type: GraphQLDate
}
})
});

voteType = new GraphQLObjectType({
name: 'Vote',
description: 'User vote on a poll',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLID)
},
user: {
type: userType,
resolve: vote => db.getVoteUser(vote.id)
},
poll: {
type: pollType,
resolve: vote => db.getVotePoll(vote.id)
},
vote: {
type: GraphQLInt
},
timestamp: {
type: GraphQLDate
}
})
});

let schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: () => ({
user: {
type: userType,
args: {
id: {
type: new GraphQLNonNull(GraphQLID)
}
},
resolve: (root, {id}) => db.getUsers()[id]
},
polls: { //this should also be sorted
type: new GraphQLList(pollType),
resolve: () => db.getPolls()
},
votes: {
type: new GraphQLList(voteType),
resolve: () => db.getVotes()
}
})
})
});

最佳答案

您可以尝试使用您想要作为输入字段的自定义排序字段创建可排序类型,并将您的轮询类型设置为实现可排序接口(interface)的列表。

关于javascript - GraphQL 排序自定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37914632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com