apollo server_在Express Server上使用Apollo实施GraphQL

apollo server

介绍 ( Introduction )

GraphQL is a data query language for APIs and runtime, a specification that defines a communication protocol between a client and server. At its core, GraphQL enables flexible, client-driven application development in a strongly typed environment. It provides a complete and understandable description of the data in your API, gives the clients the power to ask for exactly what they need.

GraphQL是一种用于API和运行时的数据查询语言,该规范定义了客户端和服务器之间的通信协议。 GraphQL的核心是在强类型环境中进行灵活的,客户端驱动的应用程序开发。 它提供了API中数据的完整且易于理解的描述,使客户可以准确地询问他们的需求。

GraphQL Home Page

GraphQL enables declarative data fetching where a client can specify exactly what data it needs from an API. Instead of multiple endpoints that return fixed data structures, a GraphQL server only exposes a single endpoint and responds with precisely the data a client asked for.

GraphQL启用声明式数据获取,客户端可以在其中确切指定需要从API中获取哪些数据。 GraphQL服务器不是公开返回固定数据结构的多个终结点,而是仅公开一个终结点并精确响应客户端请求的数据。

It is a new API standard that provides a more efficient, powerful and flexible alternative to REST and is not tied to any backend or database and is instead backed by your existing code and data.

它是一个新的API标准,它提供了REST的更有效,更强大和更灵活的选择,并且不与任何后端或数据库绑定,而是由您现有的代码和数据支持。

Client appllications using GraphQL are fast and stable because they control the data they get, not the server, providing predictable results.

使用GraphQL的客户端应用程序快速且稳定,因为它们控制获取的数据(而不是服务器)提供可预测的结果。

To successfully complete this tutorial, we will require Node JS to be setup on our computers.

为了成功完成本教程,我们将需要在计算机上设置Node JS。

概念 ( Concepts )

Schema

架构图

The server defines the schema which defines the objects that can be queried, along with corresponding data types. A sample simple project schema may be defined as follows:

服务器定义了schema ,该schema定义了可以查询的对象以及相应的数据类型。 一个简单的项目模式示例可以定义如下:

type Project{
    
  id: ID!                                            //"!" denotes a required field
  name: String
  tagline: String
  contributors: [User]
}

type User {
    
 id: ID!
 name: String
 age: Int
}

The schema above defines the shape of a project with a required id, name, tagline an contributors which is an array of type User.

上面的模式定义了项目的形状,并带有必需的idnametaglinecontributors ,该contributorsUser类型的数组。

Queries and Mutations

查询和变异

GraphQL clients communicate with GraphQL servers via queries and mutations. A query also defines the shape of the resulting data, allowing the client to explicitly control the shape of data being consumed. Queries correspond to GET requests in normal REST applications while mutations correspond to POST, PUT and other http verbs used to make chnages to data stored on the server. For instance, to retrieve a list of projects, including name and tagline only, the following query may be used.

GraphQL客户端通过查询和变异与GraphQL服务器通信。 查询还定义了结果数据的形状,从而允许客户端显式控制正在使用的数据的形状。 查询对应于普通REST应用程序中的GET请求,而变异对应于POSTPUT和用于对服务器上存储的数据进行更改的其他http动词。 例如,要检索项目列表,仅包括nametagline ,可以使用以下查询。

query {
    
    projects {
    
        name
        tagline
    }
}

An equivalent of querystrings in REST may be used as follows:

REST中的等效查询字符串可以按以下方式使用:

query {
    
  project(id
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值