vscode构建rust_如何使用Rust构建功能强大的GraphQL服务器

论坛 期权论坛     
选择匿名的用户   2021-5-30 00:13   145   0
<div style="font-size: 16px;">
<p>vscode构建rust</p>
<p>Setting up a GraphQL server with Rust, Juniper, Diesel, and Actix; learning about Rust&#39;s web frameworks and powerful macros along the way.</p>
<p> 使用Rust,Juniper,Diesel和Actix设置GraphQL服务器; 一路了解Rust的网络框架和强大的宏。 </p>
<p>Source Code: <a href="https://github.com/iwilsonq/rust-graphql-example">github.com/iwilsonq/rust-graphql-example</a></p>
<p> 源代码: <a href="https://github.com/iwilsonq/rust-graphql-example">github.com/iwilsonq/rust-graphql-example</a> </p>
<p>Serving applications via GraphQL is quickly becoming the easiest and most effective way to deliver data to clients. Whether you&#39;re on a mobile device or a browser, it provides the data requested and nothing more.</p>
<p> 通过GraphQL为应用程序提供服务正Swift成为向客户端传递数据的最简单,最有效的方法。 无论您是使用移动设备还是浏览器,它都可以提供所请求的数据,仅此而已。 </p>
<p>Client applications no longer need to stitch together information from separate data sources. GraphQL servers are in charge of the integration, eliminating the need for excess data and round-trip requests for data.</p>
<p> 客户端应用程序不再需要将来自单独数据源的信息拼接在一起。 GraphQL服务器负责集成,从而消除了对多余数据和数据往返请求的需求。 </p>
<p>Of course, this implies that the server has to handle aggregating data from different sources, such as home-owned backend services, databases, or third party APIs. This may be resource intensive, how can we optimize for CPU time?</p>
<p> 当然,这意味着服务器必须处理来自不同来源(例如,家庭后端服务,数据库或第三方API)的聚合数据。 这可能会占用大量资源,我们如何优化CPU时间? </p>
<p>Rust is a marvel of a language, pairing the raw performance of a low level language like C with the expressiveness of modern languages. It emphasizes type and memory safety, especially when there are potentially data races in concurrent operations.</p>
<p> Rust是一种语言的奇妙之处,它将诸如C之类的底层语言的原始性能与现代语言的表现力相结合。 它强调类型和内存安全性,尤其是在并发操作中可能存在数据争用时。 </p>
<p>Let us see what goes into building a GraphQL server with Rust. We are going to learn about</p>
<p> 让我们看看使用Rust构建GraphQL服务器的过程。 我们将学习 </p>
<ul><li>Juniper GraphQL Server<p class="nodelete"></p> Juniper GraphQL服务器 </li><li>Actix web framework integrated with Juniper<p class="nodelete"></p> 与瞻博网络集成的Actix Web框架 </li><li>Diesel for quering a SQL database<p class="nodelete"></p> 用于查询SQL数据库的柴油 </li><li>Useful Rust macros and derived traits for working with these libraries<p class="nodelete"></p> 用于这些库的有用的Rust宏和派生特征 </li></ul>
<p>Note that I will not go into detail regarding installing Rust or Cargo. This article assumes some preliminary knowledge of the Rust toolchain.</p>
<p> 请注意,我不会详细介绍如何安装Rust或Cargo。 本文假设您对Rust工具链有一些初步的了解。 </p>
<h2 id="setting-up-an-http-server"> 设置HTTP服务器 <span style="font-weight: bold;">(</span>Setting up an HTTP Server<span style="font-weight: bold;">)</span></h2>
<p>To begin, we need to initialize our project  with <code>cargo</code> and then install dependencies.</p>
<p> 首先,我们需要使用<code>cargo</code>初始化我们的项目,然后安装依赖项。 </p>
<pre class="blockcode"><code class="language-sh">cargo new rust-graphql-example
    cd rust-graphql-example</code></pre>
<p>The initialization command bootstraps our Cargo.toml file which contains our projects dependencies as well as a <a href="http://main.rs">main.rs</a> file which has a simple &#34;Hello World&#34; example.</p>
<p> 初始化命令引导我们的Cargo.toml文件,其中包含我们的项目依赖项;以及<a href="http://main.rs">main.rs</a>文件,其中包含一个简单的“ Hello World”示例。 </p>
<pre class="blockcode"><code class="language-rust">// main.rs
   
    fn main() {
      println!(&#34;Hello, world!&#34;);
    }</code></pre>
<p>As a sanity check, feel free to run <code>cargo run</code> in order to execute the program.</p>
<p> 作为健全性检查,请随意运行<code>cargo run</code>以执行程序。 </p>
<p>Installing the necessary libraries in Rust means adding a line containing the library name and version number. Let&#39;s update the dependencies sections of Cargo.toml like so:</p>
<p> 在Rust中安装必要的库意味着添加包含库名称和版本号的行。 让我们像这样更新Cargo.toml的依赖项部分: </p>
<pre class="blockcode"><code class="language-rust"># Cargo.toml
   
    [dependencies]
    actix-web &#61; &#34;1.0.0&#34;
    diesel &#61; { version &#61; &#34;1.0.0&#34;, features &#61; [&#34;postgres&#34;] }
    dotenv &#61; &#34;0.9.0&#34;
    env_logger &#61; &#34;0.6&#34;
    futures &#61; &#34;0.1&#34;
    juniper &#61; &#34;0.13.1&#34;
    serde &#61; &#34;1.0&#34;
    serde_derive &#61; &#34;1.0&#
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP