Build a Graphql Serverless Database for Your Client Apps Using Prisma

Share this video with your friends

Send Tweet
Published 4 years ago
Updated 2 years ago

Tools like Firebase give you a powerful API that connects directly with your client apps. Though powerful, Firebase can be difficult when writing complex data to the database or querying for a complex data structure.

GraphQL, on the other hand, has a powerful API too and an easy Graph spec to query and write to your data source. Unlike Firebase, it doesn't come with a data persistence layer.

Prisma is an open source database layer for GraphQL projects. It serves as both an ORM and also hides the complexity of writing complex resolvers.

This lesson shows how, without a server, you can Firebase-like GraphQL database that your client can talk directly with.

Instructor: [00:00] Make sure you have at least Prisma 1.8 installed by running prisma-v. Then you can initialize in your Prisma project using the prisma-init command and naming the project database. Prisma is completely open source, so you can create a database locally, or you can use a demo server.

[00:22] You can peek where you want the server to be hosted, and then give it a name. We can go with to-do database. Then make the stage div. Let's have a look at the files that prisma-init generates. The database folder has a datamodel.graphql and a prisma.yml file.

[00:45] Data model contains the model for your database. This is where you define the database schema. The prisma.yml is the configuration that connects to your server on the data module. Let's update the todomodule.graphql to have the to-do type.

[01:01] The to-do type can have some fields. The first field we want to have is the title of the to-do, which is of type string, and a completed field, which is of type Boolean. We should also add an ID field to serve as the unique identifier for each record.

[01:31] Let's head back to the command and cd into the database folder, then run prisma deploy to deploy our project.

[01:47] After deploying, you can copy the HTTP link and open in a browser. Pasting it in the browser, it should load the Prisma playground. Then you can inspect the schema by clicking the schema tab. You can see we have the queries where you can fetch the to-dos, each to-do item. It creates to-do mutation, it updates to-do mutation, and so on.

[02:11] In fact, we can test the create to-do mutation by creating a mutation query and calling the create to-do mutation. We can pass in the title, which can be Cook, and completed, of course. It's false by default. We can return the title and the completed field when the mutation is successful.

[02:44] Let's run this mutation by clicking the play button. Here you have it. It returns the created to-do, which is Cook. Of course, it's not completed. We can also query the database to see if that mutation was actually successful.

[03:00] We should have the to-dos, and we should just return the title and the completed field. We can also return the ID as well. There you have it. We have the Cook and the completed is set to false. The ID is actually generated by the database.

[03:22] Without writing any server code, you can actually go ahead and connect your client application to this API endpoint. Then you can start querying your database and creating to-dos.

shadrach Temi-tayo
shadrach Temi-tayo
~ 4 years ago

Great content christian as i was just looking for materials on using GraphQl with prisma