```const PostsConnection = prismaObjectType({
name: "PostConnection",
definition: t => {
t.field("aggregate", {
...t.prismaType.aggregate,
async resolve(root, args, ctx) {
const posts = await ctx.prisma.posts();
const count = posts.length;
return { count };
}
});
}
});
```
const PostsConnection = prismaObjectType({
name: "PostConnection",
definition: t => {
t.prismaFields(["*"]);
t.field("aggregate", {
...t.prismaType.aggregate,
resolve(root) {
const edges = root.edges;
const count = edges.length;
return { count };
}
});
}
});
I am also facing this issue. I'm really excited by Nexus, but this is a very important feature I need in order to fully buy into the lib.
Hey guy, how about this error?
@jaredjasonpeters I'm using your solution, but it's not good enough. It has a performance issue.
the edges property of the root object contains all posts objects, so when you have a lot of posts, executing aggregate will take a lot of time.
In my case, I have ~12000 products and the query
query productsConnection{
productsConnection {
aggregate {
count
}
}
}
take almost 4-5 seconds to complete.
Thanks @bkstorm
Totally agree, posted only as a temporary solution for gaining an aggregate count while the library maintainers solve the aggregate field issue.
Facing the same problem, I'm using a custom resolver to run a SQL query instead but would love to see this issue resolved. What's the ETA on this one @Weakky ?
Hey there everyone,
The problem here is due to prisma-client limitations. We intentionally decided that fetching connection types with the prisma-client would not return aggregate because it's an expensive operation.
I have just given a similar answer to #184, showing how to query more data and pass it to the children. To sum it up, the best work-around for now is to use the $fragment API to query the aggregate with the connections in one query.
However, that's terribly verbose as you have to manually define all the fields you want to fetch, and there are quite a lot with connection types.
We're also losing a lot of type-safety by using fragments, because we cannot infer the return type based on a string. You'll have to make sure that you always manually add or remove fields from the fragment over time.
Given the following datamodel:
type User {
id: ID! @unique
name: String!
posts: [Post!]!
}
type Post {
id: ID! @unique
body: String!
}
Step 1
Override prisma-client connection type
// types.ts
import { UserConnection as PrismaUserConnection, AggregateUser, } from './generated/prisma-client';
export type UserConnection = PrismaUserConnection & { aggregate: AggregateUser }
Step 2
Reference that new type in makePrismaSchema so that nexus uses that custom type
// schema.ts
const schema = makePrismaSchema({
...
typegenAutoConfig: {
sources: [
{
source: path.join(__dirname, './types.ts'), // Point to the `types.ts` file
alias: 'types',
},
/*{
source: path.join(__dirname, './context.ts'),
alias: 'ctx',
},*/
],
/*contextType: 'ctx.Context',*/
},
})
Step 3
Fetch aggregate without making two different calls
// Query.ts
import { UserConnection } from './types'
export const Query = prismaObjectType({
name: 'Query',
definition(t) {
t.field('usersConnection', {
...t.prismaType.usersConnection,
resolve(root, args, ctx) {
return ctx.prisma.usersConnection().$fragment(`fragment Connection on UserConnection {
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
aggregate { count }
edges {
cursor
node { id name }
}
}`)
}
}) as UserConnection
}
})
Step 4
Return aggregate from the parent
export const UserConnection = prismaObjectType({
name: 'UserConnection',
definition(t) {
t.prismaFields(['*'])
t.field('aggregate', {
...t.prismaType.aggregate,
resolve(root, args, ctx) {
return root.aggregate
}
})
}
})
As mentioned in #184, these limitations will be fixed once https://github.com/prisma/rfcs/blob/new-ts-client-rfc/text/0000-new-ts-client.md lands
Hopefully that answer your questions folks 馃檹
This would not be an issue if this library made use of the prisma binding (see #209 for related questions/comments).
What about the prisma-client aggregate function?
import { prismaObjectType } from "nexus-prisma";
export const AttributeConnection = prismaObjectType({
name: "AttributeConnection",
definition(t) {
t.prismaFields(["*"]);
t.field("aggregate", {
type: "AggregateAttribute",
resolve: async (parent, args, ctx) => {
return ctx.prisma.attributesConnection({}).aggregate();
}
});
}
});
You could also pass the args to the the connection.
@Weakky Not sure I've fully understood, but will this bug be fixed in the future?
So that we can use the aggregation without having to add all the boilerplate that should have been generated.
I hope this issue will be fixed soon so I don't have to write boilerplate code.
I really hope so
Hello This solution doesn't work for me I get the following TypeScript Error:
`
Expression produces a union type that is too complex to represent.
Argument of type '{ resolve(root: any, args: any, ctx: any): any; type: "AggregateUser"; args: {}; description: string; list: undefined; nullable: false; }' is not assignable to parameter of type 'NexusOutputFieldConfig<"UserConnection", "aggregate">'.
Types of property 'type' are incompatible.
Type '"AggregateUser"' is not assignable to type '"Query" | "User" | "Role" | "Privilege" | "RoleOrderByInput" | "PrivilegeOrderByInput" | "UserOrderByInput" | "Boolean" | "DateTime" | "Float" | "ID" | "Int" | "String" |
This is the error I get from UserConnection.ts:
Argument of type '{ resolve(root: any, args: any, ctx: any): any; type: "AggregateUser"; args: {}; description: string; list: undefined; nullable: false; }' is not assignable to parameter of type 'NexusOutputFieldConfig<"UserConnection", "aggregate">'.
Types of property 'type' are incompatible.
Type '"AggregateUser"' is not assignable to type '"Query" | "Privilege" | "Role" | "User" | "PrivilegeOrderByInput" | "RoleOrderByInput" | "UserOrderByInput" | "Boolean" | "DateTime" | "Float" | "ID" | "Int" | "String" | NexusObjectTypeDef
`
I honestly thought with all this codeGen it would be very easy to implement Connections, but I've had issues every step of the way.
Is this problem will never be resolved?
i switched from prisma-binding to nexus, because prisma-binding will no longer be supported by prisma2 sadly.
I want to make nexus-prisma work with @Weakky's react-admin data provider https://github.com/Weakky/ra-data-opencrud, but it throws this error: (for resource "Page")
GraphQL error: Unknown prisma-client function for field PageConnection.aggregate"
Example for setting up a custom aggregate resolver while this issue is being resolved.
name: "PostConnection", definition: t => { t.field("aggregate", { ...t.prismaType.aggregate, async resolve(root, args, ctx) { const posts = await ctx.prisma.posts(); const count = posts.length; return { count }; } }); } });
do you know if there is an easy way to add this boilerplate to all connections?
Edit:
i now do something like this:
const enhancedConnections = ["Page", "Store"].map((model) => {
return prismaObjectType({
name: model + "Connection",
definition: (t) => {
t.prismaFields(["*"]);
t.field("aggregate", {
...t.prismaType.aggregate,
resolve(root) {
const edges = root.edges;
const count = edges.length;
return { count };
},
});
},
});
});
const prismaSchema = makePrismaSchema({
types: [Query, Mutation, ...enhancedConnections],
...
unfortunatly, there seems to be no way how to get the list of all models ( ["Page", "Store"]).
There is a models-array exported in the generated prisma-client, but it also contains embedded types and enums... (i raised an issue on prisma repo: https://github.com/prisma/prisma/issues/4867)
Bump.
Hey @macrozone were you able to figure out the way to get the list of all the models for the array?
@5achinJani no. but i did not try. i am looking more towards prisma2 now anyway. For prisma2 and nexus i wrote some helpers that i use in my projects and i am considering to release them on npm
@5achinJani no. but i did not try. i am looking more towards prisma2 now anyway. For prisma2 and nexus i wrote some helpers that i use in my projects and i am considering to release them on npm
Did you end up releasing these? Would love to have a look
I鈥檓 interested in allowing all Prisma 2 types in to Nexus.
Most helpful comment
Example for setting up full functionality for aggregate resolver based on parents query args.
```
const PostsConnection = prismaObjectType({
name: "PostConnection",
definition: t => {
t.prismaFields(["*"]);
t.field("aggregate", {
...t.prismaType.aggregate,
resolve(root) {
const edges = root.edges;
const count = edges.length;
return { count };
}
});
}
});