Graphql schema first or code first ?

Im currently building an app, for something i will use but as an hobby, using NestJS, GraphQL and Prisma. I have read about both approaches, and i find the schema first very interesting because it allows everyone to understand the API whatever language is used on the backend. Since im using Typescript and I will be working, I was wondering if the code first approach wouldn't make me write less code specially because Nest has a plugin that allows to not use decorators on every property, which makes the code cleaner. But I dont have enough Graphql experience to understand if with the schema first when i scale the app, if i will write alot more code or not. But i accept all the feedback.
12 Replies
Neto
Neto2y ago
With code first the schema changes according with the businesses changes If you do schema first You change the schema then you change the business rules
Knox
Knox2y ago
So on a basic level, it will depend on if i want to learn the schema language or if i just want to write typecript, right ?
Neto
Neto2y ago
kind of nest code first is bult on top of typegraphql
Knox
Knox2y ago
Will check it out and decide. Nest has an amazing work example with the schema first and prisma integration but their code first doesnt include prisma, unless im blind.
Neto
Neto2y ago
it doesnt but prisma is in a weird place with gql and code first usually you will do something with
import { Example } from '@prisma/client'

@ObjectType()
export class ExampleEntity implements Example {
...
import { Example } from '@prisma/client'

@ObjectType()
export class ExampleEntity implements Example {
...
because prisma gives you the type but for codefirst you also need the class itself to map types
Knox
Knox2y ago
Ahhhh so thats why. Alright i can go from there and checkout the first code approach.
Neto
Neto2y ago
you should not map a prisma entity to a gql entity 1-1 if you can can get pretty nasty real soon create a output entity, and put prisma data on that the "DTO pattern"
Neto
Neto2y ago
Knox
Knox2y ago
Hum it seems easier to make the schema first and generate the code. Maybe i just need more learning on gql After playing around, Code first is making my life so much easier. And testing with GraphQL and Prisma is so easy, loving this stack
Brendonovich
Brendonovich2y ago
Personally I’m a huge fan of using Pothos for doing code first GQL. Way better than nestjs or apollo
Neto
Neto2y ago
same was messing around with it and looks really good just the error plugin is weird because of gql unions
Want results from more Discord servers?
Add your server