objection-graphql icon indicating copy to clipboard operation
objection-graphql copied to clipboard

Add ability to format lists with paginated results including totalCount

Open DaKaZ opened this issue 5 years ago • 5 comments

In our use of objection-graphql we were having a hard time doing pagination correctly. The main reason is that we did not receive a totalCount of records available which made layout of the results difficult.

With this PR, you can build your schema passing true into the build function like this:

const rootSchema = builder()
  .allModels(allModels);
  .build(true);

and once that is constructed, you can query like this:

query fetchUsers {
  users(limit: 4, offset: 6) {
    collection {
      id
      name
      handle
      location
      pets {
        name
        type
      }
    }
    totalCount
  } 
}

and the totalCount is returned along with a list of the users.

DaKaZ avatar May 29 '19 18:05 DaKaZ

@nasushkov any chance to get a review/comments on this PR?

DaKaZ avatar Sep 13 '19 15:09 DaKaZ

@DaKaZ This looks nice except that I wouldn't do it with a naked boolean to build(). I'd add .paginated() or at least pass in an object into build(), which could take an argument, the name of the 'collection' and 'totalCount' properties in case someone wanted to use something else.

lookfirst avatar Sep 17 '19 07:09 lookfirst

@lookfirst sorry for the incredibly slow turn around! I just revisited this and had an AH HA moment. I don't know why I did not before, but I move the paginated option into the setBuilderOptions so you can now do this:

const graphQlSchema = graphQlBuilder()
  .model(Movie)
  .model(Person)
  .model(Review)
  .setBuilderOptions({ paginated: true })
  .build(); 

What do you think? I did not add the ability to change the collection or totalCount fields, but that could be subsequent PR if someone really wanted/needed that.

DaKaZ avatar Dec 18 '19 23:12 DaKaZ

@DaKaZ 🤷‍♂ ... I'm super sorry but I moved onto using type-graphql and mikro... pretty much the ideal combination.

lookfirst avatar Dec 19 '19 08:12 lookfirst

@koskimas or @nasushkov - any chance on of you could take a quick look at this and perhaps some of the other PRs that are outstanding?

DaKaZ avatar Dec 19 '19 16:12 DaKaZ