2016-08-13 9 views
0

"リスト"を使用してウィンドウ付きページネーションを実装しようとしています。私は、番号付きのページをユーザーに表示する必要があるため、接続にカーソルベースのソリューションは必要ありません。GraphQL:標準リストのウィンドウ化ページ分割の実装

「ユーザー」と「投稿」オブジェクトがあります。「ユーザー」は「投稿」と1対多の関係にあります。

var userType = new GraphQLObjectType({ 
    name: 'User', 
    fields:() => ({ 
     id: globalIdField('User'), 
     posts: { 
      type: new GraphQLList(postType), 
      args: { 
       page:{ 
        type: GraphQLInt, 
        defaultValue: 0 
       } 
      }, 
      resolve: (_, args) => { 
       //code to return relevant result set 
      }, 
     }, 
     totalPosts:{ 
      type: GraphQLInt, 
      resolve:() => { 
       //code to return total count 
      } 
     }, 
    }), 
    interfaces: [nodeInterface], 
}); 


var postType = new GraphQLObjectType({ 
    name: 'Post', 
    fields:() => ({ 
     id: globalIdField('Post'), 
     name: {type: GraphQLString}, 
     //other fields 
    }), 
    interfaces: [nodeInterface], 
}); 

"USERTYPE" で "totalPosts" フィールドに注意してください:graphql-jsのスキーマの使用

は、 はここUSERTYPEとpostTypeのための私のスキーマです。同じページングの必要性を持つ、ユーザーのための他のリストが存在することになるので、フラグメントの中にたくさんの "total {Type}"変数を維持するつもりです。これは、リスト結果内のtotalCountを何らかの形で送信できる場合には解決できます。

https://github.com/facebook/graphql/issues/4この問題では、結果セットにtotalCountを含めるためにListにラッパーを実装する方法について説明しています。

私はこのようなラッパーの作成を試みた:

var postList = new GraphQLObjectType({ 
    name: 'PostList', 
    fields:()=>({ 
     count: { 
      type: GraphQLInt, 
      resolve:()=>getPosts().length //this is total count 
     }, 
     edges: { 
      type: new GraphQLList(postType), 
      resolve:() => { 
       return getPosts() ; // this is results for the page, though I don't know how to use 'page' argument here 
      }, 
     } 

    }), 
    interfaces: [nodeInterface], 
}); 

のが、どのように私はuserTypepostsフィールドにこれを接続する必要がありますか?そして、元のuserTypeのように、このラッパーで 'ページ'引数を使用するにはどうすればいいですか?

答えて

1

これをuserTypeの投稿フィールドにどのように関連付ける必要がありますか?そして、元のuserTypeのように、このラッパーで 'ページ'引数を使用するにはどうすればいいですか?そのラッパーのフィールドを追加し、userType定義に続いて

var postList = new GraphQLObjectType({ 
    name: 'PostList', 
    fields:()=>({ 
     count: { type: GraphQLInt }, 
     edges: { type: new GraphQLList(postType) } 
     // Consider renaming 'edges'. In your case, it's a list, not a 
     // connection. So, it can cause confusion in the long run. 
    }), 
}); 

:あなたが何をしようとして実施する1つの簡単な方法は、このようなダムラッパー型postListを定義することです

下記のような解決関数を定義します。引き数pageについては、フィールドタイプpostsを定義するときにそれを記述するだけです。

posts: { 
    type: postList, 
    args: { 
     page:{ 
      type: GraphQLInt, 
      defaultValue: 0 
     }, 
     ...otherArgs 
    }, 
    resolve: async (_, {page, ...otherArgs}) => { 
     // Get posts for the given page number. 
     const posts = await db.getPosts(page); 

     // Prepare a server-side object, which corresponds to GraphQL 
     // object type postList. 
     const postListObj = { 
      count: posts.length, 
      edges: posts 
     }; 
     // Consider renaming 'edges'. In your case, it's a list, not a 
     // connection. So, it can cause confusion in the long run. 
    }, 
}, 
+0

ありがとうございます!出来た。インターフェイスを削除する必要がありました:[nodeInterface]、postListラッパー – sanandrl

+0

からあなたはそれが有用であることをうれしく思っています。あなたのフィードバックに基づいて、 'post'ラッパーから' interfaces'フィールドを削除します。ありがとう! –

関連する問題