How does a mutation like createUser in schema know...
# orm-help
j
How does a mutation like createUser in schema know to associate itself with the model User? We don't specifically define the relationship between createUser and User. I could rename createUser to createMember and it'll still work, but how?
n
In the resolver for
createMember
you use the model User
j
@nuno sorry I should've been clearer. Take the following code for example, async createUser(parent, args, { prisma }, info) { if (args.data.password.length < 😎 { throw new Error('Password must be 8 characters or longer.') } const password = await bcrypt.hash(args.data.password, 10) const user = await prisma.mutation.createUser({ data: { ...args.data, password } }) return { user, token: jwt.sign({ userId: user.id }, 'thisisasecret') } }
There is no instantiation like in MongoDB where we use new User({ user: req.userId}). It seems like prisma.mutation.createUser() is somehow automatically associating createUser() with User and creating a table named User in Postgres. I've never had to explicitly establish a relationship between createUser and User in graphql. How is this done?
n
That's handled by the Prisma server. The mutation
createUser
exists because you've defined a type
User
in the datamodel, you've deployed that datamodel and generated the Prisma bindings (the
prisma
object in your code).
Also, you're using Prisma bindings (https://github.com/prisma/prisma-binding) which doesn't have much documentation now, as they're pushing people to use Prisma Client. Take a look at https://www.prisma.io/docs/faq/prisma-client-vs-prisma-bindings-fq06/
j
@nuno Thank you for these references. They're extremely helpful 👍