In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is GraphQL, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.
Those who have written front-ends know that REST-style APIs are suitable for simple additions, deletions and changes. For a slightly more complex association query, it is not suitable: if you design a REST interface, you will generally return all the fields of the association table to meet more similar query requirements. If you design multiple fine-grained interfaces, the front end needs to query many times and assemble the data itself. Coarse-grained interfaces lead to unnecessary data transfer, fine-grained interfaces lead to function explosion, and you've seen JavaScript promises fly.
In this scenario, Facebook engineers open-sourced GraphQL in 2015, allowing the front-end to describe the data form it wants, and the server to return the data structure described by the front-end. Simply put, what the front end wants, what the back end returns, very flexible.
What is GraphQL?
GraphQL is a data-oriented API query style that treats all data as connected graphs, enabling clients to get exactly what they need without any redundancy, making it easier for APIs to evolve over time, and for building powerful developer tools.
For example, the front end only needs the name of hero, so the back end only returns this name, no redundancy:
Another example: the front end needs to display the author's post information, the author's own information, the author's follower list, if it is REST, the front end needs to request these three interfaces, and then assemble:
/user/
Get user (author) details, possibly name.
/user/
/posts Get a list of posts posted by this user.
/user/
/followers Get the user's list of followers.
Now we can get all the information in GraphQL in one query, instead of going back and forth through several asynchronous APIs:
query { User(id: '123') { name posts { title } followers { name } } }
Simple, isn't it?
GraphQL changes
At present, the mainstream of application development is the separation of the front and rear ends, and the front and rear ends only communicate through APIs. The structure is roughly as follows:
If GraphQL is used, then the backend will no longer produce APIs, but will maintain the Controller layer as a Resolver, and the frontend will agree on a schema that will be used to generate interface documents, and the frontend will make its own desired requests directly through the Schema or generated interface documents.
After several years of first-line developers filling in the pit, there are already some good awesome-graphql(https://github.com/chentsulin/awesome-graphql) for development and production, and many languages also provide GraphQL support, such as JavaScript/Nodejs, Java, PHP, Ruby, Python, Go, C#, etc.
GraphQL usage trends
Some of the more prominent companies are converting REST APIs to GraphQL, such as Twitter, IBM, Coursera, Airbnb, Facebook, Github, Ctrip, etc., especially Github, whose v4 version of the external API uses GraphQL only. According to a Twitter boss, many first-tier and second-tier companies in Silicon Valley are trying to find ways to move to GraphQL, but at the same time, GraphQL also needs time to develop, because it needs a lot of front-end restructuring to use it in the production environment, which undoubtedly requires high-level promotion and determination.
Limitations of GraphQL
For simple applications, setting types, queries, etc. can be somewhat complex, but REST can be easier to do.
For complex queries, GraphQL only requires one interface request, so although the number of requests at the network level is optimized, there are no database queries at all, database queries may become performance bottlenecks, and there is still a lot of room for optimization.
Advantages of GraphQL
Retrieves accurate data without redundancy. Typically, when data requirements change, you only need to modify the query, and you don't need to change too much, which improves front-end and back-end development efficiency and allows for rapid product iteration.
GraphQL has been used by teams of all sizes to enhance mobile apps, websites and APIs in different environments and languages. If you're tired of REST APIs, it's time to learn GraphQL.
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.