简体   繁体   中英

Can prisma achieve the same things as graphql?

I'm having a hard time understanding what graphql can achieve on top of prisma besides having a singular endpoint.

I've been hearing that the main selling point of graphql is the fact that you are able to query for the specific fields you want to receive, but can't that also be achieved with prisma using the "select" property?

const users = await user.findMany({
    select: {
        email: true,
    },
});

Thank you for your answers!

I understand your confusion. I'll try to make the difference between Prisma and GraphQL clearer for you.

Prisma

Prisma is an ORM like tool. This means that it helps you search for data in your database. Helps you create queries, join data and etc. Prisma is a tool that lives in your back-end (usually) and helps you find the data that you will send to your front-end or mobile application.

GraphQL

GraphQL is a way to communicate with your back-end. So let's say that you want get the name of a user based on its id. On a REST API (that is the more common way) you would send a GET request to the server, and it would respond with a JSON with all of the data for this user.

// Imagine this is your request to the server
await api.get(http://myserver.com/user/1)

// Imagine this is your JSON response
{
  name: "Person",
  age: 24,
  address: "Avenue Street, number 7",
  ...
}

You see that even though you just wanted the name of the user you got all of the data, when using a REST API. But with GraphQL that is not the case. You can request for only the data that you want.

// Your request to the server would look like this
{
  GetUserById(1) {
    name
  }
}

// In the response you would be getting a JSON with only the data that you requested

{
  name: "Person"
}

That is what GraphQL is used for, requesting specific data to your back-end and receiving only what you need. In both cases, REST API and GraphQL, you can use Prisma. It will help you search in your database for the data that was requested.

If you want to know more about Prisma you can check their website where you will find more about how it works and how to use it.

If you want to know more about GraphQL you can check their FAQ page

You can also learn how to use Prisma and GraphQL together

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM