What is your recommended TypeScript setup to gener...
# orm-help
k
What is your recommended TypeScript setup to generate types when working with
graphql-yoga
server?
graphqlgen
or
graphql codegen
? I’m starting a new project and want to hear some thoughts.
I’ve tried a boilerplate from
prisma examples
and
graphql-boilerplates
repos and they all work differently
n
Definitely go with
graphqlgen
, it's the more recent tool for generating proper typings 🙂
k
Cool, thanks @nikolasburk
@nikolasburk none of
prisma-examples
import schema from
./generated/prisma.graphql
(cause it’s not generated through graphql-cli) and duplicate types instead. Is it really the best practice? I want to share certain schemas between prisma and yoga.
n
This is possible and you can certainly use
graphlq-import
to import types from
prisma.graphql
into
schema.graphql
. I recently had a similar discussion in the #prisma-client channel: https://prisma.slack.com/archives/CCWDULGUW/p1542965781037400?thread_ts=1542861207.027200&cid=CCWDULGUW
Let me know if that helps 🙂
k
Great, I’ll give it a try and let you know if I have any issues. It’d be cool to see
graphql-import
example in
prisma-examples
repo in the future 🙂
I had a look at
graphql-schema-delegation
example and it’s probably what I’m most after at the moment since the API is tightly coupled with Prisma, however, I don’t like the DX of lack of type safety. I’m not sure how to implement a hybrid setup that uses
graphqlgen
(with all type definitions) while delegating parts of the schema to Prisma directly. I don’t want to expose the full Prisma schema either so I’m not sure if
graphql-import
helps here? I essentially don’t want to write resolvers for each type and non-scalar field as this would end up in loads of boilerplate-like code, such as:
Copy code
import { UserResolvers } from '../generated/graphqlgen'

export const User: UserResolvers.Type = {
  ...UserResolvers.defaultResolvers,

  permissions: (parent, args, ctx) =>
    ctx.prisma.user({ id: parent.id }).permissions()
}
Any thoughts @nikolasburk?
n
Yeah, so there basically are 2 ways of building GraphQL servers with Prisma at the moment: 1. Using the Prisma client: Full type-safety for your resolvers (all input arguments and return value can be typed); requires lots of boilerplate, so it's recommended to use
graphqlgen
2. Using Prisma bindings (schema delegation): Resolvers can't be entirely type-safe since the typings depend on the
info
object which is dynamic. For your current use case, it might be the more suitable way to go for now since you can use
forwardTo
to easily proxy the Prisma operations you want to expose. Hope that helps!
Also note that we're aware that none of the solutions is ideal if you just want to expose a few Prisma operations through your own application server, we're already working on tooling that further improve the workflows here!
k
Thanks for such detailed answer! I’ll move back to Prisma bindings then and wait for the news 🙂
@nikolasburk I’ve noticed that the typescript schema-delegation example has been removed from the repo. I’m currently following typescript-advanced boilerplate from https://github.com/graphql-boilerplates/typescript-graphql-server/tree/master/advanced. Is it still the recommended way of using Prisma bindings?