DEV Community

GraphQL Code-First Approach with NestJS 7

Marc Stammerjohann on March 31, 2020

Recently the release of NestJS 7 was announced with amazing updates to the whole framework including the @nestjs/graphql ❤️ package. We create a G...
Collapse
 
dadooda profile image
Alex Fortuna

Hey Marc,

Great post, thank you. There's a question many people ask, although it's not that easy to find answers for:

How can we (if we can) document the GraphQL schema (fields, queries, mutations) using code first approach?

I use code first, but I also want "DOCS" be more meaningful in the playground. If you know it and you can demo it in your examples, that'd be insanely awesome.

Cheers,
Alex

Collapse
 
marcjulian profile image
Marc Stammerjohann • Edited

Hi Alex,

you can add a description option to the code first decorators @Query, @Mutation, @ObjectType, @Field and more.

Here is an example how to add a description:

@ObjectType({ description: 'Authentication Payload' })
export class Auth {
  @Field({ description: 'JWT Bearer Token for Authentication' })
  token: string;

   ....
}

This documentation is added to the generated graphql schema as comment """DESCRIPTION""" to the field or query, this is getting picked up by the graphql playground.

Let me know if this helps you.

Collapse
 
dadooda profile image
Alex Fortuna

Hey Marc,

Thank you for the info.

The recipe seems to work, just tried it in NestJS 7. Apollo's GraphQL Playground doesn't make descriptions quite visible though. But they are there (the "DOCS" tab).

The set of supported markups is yet to be discovered. Markdown's code seems to work, don't know about the rest.

Cheers!

Thread Thread
 
marcjulian profile image
Marc Stammerjohann

Good idea to add markdown to the docs that helps too.

I am glad I could help :)

Collapse
 
thavoo profile image
Gustavo Herrera

Nice, thanks for you post.

Collapse
 
marcjulian profile image
Marc Stammerjohann

Thank you!

Collapse
 
humayunkabir profile image
Humayun Kabir

I am facing a weird problem. I am using @nestjs/graphql/plugin. Sometimes It generates the schema properly and sometimes it doesn't. Is there any issue with this plugin?

Collapse
 
marcjulian profile image
Marc Stammerjohann

Hi Humayun, can you provide a repo for me to reproduce? Maybe two eyes more can find the problem. I have not found any issues with the plugin until now.

Collapse
 
mark600211 profile image
mark600211

and the main question. can I use schema.gql file auto generated when I use code first approach as schema.prisma???

Collapse
 
marcjulian profile image
Marc Stammerjohann

I am not sure if I understand your question.

You use Graphql code first approach to generate your graphql api which generates the schema.gql.

The schema.prisma describes your database and is separate to the graphql endpoint. You have to write your prisma schema your self.

That means you can use both at the same time. Code first to generate schema.gql and write the database schema.prisma manually.

Does that answer your question?

Collapse
 
mark600211 profile image
mark600211

thanks for answer. I get it yesterday, when start do it, not read) But this good idea, I think: we have slightly different between our schema.gql and schema.prisma. Cause we would be generate prisma use our ObjectType objects. it would be pretty nice.

Collapse
 
mark600211 profile image
mark600211 • Edited

Hi Marc,

Great post. I saw your repository and found out that you use Prisma Client there. But in official docs of nest they tell about Prisma bindings. What's the different?

Collapse
 
marcjulian profile image
Marc Stammerjohann • Edited

Hi Mark, nice name by the way 😄!

Yes the nest docs describe how to integrate with prisma bindings which are used for Prisma 1. I am using the Prisma client from Prisma 2.

Here you can read a bit more about Prisma history prisma.io/blog/prisma-and-graphql-... which explains the terms.