欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET Core使用GraphQL第一章之Hello World

程序员文章站 2023-11-09 09:09:58
前言 你是否已经厌倦了rest风格的api? 让我们来聊一下graphql。  下面是graphql的定义: graphql 既是一种用于...

前言

你是否已经厌倦了rest风格的api? 让我们来聊一下graphql。

 下面是graphql的定义:

graphql 既是一种用于 api 的查询语言也是一个满足你数据查询的运行时。 graphql 对你的 api 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 api 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。

graphql由facebook开发,始于2012年,2015年公开。

graphql牛逼之处是它可以让客户端精确的查询它们想要的,不附加额外的东西,这样的话就很容易让客户端随着api的演进去使用。

graphql提供了一种声明式的方式从服务器拉取数据。你可以从graphql官网中了解到graphql的所有优点。在这一系列博客中,我将展示如何在asp.net core中集成graphql, 并使用graphql作为你的api查询语言。

使用graphql的声明式查询,你可以自定义api返回的属性列表。这与rest api中每个api只返回固定字段不同。

安装graphql

为了在c#中使用graphql, graphql社区中提供了一个开源组件 graphql-dotnet 。本系列博客中我们都将使用这个组件。

首先我们创建一个空的asp.net core app

dotnet new web --name chatper1

然后我们添加对 graphql-dotnet 库的引用

dotnet add package graphql

创建第一个query

下面我们来创建一个 query 类, 我们将它命名为 helloworldquery 。 graphql-dotnet 中,查询类都需要继承 objectgraphtype 类,所以 helloworldquery 的代码如下

using graphql.types;
public class helloworldquery : objectgraphtype
{
  public helloworldquery()
  {
    field<stringgraphtype>(
      name: "hello",
      resolve: context => "world"
    );
  }
}

这里你可能注意到我们使用了一个泛型方法 field ,并传递了一个graphql的字符串类型 stringgraphtype 来定义了一个 hello 字段, resolve 参数是一个func委托,在其中定义了如何返回当前字段的值,这里我们是直接返回了一个字符串hello。

查询类中的返回字段都是定义在查询类的构造函数中的

现在我们一个有了一个查询类,下一步我们需要使用这个查询类构建一个结构(schema)。

在 startup.cs 文件的 configure 方法中,使用以下代码替换原有代码

var schema = new schema {
  query = new helloworldquery() 
};

app.run(async (context) =>
{
  var result = await new documentexecuter()
    .executeasync(doc =>
    {
      doc.schema = schema;
      doc.query = @"
        query {
          hello
        }
      ";
    }).configureawait(false);

  var json = new documentwriter(indent: true)
    .write(result)
  await context.response.writeasync(json);
});
  • documentexecuter 类的 executeasync 方法中我们定义action委托,并通过这个委托设置了一个 executionoptions 对象。这个对象初始化了我们定义的结构(schema), 并执行了我们定义的查询字符串。
  • doc.query 定义了一个查询字符串
  • 最终查询执行的结果会通过 documentwriter 类实例的 write 被转换成一个json字符串

下面我们来运行一下这个程序

dotnet run

你将在浏览器中看到以下结果

{
  "data": {
    "hello": "world"
  }
}

从以上的例子中,你会发现使用graphql并不像想象中那么难。下面我们可以在 helloworldquery 类的构造函数中再添加一个字段 howdy , 并指定这个字段会返回一个字符串 universe 。

field<stringgraphtype>(
  name: "howdy",
  resolve: context => "universe"
);

然后我们继续修改 startup 类中的 configure 方法, 修改我们之前定义的query

var schema = new schema { 
  query = new helloworldquery()
};

app.run(async (context) =>
{
  var result = await new documentexecuter()
    .executeasync(doc =>
    {
      doc.schema = schema;
      doc.query = @"
        query {
          hello
          howdy
        }
      ";
    }).configureawait(false);

  var json = new documentwriter(indent: true)
    .write(result)
  await context.response.writeasync(json);
});

重新启动项目后,结果如下

{
  "data": {
    "hello": "world",
    "howdy": "universe"
  }
}

总结

本篇我们只是接触了graphql的一些皮毛,你可能会对graphql声明式行为有很多问题,没有关系,后续博客中,我们慢慢解开graphql的面纱。下一篇我们将介绍如何创建一个中间件(middleware)

本篇源代码:https://github.com/lamondlu/graphql_blogs (本地下载)

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。