Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the features of graphql

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what characteristics graphql has". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "what characteristics graphql has" can help you solve the problem.

Graphql is a query language for APIs that provides an easy-to-understand, complete description of the data in your API, enabling clients to get exactly the data they need and reduce data redundancy.

Compare Rest API

Rest API is a query language of the same kind as GraphQL. Rest represents each resource as a URL, and visiting this URL will get a JSON formatted data response, but this has a disadvantage, you may get data that is irrelevant to the requirements. GraphQL doesn't send past requests specifying which resource is needed. For example, if you need the last name of the author of the book, the Rest API will send you the author's name, because you get the last name by accessing the URL of the author's information. GraphQL will only send the required information. In other words, what resources are needed is determined by the user.

RPC vs REST vs GraphQL

Choosing the right tool at the right time is important, and here are some examples of what tools are best used in scenarios to refer to

1. If it is a Management API, the characteristics of this API are as follows:

Focus on objects and resources There are many different clients that need good discoverability and documentation. This scenario might be better with the REST + JSON API.

2. If it is a Command or Action API, the characteristics of this API are as follows:

Action-oriented or instruction-oriented only requires simple interaction in which case RPC is sufficient.

3. If it is an Internal Micro Services API, the characteristics of this API are as follows:

RPC is still recommended for message-intensive, performance-intensive scenarios.

4. If it is a Micro Services API, the characteristics of this API are as follows:

Message intensive with low overhead is a scenario where RPC or REST can be used.

5. If it is a Data or Mobile API, the characteristics of this API are:

The data type is graphical and hopefully optimized for high-latency scenarios where GraphQL is definitely the best choice.

GraphQL queries and changes--how to query GraphQL servers Take a query result as an example:

{hero {name}}

This query will get a result that is almost identical to its structure:

{"data": {"hero": {"name": "R2-D2"}}}

This is the most important feature of GraphQL, because then you always get the data you want, and the server knows exactly what fields the client is requesting. And queries are interactive in GraphQL, you can change queries as you like and see new results.

You can add parameters to the query, and the results will be more interesting. Parameters can be of many different types. GraphQL comes with a default set of types, but GraphQL servers can declare their own custom set of types, as long as they can be serialized into your transport format.

For example, there is the following query:

{human(id: "1000") {nameheight}}

The results are:

{"data": {"human": {"name": "Luke Skywalker","height": 1.72}}}

In a REST-like system, you can only pass a simple set of parameters-query parameters and URL segments in the request. But in GraphQL, each field and nested object can have its own set of parameters, making GraphQL a perfect substitute for multiple API fetch requests. You can even pass arguments to scalar fields to implement a single conversion on the server side instead of a separate conversion for each client.

About "graphql what features" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report