قالب وردپرس درنا توس
Home / Tips and Tricks / What is GraphQL and how is it different from REST? – CloudSavvy IT

What is GraphQL and how is it different from REST? – CloudSavvy IT



GraphQL provides more structure for services that work with complex data

GraphQL is a query language and runtime resolver for APIs, used to provide more structure for services that work with complex data. With GraphQL, clients can only request the data they need and nothing more.

The difference is who sets the schedule

Traditional REST APIs involve an API server that responds to requests with structured data, usually in the form of JSON. If you make another GET request api.com/users/, you can expect to come back in response to something like:

{
  users: [
    {
      name: 'Anthony',
      friends: [...]
    }
  ]
}

Your app will look different, but the point is that the schedule is defined by the REST endpoint. You just made one more request /users/, but you̵

7;ve got all this information back.

However, this works well, if you know what data you are getting back, and that is the standard for most services on the web. But if you make PUT requests, you must provide the API with the correct parameters for it to work, otherwise you will encounter errors. Of course, you can use URL parameters to submit values ​​to the API, such as the user ID to be retrieved, which you can use to make the API more dynamic.

GraphQL APIs do things a little differently. Instead of making individual requests to different endpoints, all of which represent different parts of data, a GraphQL application sends a request to a single source. The request contains a schedule and tells the API which data to send back. This way, you can request complex data in just one request, and the API never sends back more than it is told to, which reduces large response sizes.

For example, if you want to request the e-mail address of a specific user, you can send another GraphQL request api.com end point, with the following question:

{
  users(name: "Anthony") {
    email
  }
}

It does not matter if the “User” object contains fields for other information; because you only requested the email, you will only receive the email. Of course, you still get a JSON object like REST, and you still communicate the same way, but the queries are much more informative.

For apps with complex schedules, this type of API can be very useful for organizational reasons. GraphQL forces you to strictly define your schedule when setting the API, just as static typing forces you to follow strings and types. The data structure is easy to refer to and can be changed. You can easily achieve the same effect with standard REST APIs, but GraphQL implements it.

In addition, GraphQL queries can be more efficient. A popular problem is, for example, the question “friends of friends”. In a REST API, you have to send a request to the user in question and then when you receive the ID of their friends, you send out individual requests for each friend’s information and then filter according to the things you want. Of course, you can (and should) implement a new endpoint to perform this query on the database side of things, but that’s a bandaid on the real issue.

With GraphQL, this question is simple. You just need to specify that you want the name of each friend, and (assuming the backend is configured to handle this properly) the API will handle this interaction naturally, with no special configuration for each request.

{
  users(name: "Anthony") {
    friends {
      name
    }
  }
}

Of course, GraphQL is not without its drawbacks. For simple apps, replacing a basic REST endpoint is far too complicated. With a traditional REST API, you also get the advantage that you can divide different routes. For example, /users and /posts can be managed with separate serverless Lambda functions and can be worked on and updated independently of each other, a concept called microservice backend. For GraphQL, it is much more centralized and more difficult (but not impossible) to divide into separate chunks.

How do you get started?

GraphQL has plenty of server libraries for different languages, but it is often used with JavaScript and NodeJS.

There are two things that a GraphQL query needs – a schema that defines the data structure and types and a resolver that takes an input and returns the value associated with the schema. In resolver, you can do things like retrieve database requests, modify data, and perform all the actions you need, provided everything condenses to a return value that matches the schedule at the end.

For complex apps, the schema and related resolver can be generated automatically based on the structure, but at the end of the day the schema is just a type definition and the resolver is just an object with functions that solve different keys in the schema.

var { graphql, buildSchema } = require('graphql');


var schema = buildSchema(`
  type Query {
    hello: String
  }
`);


var root = {
  hello: () => {
    return 'Hello world!';
  },
};


graphql(schema, '{ hello }', root).then((response) => {
  console.log(response);
});

For more information on setting up a complete GraphQL server, read the GraphQL Guide on installing and using it with Express.


Source link