1. Overview
1.概述
GraphQL has been widely used as a pattern of communication in web services. The basic premise of GraphQL is to be flexible in use by client-side applications.
GraphQL已被广泛用作Web服务中的一种通信模式。GraphQL的基本前提是由客户端应用程序灵活使用。。
In this tutorial, we’ll look into another aspect of flexibility. We’ll also explore how a GraphQL field can be exposed with a different name.
在本教程中,我们将探究灵活性的另一个方面。我们还将探讨如何用不同的名称来暴露GraphQL字段。
2. GraphQL Schema
2.GraphQL方案
Let us take an example of a blog having Posts by different Authors. The GraphQL schema looks something like this:
让我们以一个博客为例,它有不同作者的帖子。GraphQL模式看起来是这样的。
query {
recentPosts(count: 1, offset: 0){
id
title
text
category
author {
id
name
thumbnail
}
}
}
type Post {
id: ID!
title: String!
text: String!
category: String
authorId: Author!
}
type Author {
id: ID!
name: String!
thumbnail: String
posts: [Post]!
}
Here we can fetch recent posts. Every post will be accompanied by its author. The result of the query is as follows:
在这里,我们可以取到最近的帖子。每个帖子都会有其作者。查询的结果如下。
{
"data": {
"recentPosts": [
{
"id": "Post00",
"title": "Post 0:0",
"text": "Post 0 + by author 0",
"category": null,
"author": {
"id": "Author0",
"name": "Author 0",
"thumbnail": "http://example.com/authors/0"
}
}
]
}
}
3. Exposing GraphQL Field with a Different Name
3.用不同的名字暴露GraphQL字段
A client-side application may require to use of the field first_author. Right now, it’s using the author. To accommodate this requirement, we have two solutions:
一个客户端应用程序可能需要使用字段first_author.。现在,它正在使用author。为了适应这一要求,我们有两个解决方案。
- Change the definition of the schema in the GraphQL server
- Make use of the concept of Aliases in GraphQL
Let’s look at both one by one.
让我们逐一看一下这两方面的情况。
3.1. Changing Schema
3.1.改变模式
Let’s update the schema definition of the post:
让我们更新一下帖子的模式定义。
type Post {
id: ID!
title: String!
text: String!
category: String
first_author: Author!
}
The author isn’t a trivial field. It’s a complex one. We’ll also have to update the handler method to accommodate this change.
作者并不是一个微不足道的领域。它是一个复杂的字段。我们还必须更新处理方法以适应这一变化。
The method author(Post post), marked with @SchemaMapping in PostController, will need to be updated to getFirst_author(Post post). Alternatively, the field attribute has to be added in the @SchemaMapping to reflect the new field name.
方法author(Post post),用@SchemaMapping标记在PostController中。需要更新为getFirst_author(Post post)。或者,必须在@SchemaMapping中添加field属性以反映新的字段名。。
Here’s the query:
下面是询问的内容。
query{
recentPosts(count: 1,offset: 0){
id
title
text
category
first_author{
id
name
thumbnail
}
}
}
The result of the above query is as follows:
上述查询的结果如下。
{
"data": {
"recentPosts": [
{
"id": "Post00",
"title": "Post 0:0",
"text": "Post 0 + by author 0",
"category": null,
"first_author": {
"id": "Author0",
"name": "Author 0",
"thumbnail": "http://example.com/authors/0"
}
}
]
}
}
This solution has two main issues:
这个解决方案有两个主要问题。
- It is introducing changes to the schema and server-side implementation
- It is forcing other client-side applications to follow this updated schema definition
These issues contradict the flexibility feature GraphQL offers.
这些问题与GraphQL提供的灵活性功能相矛盾。
3.2. GraphQL Aliases
3.2 GraphQL别名
Aliases, in GraphQL, let us rename the result of a field to anything we want without changing the schema definition. To introduce an alias in a query, the alias and colon symbol (:) have to precede the GraphQL field.
在GraphQL中,别名让我们将一个字段的结果重命名为我们想要的任何东西,而无需改变模式定义。要在查询中引入一个别名,别名和冒号(:)必须在GraphQL字段之前。
Here’s the demonstration of the query:
下面是查询的演示。
query {
recentPosts(count: 1,offset: 0) {
id
title
text
category
first_author:author {
id
name
thumbnail
}
}
}
The result of the above query is as follows:
上述查询的结果如下。
{
"data": {
"recentPosts": [
{
"id": "Post00",
"title": "Post 0:0",
"text": "Post 0 + by author 0",
"category": null,
"first_author": {
"id": "Author0",
"name": "Author 0",
"thumbnail": "http://example.com/authors/0"
}
}
]
}
}
Let’s notice that the query itself is requesting the first post. Another client-side application may request to have first_post instead of recentPosts. Again, Aliases will come to the rescue.
让我们注意到,查询本身是在请求第一个帖子。另一个客户端应用程序可能要求获得first_post 而不是recentPosts.再次,Aliases会来拯救。
query {
first_post: recentPosts(count: 1,offset: 0) {
id
title
text
category
author {
id
name
thumbnail
}
}
}
The result of the above query is as follows:
上述查询的结果如下。
{
"data": {
"first_post": [
{
"id": "Post00",
"title": "Post 0:0",
"text": "Post 0 + by author 0",
"category": null,
"author": {
"id": "Author0",
"name": "Author 0",
"thumbnail": "http://example.com/authors/0"
}
}
]
}
}
These two examples clearly show how flexible it is to work with GraphQL. Every client-side application can update itself according to the requirement. Meanwhile, the server-side schema definition and implementation stay the same.
这两个例子清楚地表明了使用GraphQL的灵活性。每个客户端应用程序都可以根据需求进行自我更新。同时,服务器端的模式定义和实现保持不变。
4. Conclusion
4.总结
In this article, we’ve looked into two ways of exposing a graphQL field with a different name. We’ve introduced the concept of Aliases with examples and explained how it is the right approach.
在这篇文章中,我们已经研究了用不同的名字暴露一个graphQL字段的两种方法。我们用例子介绍了别名的概念,并解释了它是如何正确的方法。
As always, the example code for this article is available over on GitHub.
像往常一样,本文的示例代码可在GitHub上获得。