graphql 入门_GraphQL入门

graphql 入门

GraphQL was developed by Facebook in 2012 to power up its mobile apps. Since open-sourcing GraphQL specification in 2015, it gained a lot of popularity and is now used by many development teams, including giants like GitHub, Twitter or Airbnb. Why so? And what exactly is a GraphQL? Let's take a look.

GraphQL由Facebook在2012年开发,旨在增强其移动应用程序的功能。 自2015年开源GraphQL规范以来,它就获得了广泛的欢迎,现已被许多开发团队使用,包括GitHub,Twitter或Airbnb等巨头。 为什么这样? GraphQL到底是什么? 让我们来看看。

GraphQL Logo

什么是GraphQL (What's GraphQL)

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask the server for what is exactly needed, which will result in the response containing only the requested data, nothing more.

GraphQL是API的查询语言,是用于使用现有数据来完成这些查询的运行时。 GraphQL在您的API中提供了对数据的完整且可理解的描述,使客户端可以向服务器询问确切需要什么,这将导致响应仅包含所请求的数据,仅此而已。

The GraphQL idea arose in the time of the mobile boom and its main goal for GraphQL was to solve many of the shortcomings and inefficiencies that Facebook mobile app developers experienced while working with REST API.

GraphQL想法是在移动技术繁荣时期兴起的,其GraphQL的主要目标是解决Facebook移动应用程序开发人员在使用REST API时遇到的许多缺点和效率低下的问题。

GraphQL Team at Facebook

资料来源: GraphQL:纪录片 (Source: GraphQL: The Documentary)

GraphQL与REST中的数据获取 (Data-fetching in GraphQL vs REST)

In typical REST implementation, the client gathers data by accessing multiple endpoints i.e. you would first call endpoint to fetch the initial user data, then make a sperate call to fetch its all properties. GraphQL handles it differently. The specification of what can be queried lays on the client-side and while querying GraphQL server for specific data it will respond with exact data that was requested which means fewer bits transferred over the wire.

在典型的REST实现中,客户端通过访问多个端点来收集数据,即,您将首先调用端点以获取初始用户数据,然后进行一次明智的调用以获取其所有属性。 GraphQL以不同的方式处理它。 可以查询的内容位于客户端,在查询GraphQL服务器以获取特定数据时,它将以请求的确切数据进行响应,这意味着通过网络传输的位数更少。

GraphQL vs REST

架构定义语言(SDL) (Schema Definition Language (SDL))

The GraphQL schema is the core of any GraphQL project. Nested in a GraphQL server, it defines every functionality available for the GraphQL client. The most basic element of each schema is a type which allows establishing relations between different schema elements & define allowed GraphQL operations to be performed on the server and more. To make it easier to understand the operation that a server can perform GraphQL defines a universal schema syntax know as Schema Definition Language (SDL). The most basic elements of a GraphQL schema are object types. They represent an object you can fetch from your GraphQL server with available fields i.e.:

GraphQL 模式是任何GraphQL项目的核心 。 嵌套在GraphQL服务器中,它定义了GraphQL客户端可用的所有功能。 每个架构的最基本元素是一种type ,它允许在不同架构元素之间建立关系并定义允许在服务器上执行的GraphQL操作等。 为了更容易理解服务器可执行GraphQL的操作,定义了一种通用架构语法,称为Schema Definition Language(SDL) 。 GraphQL模式的最基本元素是对象类型 。 它们表示您可以使用可用字段从GraphQL服务器获取的对象,即:

type Movie {
   title: String
   Director: Director
}

type Director {
   name: String
   movies: [Movie]
}

Query is a basic fetch operation in GraphQL to request data from the GraphQL server

查询是GraphQL中的基本提取操作,用于从GraphQL服务器请求数据

type Query{
    getMovies: [Movie]
    getDirectors: [Director]
}

Mutation is one of the basic GraphQL operations allowing you to manipulate the data (create, modify or delete):

突变是GraphQL的基本操作之一,可让您操纵数据(创建,修改或删除):

type Mutation {
   addMovie(title: String, director: String) : Movie
}

If you want to read more about the SDL the official GraphQL webiste is a way to go.

如果您想了解有关SDL的更多信息,可以使用官方的GraphQL网站

GraphQL的第一步 (First steps with GraphQL)

If you want to discover more about GraphQL the best way is to try it yourself. I believe that GraphQL Editor is an option to consider. It's a tool that comes in handy when you make your first steps with GraphQL as you can work with your GraphQL schema on a visual interface (of course you can write code too). GraphQL Editor provides many useful features that will help you make sure that your GraphQL API is well-developed.

如果您想了解有关GraphQL的更多信息,最好的方法是亲自尝试。 我相信可以选择GraphQL Editor 。 当您使用GraphQL迈出第一步时,它是一个方便的工具,因为您可以在可视界面上使用GraphQL模式(当然您也可以编写代码)。 GraphQL编辑器提供了许多有用的功能,这些功能将帮助您确保GraphQL API开发良好。

Users new to GraphQL will find a built-in interactive tutorial very handy. Six short tasks you will brief you on the basis of SDL syntax like:

GraphQL的新用户会发现内置的交互式教程非常方便。 您将根据SDL语法向您介绍六个简短的任务,例如:

  • Types

    种类
  • Queries

    查询
  • Mutations

    变异

allowing you to start working on your first GraphQL schema. Click this link if you want to and you will be redirected to the GraphQL Interactive tutorial. GraphQL Editor provides easier:

使您可以开始处理第一个GraphQL模式。 如果需要,请单击此链接 ,您将被重定向到GraphQL Interactive教程。 GraphQL编辑器提供了更简单的方法:

1. Schema Creation - GraphQL Editor is an environment for GraphQL development, where you can prototype your schema with visual nodes, validates and validates it. Parallelly, the traditional code is created so you can do it both ways.

1.模式创建 -GraphQL编辑器是GraphQL开发的环境,您可以在其中使用可视节点对模式进行原型设计,验证和验证。 同时创建传统代码,因此您可以同时使用两种方法。

2. Management - The editor provides automation of your GraphQL project as you can import an already existing production schema, edit it and generate out a fake backend. This way you can test new features even without a fully operational production backend!

2.管理 -编辑器提供了GraphQL项目的自动化,因为您可以导入已经存在的生产模式,对其进行编辑并生成伪造的后端。 这样,即使没有完全可操作的生产后端,您也可以测试新功能!

3. Auto-complete libraries for frontend - Additionally if JavaScript/TypeScript is among your preferred languages you can export auto-complete libraries for your project which are powered by graphql-zeus.

3.用于前端的自动完成库 -另外,如果JavaScript / TypeScript在您的首选语言中,则可以导出由graphql-zeus支持的项目的自动完成库。

4. Collaboration - Visual representation of a schema improves communication between developers & people with business roles in the team.

4.协作 -模式的可视化表示可改善开发人员与团队中具有业务角色的人员之间的沟通。

结论 (Conclusion)

GraphQL, tracked by its most popular client library Apollo, continues to explode in popularity. GraphQL it’s going to be a technical force to reckon within 2019. Many people from the industry think that 2019/2020 will be the time of massive GraphQL adoption. Although REST is still more mature & popular technology, by not giving GraphQL a try you are making a mistake. I believe its a very interesting and promising approach to client-server communication.

由最受欢迎的客户端库Apollo跟踪的GraphQL继续呈爆炸式增长。 GraphQL将在2019年成为一支技术力量。许多业内人士认为,2019/2020年将是GraphQL大规模采用的时期。 尽管REST仍然是更为成熟和流行的技术,但是如果不尝试GraphQL,您会犯错。 我相信这是一种非常有趣且有希望的客户端与服务器通信方法。

翻译自: https://davidwalsh.name/getting-started-with-graphql

graphql 入门

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值