Vincit / objection-graphql

GraphQL schema generator for objection.js
MIT License
307 stars 34 forks source link

Add ability to format lists with paginated results including totalCount #80

Open DaKaZ opened 5 years ago

DaKaZ commented 5 years ago

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 commented 4 years ago

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

lookfirst commented 4 years ago

@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.

DaKaZ commented 4 years ago

@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.

lookfirst commented 4 years ago

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

DaKaZ commented 4 years ago

@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?