In this section, you’re going to implement signup and login functionality that allows users to authenticate against your GraphQL server.
User modelThe first thing you need is a way to represent user data in the database. To do so, you can add a User type to your Prisma data model.
You’ll also want to add a relation between the User and the existing Link type to express that Links are posted by Users.
Here, you can further see how Prisma helps you to reason about your data in a way that is more aligned with how it is represented in the underlying database.
Notice how you’re adding a new relation field called postedBy to the Link model that points to a User instance. The User model then has a links field that’s a list of
Links.
To do this, we need to also define the relation by annotating the postedBy field with
the @relation attribute. This is required for every relation field in
your Prisma schema, and all you’re doing is defining what the foreign key of the related table will be. So in this case, we’re adding an extra field to store the id of the User
who posts a Link, and then telling Prisma that postedById will be equal to the id field in the User table.
If this is quite new to you, don’t worry! We’re going to be adding a few of these relational fields and you’ll get the hang of it as you go! For a deeper dive on relations with Prisma, check out these docs.
This is a great time to refresh your memory on the workflow we described for your project at the end of Chapter 4.
After every change you make to the data model, you need to migrate your database and re-generate Prisma Client.
This command has now generated your second migration inside of prisma/migrations, and you can start to see how this becomes a historical record of how your database evolves over
time.
You will notice once again that Prisma Client gets regenerated automatically when you run a migration. Your database structure and Prisma Client has both been updated to reflect the changes for the newly added User model – woohoo! 🎉
User typeNow that you have a User data model in your database schema, it’s time to add a User type to your GraphQL schema. To keep your codebase modular and readable, you are going to put the Nexus definitions and resolver code in a separate src/graphql/User.ts file.
Now just like you did with Link, you are going to write the Nexus type definition for the User type using the objectType function.
Note: You might have noticed the definition of
linksfield in the GraphQL schema is very similar to the definition of thelinksfield in theschema.prismafile. The Prisma schema language bears a lot of resemblences to the GraphQL SDL, making it easy to reason about the properties of both schema files.
Link typeNote that, the relation between User and Link is bidirectional. A user has zero or more links that they have created. Similary a single link is optionally connected to a user who posted the link. To reflect this bidirectional relation, you will add a postedBy field to the existing Link model definition in
your GraphQL schema.