Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code First Approach with Nexus-Prisma #139

Closed
marcjulian opened this issue Mar 6, 2019 · 7 comments
Closed

Code First Approach with Nexus-Prisma #139

marcjulian opened this issue Mar 6, 2019 · 7 comments
Labels

Comments

@marcjulian
Copy link

I'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:

@tanekim88
Copy link

tanekim88 commented Mar 17, 2019

On top of this feature, support for CQRS (with Sagas) + Nexus Prisma to be working together would be incredible. Example code would be highly appreciated.

@kamilmysliwiec
Copy link
Member

Isn't Nest 6.0.0 code first approach (with type-graphql) cleaner than this? See https://docs.nestjs.com/graphql/resolvers-map#code-first

@tanekim88
Copy link

With nexus-prisma, we can first create schema with sdl, and then we can extend/override/modify its methods and fields properties using typescripts, just like the schema first approach in your documentation.

Types generated from sdl using 'prisma deploy' command give a lot of free methods. For example, if I created type User{} in sdl, it would create 'createUser', 'deleteUser', 'updateUser', and many many other methods that are functional in graphql playground.

However, unfortunately, we are not allowed to add type Query{} or type Mutation{} in datamodel.prisma file (in prisma documentation), because 'prisma deploy' commands will generate them for us and create final version of graphql type file that will be used in graphql, and we adding them instead manually will cause duplicate type error during ' prisma deploy' due to conflict with generated types.

So, schema approach documentation in nests's graphql section cannot be done when used with prisma.
It would be awesome if you can create framework that can make this work together smoothly.

@kamilmysliwiec
Copy link
Member

There is no bandwidth to support nexus-prisma

@marcjulian
Copy link
Author

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.

@VienNguyen113
Copy link

@kamilmysliwiec with nexus-prisma we can easily expose prisma generated query+mutation instead of using prisma-binding

@lock
Copy link

lock bot commented Apr 25, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants