Skip to content

Commit

Permalink
update docs on totalCount for when using shared connection objects
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Oct 5, 2023
1 parent 217a0f7 commit b037414
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions website/pages/docs/plugins/prisma.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,8 @@ builder.queryType({
selections.
- `totalCount`: A function for loading the total count for the connection. This will add a
`totalCount` field to the connection object. The `totalCount` method will receive (`connection`,
`args`, `context`, `info`) as arguments
`args`, `context`, `info`) as arguments. Note that this will not work when using a shared
connection object (see details below)

The created connection queries currently support the following combinations of connection arguments:

Expand Down Expand Up @@ -1117,7 +1118,8 @@ builder.prismaNode('User', {
- `query`: A method that accepts the `args` and `context` for the connection field, and returns
filtering and sorting logic that will be merged into the query for the relation.
- `totalCount`: when set to true, this will add a `totalCount` field to the connection object. see
`relationCount` above for more details.
`relationCount` above for more details. Note that this will not work when using a shared
connection object (see details below)

### Indirect relations as connections

Expand Down Expand Up @@ -1290,6 +1292,57 @@ builder.prismaObjectFields('Post', (t) => ({
}));
```

### Total count on shared connection objects

If you are set the `totalCount: true` on a `prismaConnection` or `relatedConnection` field, and are
using a custom connection object, you will need to manually add the `totalCount` field to the
connection object manually. The parent object on the connection will have a `totalCount` property
that is either a the totalCount, or a function that will return the totalCount.

```typescript
const CommentConnection = builder.connectionObject({
type: Comment,
name: 'CommentConnection',
fields: (t) => ({
totalCount: t.int({
resolve: (connection) => {
const { totalCount } = connection as {
totalCount?: number | (() => number | Promise<number>);
};

return typeof totalCount === 'function' ? totalCount() : totalCount;
},
}),
}),
});
```

If you want to add a global `totalCount` field, you can do something similar using
`builder.globalConnectionField`:

```typescript
export const builder = new SchemaBuilder<{
PrismaTypes: PrismaTypes;
Connection: {
totalCount: number | (() => number | Promise<number>);
};
}>({
plugins: [PrismaPlugin, RelayPlugin],
relayOptions: {},
prisma: {
client: db,
},
});

builder.globalConnectionField('totalCount', (t) =>
t.int({
nullable: false,
resolve: (parent) =>
typeof parent.totalCount === 'function' ? parent.totalCount() : parent.totalCount,
}),
);
```

### Relay Utils

#### `parsePrismaCursor` and `formatPrismaCursor`
Expand Down

1 comment on commit b037414

@vercel
Copy link

@vercel vercel bot commented on b037414 Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.