Code First Approach with Nexus-Prisma
See original GitHub issueI’m submitting a…
[ ] Regression
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
Nestjs Graphql uses the “Schema-First” approach to define the Graphql Api. Prisma and Prisma-Client can be used in connection with Nestjs to build the Graphql Api.
Expected behavior
Nestjs Graphql can use the “Resolver/Code-First” approach to define the Graphql Api.
Prisma released the nexus-prisma plugin to generate the Graphql Schema based on the implemented resolvers using the prisma-client under the hood and inside the resolvers. Furthermore, it is compatible with e.g. apollo-server
.
Minimal reproduction of the problem with instructions
I created a project with nestjs, prisma-client and nexus-prisma. You can find it here https://github.com/marcjulian/nestjs-prisma-nexus. The Schema generation with nexus-prisma works for exposing and hiding queries and fields. However, I couldn’t add a custom query using Nestjs Resolver
and Query
.
The Schema is setup inside GraphqlConfigService
located in src/prisma
.
Here is an example:
I am exposing all queries from the prisma api to the nestjs api and I am adding a custom helloWorld
query.
const Query = prismaObjectType({
name: 'Query',
definition(t) {
t.prismaFields(['*'])
t.field('helloWorld', {
type: 'String',
resolve: (_, { id }, ctx) =>{
return 'Hello World'
}
});
}
});
You can call the query from the Nestjs Playground and you receive Hello World
.
Instead of defining the custom query inside of GraphqlConfigService
I would like to use a resolver
for example AppResolver
:
@Resolver()
export class AppResolver {
constructor() {
}
@Query()
helloWorld() {
return 'Hello World!';
}
}
The approach with the Resolver
doesn’t work because nexus-prisma doesn’t know about the custom Query
. How can I use a Query
inside a Resolver
and use nexus-prisma to generate the custom query to the Graphql Schema?
What is the motivation / use case for changing the behavior?
The motivation to use Resolver
and Query
, Mutation
and Subscription
is to use the existing features of Nestjs such as guards
andinterceptors
.
Environment
Nest version: X.Y.Z
For Tooling issues:
- Node version: XX
- Platform:
Others:
Issue Analytics
- State:
- Created 5 years ago
- Reactions:18
- Comments:7 (2 by maintainers)
Sorry for my late reply. Yes the code first approach of 6.0.0 with
type-graphql
is much cleaner. I like the concept of decorators.@kamilmysliwiec with nexus-prisma we can easily expose prisma generated query+mutation instead of using prisma-binding