投票アプリケーションの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()
}
})
})
});