包阅导读总结
1. 关键词:GraphQL、Mutation Queries、Data Return、REST、Advantages
2. 总结:本文介绍了 GraphQ 的基本概念,如突变查询(mutation queries)及其数据返回方式,强调了查询和数据类型在请求与返回时的不同,还阐述了选择 GraphQL 而非 REST 的关键原因在于其查询的明确和声明性质所带来的优势。
3. 主要内容:
– GraphQL 简介
– 介绍什么是 GraphQL
– Mutation Queries
– 示例展示创建电影的 mutation 查询
– 说明指定返回字段以获取创建新记录后的服务器返回数据
– 指出请求数据和返回数据的查询和数据类型不同
– 选择 GraphQL 而非 REST 的原因
– GraphQL 查询具有明确、声明的性质
– 正式定义查询和返回数据的方式有诸多优势
思维导图:
文章地址:https://www.infoworld.com/article/2267992/what-is-graphql-better-apis-by-design.html
文章来源:infoworld.com
作者:InfoWorld
发布时间:2024/7/24 9:00
语言:英文
总字数:1613字
预计阅读时间:7分钟
评分:87分
标签:GraphQL,REST,API 设计,数据查询,模式定义
以下为原文内容
本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com
mutation CreateMovie ($title: String!, $released: Date!) { createMovie (title: $title, released: $released){ id title released }}[submitted data]{ “title”: “Seven Samurai” “released”: “1950”}
All queries, including mutation queries, canreturn data.Here, the list of fields in the curly braces after createMovie
specifies what we want to see returned from the server after a new record is created with this request. The value for theid
field, in this case, would be created by the database; the values for the other fields are submitted in the query.
Another thing to keep in mind is that the queries and data types used to return data are by design different from those used to request data. Mutation queries need to validate incoming data, so the types used for those queries are meant to serve that function. Likewise, the fields used in returned query objects are for display, not validation. If you take in a GraphQL object as returned from a query, it might have fields with circular references or other issues that make it unusable as a query parameter.
Why use GraphQL?
A key reason to choose GraphQL over REST is the explicit, declarative nature of GraphQL queries. Having a formal definition for how queries and returned data should look has advantages aside from being consistent across APIs and implementations.