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
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
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 ?
kind of
nest code first is bult on top of typegraphql
TypeGraphQL · Modern framework for GraphQL API in Node.js
Modern framework for GraphQL API in Node.js
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.
it doesnt
but prisma is in a weird place with gql and code first
usually you will do something with
because prisma gives you the type
but for codefirst you also need the class itself to map types
Ahhhh so thats why. Alright i can go from there and checkout the first code approach.
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"
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
Personally I’m a huge fan of using Pothos for doing code first GQL. Way better than nestjs or apollo
same
was messing around with it and looks really good
just the error plugin is weird because of gql unions