1

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

2 に答える 2

0

何をしようとしているのかわかりませんが、これが役立つかもしれません: https://github.com/javascriptiscoolpl/graphQL-tinySQL (コマンドフォルダーと sortBy.js ファイルを確認してください)

于 2016-06-22T20:43:18.630 に答える
0

入力フィールドとして必要なカスタム並べ替えフィールドを使用して Sortable タイプを作成し、 Poll タイプを Sortable インターフェイスを実装するリストにすることができます。

于 2016-06-20T14:22:10.260 に答える