graphql-nexus / nexus-plugin-prisma

Deprecated
MIT License
828 stars 118 forks source link

Relay compatible pagination #334

Open kleinspire opened 5 years ago

kleinspire commented 5 years ago

Following code...

// User.ts
import { objectType } from '@prisma/nexus';

export const User = objectType({
  name: 'User',
  definition(t) {
    t.string('username', { nullable: false });
    t.string('name', { nullable: false });
    t.string('email', { nullable: false });
    t.list.field('posts', {
      type: 'Post',
      nullable: true,
      resolve: (parent, args, ctx) => {
        return ctx.photon.posts.findMany({
          where: {
            author: parent.id,
          },
        });
      },
    });
  },
});
// Query.ts
import { queryType, idArg } from '@prisma/nexus';
import { getUserFromToken } from '../../utils';

export const Query = queryType({
  definition(t) {
    t.field('me', {
      type: 'User',
      nullable: true,
      resolve: async (parent, args, ctx) => {
        return getUserFromToken(ctx);
      },
    });

    t.crud.findManyPost({
      alias: 'posts',
    });
  },
});

...generates this:

### schema.graphql
type Query {
  me: User
  posts(after: String, before: String, first: Int, last: Int, skip: Int): [Post!]
}

type User {
  email: String!
  name: String!
  posts: [Post!]
  username: String!
}

I suppose there is no way to automatically generate Relay style connections for paginating lists so that I can have:

### schema.graphql
type User {
  email: String!
  name: String!
  posts(before: String, after: String, first: Int, last: Int): PostConnection!
  username: String!
}

type PostConnection {
  edges: [PostEdge!]
  pageInfo: PageInfo!
}

type PostEdge {
  node: Post!
  cursor: String!
}

type PageInfo {
  hasNextPage: Boolean!
  hasPreviousPage: Boolean!
  endCursor: String!
  startCursor: String!
}

interface Node {
  id: ID!
}

type Post implements Node {
  id: ID!,
  author: String!
  title: String
  content: String
}
reinvanimschoot commented 5 years ago

I was wondering this as well. I miss this kind of pagination in Prisma 2.

gotexis commented 5 years ago

So there is no pagination solution currently?

tafelito commented 5 years ago

this doesn't work in prisma v1 either, even tho the connection type is generated

johannesschobel commented 5 years ago

i don't know, if this is a prisma2 or nexus2 issue.. I believe, that this has nothing to do with Prisma2 itself. As far as i have understood prisma, it is a database structure language and automatically generated client to access the database. This has - at least in my opinion - nothing to do with "how you query the data" (i.e., the relay pagination style).

the prisma-nexus client, in turn, should generate the required Connection elements and make them visible to the graphql server..

All the best

soqt commented 4 years ago

Any solutions on this issue yet?

nikolasburk commented 4 years ago

Hey everyone, I just want to quickly highlight @ctrlplusb's work who wrote a helper to support Relay Cursor Connection Specification type querying: https://gist.github.com/ctrlplusb/17b5a1bd1736b5ba547bb15b3dd5be29 🚀

belloenny commented 4 years ago

If you're using nexus version 0.12.0 upwards, you should the documentation here. It has been implemented and it works