preloader
心得

NPM Join-Monster Package Is Hard to Do Pagination With Graphql-Relay automatically | NPM Join-Monster 套件很難搭配 Graphql-Relay 做到自動提供分頁機制

NPM Join-Monster Package Is Hard to Do Pagination With Graphql-Relay automatically | NPM Join-Monster 套件很難搭配 Graphql-Relay 做到自動提供分頁機制

In my leisure time at July and first part of this August 2021, I have tried to use packages of express, passport, graphql, graphql-passport, join-monster, knex, graphql-relay, and express-session, etc… for building graphql API server. I have successfully built it in early stage.

In summary for quick, it’s impossible for me to create pagination mechanism using graphql-relay package automatically. All of I expect to use graphql-relay and join-monster packages together is they will do most and rest part of pagination and also they do it automatically. I had tried and found it in vain.

When I want to add pagination mechanism to graphql’s query results, I found join-monster pagination offical document says it supports three kinds of pagination:

  • Application-layer paging
  • Integer offset paging
  • Keyset paging

All of them need to do relay-compliant spec for integrating to join-monster, that means type of query result must have two fields: pageInfo and edges. You can refer it for relay spec. content and examples from graphql server spec. for relay spec..

The process I have tried is use connectionDefinitions() from graphql-relay package into model/Query. This function says it

returns a connectionType and its associated edgeType, given a node type.

And I always receive error message from graphiql interface: must provide fields of pageInfo and edges.

 

The excerpted structure of server is:

model/
 |
  --- Query.js
 |
  --- User.js
 |
  --- Program.js
 |
  --- UserProgramRel.js

// Query has the method: users().
// A user may have many programs and UserProgramRel has relation records between User and Program.

Excerpted Content of model/User.js: Notice: This unworks even I add fields of pageInfo and edges, which are I following pagination tutorial of join-monster package.

// skip parts...

const {
  forwardConnectionArgs,
  connectionDefinitions,
} = require("graphql-relay");

const { connectionType: UserProgramRelConnection } = connectionDefinitions({
  nodeType: UserProgramRel,
});

const User = new graphql.GraphQLObjectType({
  // skip parts...

  fields: () => ({
    // skip parts...

    hasProgram: {
      type: UserProgramRelConnection,
      args: forwardConnectionArgs,
      extensions: {
        joinMonster: {
          orderBy: {
            created_at: "desc",
          },
          sqlPaginate: true,
          junction: {
            sqlTable: "xxx",
            sqlJoins: [
              (userTable, userProgramRelTable) =>
                `${userTable}.id = ${userProgramRelTable}.u_id`,
              (userProgramRelTable, programTable) =>
                `${userProgramRelTable}.p_id = ${programTable}.id`,
            ],
          },
        },
      },
    },
  }),
});