Contents

公开GraphQL字段

1. 概述

GraphQL 已被广泛用作 Web 服务中的一种通信模式。GraphQL 的基本前提是客户端应用程序使用灵活。

在本教程中,我们将研究灵活性的另一个方面。我们还将探讨如何使用不同的名称公开 GraphQL 字段。

2. GraphQL Schema

让我们举一个博客 的例子,其中包含不同AuthorPost。GraphQL 模式如下所示:

type Post {
    id: ID!
    title: String!
    text: String!
    category: String
    author: Author!
}
type Author {
    id: ID!
    name: String!
    thumbnail: String
    posts: [Post]!
}
query {
    recentPosts(count: 1,offset: 0){
        id
        title
        text
        category
        author{
            id
            name
            thumbnail
        }
    }
}

在这里我们可以获取最近的帖子。**每篇Post都会附有Author。**查询结果如下:

{
    "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. 使用不同名称公开 GraphQL 字段

客户端应用程序可能需要使用字段first_author。现在它正在使用author。为了满足这一要求,我们有两种解决方案:

  • 更改 GraphQL 服务器中的模式定义
  • 在 GraphQL中使用别名的概念

让我们一一看看。

3.1. 改变架构

让我们更新Post的架构定义:

type Post {
    id: ID!
    title: String!
    text: String!
    category: String
    first_author: Author!
}

作者不是一个微不足道的领域。这是一个复杂的。我们还必须更新相应的解析器以适应此更改。

PostResolver中的getAuthor (Post post) 方法将更新为getFirst_author(Post post)。**

这是查询:

query{
    recentPosts(count: 1,offset: 0){
        id
        title
        text
        category
        first_author{
            id
            name
            thumbnail
        }
    }
}

上述查询的结果如下:

{
    "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"
                }
            }
        ]
    }
}

这个解决方案有两个主要问题:

  • 它正在对架构和服务器端实现进行更改
  • 它迫使其他客户端应用程序遵循这个更新的模式定义

这些问题与 GraphQL 提供的灵活性功能相矛盾。

3.2. GraphQL 别名

**在 GraphQL 中,别名让我们可以在不更改架构定义的情况下将字段的结果重命名为我们想要的任何内容。**要在查询中引入别名,别名和冒号 (:) 必须位于 GraphQL 字段之前。

这是查询的演示:

query{
    recentPosts(count: 1,offset: 0){
        id
        title
        text
        category
        first_author:author{
            id
            name
            thumbnail
        }
    }
}

上述查询的结果如下:

{
    "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"
                }
            }
        ]
    }
}

让我们注意到查询本身正在请求第一个帖子。另一个客户端应用程序可能会请求使用first_post而不是*recentPosts。*再一次,别名将来救援。

query{
    first_post: recentPosts(count: 1,offset: 0){
        id
        title
        text
        category
        author{
            id
            name
            thumbnail
        }
    }
}

上述查询的结果如下:

{
    "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"
                }
            }
        ]
    }
}

**这两个示例清楚地展示了使用 GraphQL 的灵活性。**每个客户端应用程序都可以根据需要进行自我更新。同时,服务器端模式定义和实现保持不变。