看过基础篇的都知道,GraphQL创建Schema有两种方式,Schema First和Graph Type,前者使用GraphQL Schema Language类似于EF的DB First;后者和EF 的Code First相似。使用GraphQL请求流程大致如下图所示,今天我就用Graph Type通过控制台程序简单的介绍下大致使用过程:

1、安装GraphQL包

GraphQL包提供了GraphQL.Types命名空间,GraphQL的使用离不开GraphQL.Types。

2、自定义数据模型

     /// <summary>
/// 自定义客户类
/// </summary>
[Table("t_customer")]
public partial class CustomerEntity
{
public CustomerEntity()
{ }
/// <summary>
/// 主键GuID
/// </summary>
public string Guid { get; set; }
/// <summary>
/// 中文性
/// </summary>
[Required, Column("code_cn", TypeName = Constants.NVarChar255)]
public string Code_CN { get; set; }
/// <summary>
/// 中文名
/// </summary>
[Required, Column("name_cn", TypeName = Constants.NVarChar255)]
public string Name_CN { get; set; }
/// <summary>
/// 英文性
/// </summary>
[Required, Column("code_en", TypeName = Constants.NVarChar255)]
public string Code_EN { get; set; }
/// <summary>
/// 英文名
/// </summary>
[Required, Column("name_en", TypeName = Constants.NVarChar255)]
public string Name_EN { get; set; }
//中文姓,中文名,英文姓,英文名,性别,生日,默认出票方式,国籍,电子邮箱,工作单位,备注 /// <summary>
/// 性别
/// </summary>
[Required, Column("sex", TypeName = Constants.NVarChar255)]
public string Sex { get; set; }
/// <summary>
/// 生日
/// </summary>
[Required, Column("birthday", TypeName = Constants.NVarChar255)]
public string BirthDay { get; set; }
/// <summary>
/// 出票方式
/// </summary>
[Required, Column("drawerway", TypeName = Constants.NVarChar255)]
public string DrawerWay { get; set; }
/// <summary>
/// 国籍
/// </summary>
[Required, Column("country", TypeName = Constants.NVarChar255)]
public string Country { get; set; }
/// <summary>
/// 电子邮箱
/// </summary>
[Required, Column("email", TypeName = Constants.NVarChar255)]
public string EMail { get; set; }
/// <summary>
/// 工作单位
/// </summary>
[Required, Column("workunit", TypeName = Constants.NVarChar255)]
public string WorkUnit { get; set; }
/// <summary>
/// 备注
/// </summary>
[Required, Column("remark", TypeName = Constants.Text)]
public string Remark { get; set; }
}

CustomerEntity

3、GraphQL的数据模型

定义GraphQL的数据模型该模型继承自ObjectGraphType,对自定义数据模型CustomerEntity的属性进行相应映射。

     /// <summary>
/// GraphQL的数据模型:继承自ObjectGraphType,并传递范型CustomerEntity
/// </summary>
public class CustomerEntityType : ObjectGraphType<CustomerEntity>
{
//在构造函数中,对属性作影射
public CustomerEntityType()
{
Name = "customers";
Field(x => x.Guid);
Field(x=>x.BirthDay);
Field(x => x.Code_CN);
Field(x => x.Code_EN);
Field(x => x.Country);
Field(x => x.DrawerWay);
Field(x => x.EMail);
Field(x => x.Name_CN);
Field(x => x.Name_EN);
Field(x => x.Remark);
Field(x => x.Sex);
Field(x => x.WorkUnit);
Field<ListGraphType<CustomerEntityType>>("Customers");
} }

CustomerEntityType

4、定义操作模型

在这里我在啰嗦一遍GraphQL的操作包括 Query(Select), Mutation (Create,Update,Delete),Subscription (订阅),在这里我把所有请求查询的字段映射到了CustomerRepository的调用上。

     public class CustomerRepository : ICustomerRepository
{
//public List<CustomerEntity> CustomersList { get; set; }
public IEnumerable<CustomerEntity> GetAll()
{
var jack = new CustomerEntity
{
Guid = Guid.NewGuid().ToString(),
Code_CN = "张",
Name_CN = "三",
Code_EN = "Jack",
Name_EN = "Jack-Jie",
Sex = "男"
};
var lx = new CustomerEntity
{
Guid = Guid.NewGuid().ToString(),
Code_CN = "李",
Name_CN = "五",
Code_EN = "lx",
Name_EN = "lx-Jie",
Sex = "男"
};
var wz = new CustomerEntity
{
Guid = Guid.NewGuid().ToString(),
Code_CN = "王",
Name_CN = "三",
Code_EN = "wz",
Name_EN = "Wz-Jie",
Sex = "女"
}; var query = new List<CustomerEntity> { jack, lx, wz };
return query;
}
public CustomerEntity GetByID(string guid)
{
return GetAll().FirstOrDefault(x=>x.Guid == guid);
}
}

CustomerRepository

查询操作:

     /// <summary>
/// GraphQL查询
/// </summary>
public class QueryAction : ObjectGraphType
{
IEnumerable<CustomerEntity> customers = null;
ICustomerRepository repository = new CustomerRepository(); public QueryAction()
{
Field<ListGraphType<CustomerEntityType>>(//在构造函数中定义查询操作
name: "customers", //注意这个名字,后边查询的时候需要对应
arguments: new QueryArguments //定义查询参数
{
new QueryArgument<StringGraphType>
{
Name = "Guid",
Description = "The Guid for the CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "BirthDay",
Description = "The BirthDay for the CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Code_CN",
Description = "The Code_CN for the CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Code_EN",
Description = "The Code_EN for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Country",
Description = "The Country for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "DrawerWay",
Description = "The DrawerWay for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "EMail",
Description = "The EMail for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Name_CN",
Description = "The Name_CN for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Name_EN",
Description = "The Name_EN for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Remark",
Description = "The Remark for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Sex",
Description = "The Sex for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "WorkUnit",
Description = "The WorkUnit for CustomerEntity"
}
},
resolve: context =>// 定义查询操作的执行
{
var customerContext = context.UserContext;// 获取上下文,可在此作用户验证操作
customers = repository.GetAll();
var Guid = context.GetArgument<string>("Guid");
customers = customers.Where(u => Guid == null || u.BirthDay == Guid);
var BirthDay = context.GetArgument<string>("BirthDay");
customers = customers.Where(u => BirthDay == null || u.BirthDay == BirthDay);
var Code_CN = context.GetArgument<string>("Code_CN");
customers = customers.Where(u => Code_CN == null || u.Code_CN == Code_CN);
var Code_EN = context.GetArgument<string>("Code_EN");
customers = customers.Where(u => Code_EN == null || u.Code_EN == Code_EN);
var Country = context.GetArgument<string>("Country");
customers = customers.Where(u => Country == null || u.Country == Country);
var DrawerWay = context.GetArgument<string>("DrawerWay");
customers = customers.Where(u => DrawerWay == null || u.DrawerWay == DrawerWay);
var EMail = context.GetArgument<string>("EMail");
customers = customers.Where(u => EMail == null || u.EMail == EMail);
var Name_CN = context.GetArgument<string>("Name_CN");
customers = customers.Where(u => Name_CN == null || u.Name_CN == Name_CN);
var Name_EN = context.GetArgument<string>("Name_EN");
customers = customers.Where(u => Name_EN == null || u.Name_EN == Name_EN);
var Remark = context.GetArgument<string>("Remark");
customers = customers.Where(u => Remark == null || u.Remark == Remark);
var Sex = context.GetArgument<string>("Sex");
customers = customers.Where(u => Sex == null || u.Sex == Sex);
var WorkUnit = context.GetArgument<string>("WorkUnit");
customers = customers.Where(u => WorkUnit == null || u.WorkUnit == WorkUnit); return customers;
}); Field<CustomerEntityType>(
name: "AddCustomer", //注意这个名字,后边查询的时候需要对应
arguments: new QueryArguments //定义查询参数
{
new QueryArgument<StringGraphType>
{
Name = "Guid",
Description = "The Guid for the CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "BirthDay",
Description = "The BirthDay for the CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Code_CN",
Description = "The Code_CN for the CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Code_EN",
Description = "The Code_EN for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Country",
Description = "The Country for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "DrawerWay",
Description = "The DrawerWay for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "EMail",
Description = "The EMail for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Name_CN",
Description = "The Name_CN for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Name_EN",
Description = "The Name_EN for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Remark",
Description = "The Remark for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "Sex",
Description = "The Sex for CustomerEntity"
},
new QueryArgument<StringGraphType>
{
Name = "WorkUnit",
Description = "The WorkUnit for CustomerEntity"
}
},
resolve: context =>// 定义查询操作的执行
{
string guid = context.GetArgument<string>("Guid");
return repository.GetByID(guid);
});
}
}

QueryAction

更新操作:

     /// <summary>
/// GraphQL修改更新
/// </summary>
public class MutationAction : ObjectGraphType
{
private ICustomerRepository _repository = new CustomerRepository();
IEnumerable<CustomerEntity> customers = null; public MutationAction()
{
Field<StringGraphType>(
"run",
arguments: new QueryArguments(new QueryArgument<CustomerEntityType> { Name = "customer" }),
resolve: ctx => ctx.GetArgument<CustomerEntity>("customer").Guid);
}
}

MutationAction

5、定义GraphSchema模型

GraphSchema是GraphQL重中之重,它是所有操作的枢纽。

   public class GraphSchema : Schema
{
public GraphSchema()
{
Query = new QueryAction();
Mutation = new MutationAction();
}
}

GraphSchema

6、测试调用

测试一下查询操作,关键代码如下:

    public async void QueryCustomers(string Code_CN, string Code_EN)
{
var queryStr = @"{customers(Code_CN:" + Code_CN + ",Code_EN:" + "\"" + Code_EN + "\"" + "){Code_CN Code_EN Name_CN Name_EN}}";
var result = await execute.ExecuteAction(new GraphQLQuery { Query = queryStr, CustomerEntityContext = "Add Customer" });
var data = result.Data;
Assert.IsNull(result.Errors?.Count);
}

QueryCustomersTest

这里你可以修改你的查询参数,无须修改API接口便可以达到目的。

修改更新接口的操作主要代码如下:

         public async void CreateCustomer(string Code_CN, string Code_EN)
{
var queryStr = @"{query: mutation ($customer: UserInput!){AddCustomer($customer:$customer){Code_CN Code_EN Name_CN Name_EN}},variables:{customer:{Code_CN: " + Code_CN + @",Code_EN:" + Code_EN + @"}}}"; var query = new GraphQLQuery
{
Query = "mutation ($customer: UserInput!){AddCustomer(customer:$customer){Code_CN Code_EN Name_CN Name_EN}}",
Variables = JObject.Parse("{customer:{\"Code_CN\": \"" + Code_CN + "\",\"Code_EN\":" + Code_EN + "}}")
};
var result = await execute.ExecuteAction(query);
Assert.IsNull(result.Errors.Count);
}

MutationActionTest

好了,以上就是我得初步总结和实践,后续会继续跟踪,欢迎纠错!!!

GraphQL实战篇(一)的更多相关文章

  1. 二、Redis基本操作——String(实战篇)

    小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...

  2. AngularJS in Action读书笔记6(实战篇)——bug hunting

    这一系列文章感觉写的不好,思维跨度很大,原本是由于与<Angularjs in action>有种相见恨晚而激发要写点读后感之类的文章,但是在翻译或是阐述的时候还是会心有余而力不足,零零总 ...

  3. ROS2.9.27架设网吧软路由实战篇之端口映射与回流

    转载:http://blog.csdn.net/zm2714/article/details/7924280 上一篇:ROS2.9.27架设网吧软路由实战篇之连通网络,主要讲述了网吧架设软路由ROS2 ...

  4. 2天驾驭DIV+CSS (实战篇)(转)

     这是去年看到的一片文章,感觉在我的学习中,有不少的影响.于是把它分享给想很快了解css的兄弟们.本文是实战篇. 基础篇[知识一] “DIV+CSS” 的叫法是不准确的[知识二] “DIV+CSS” ...

  5. javamail模拟邮箱功能发送电子邮件-基础实战篇(javamail API电子邮件实例)

    引言: JavaMail 是一种可选的.能用于读取.编写和发送电子消息的包 JavaMail jar包下载地址:http://java.sun.com/products/javamail/downlo ...

  6. javamail模拟邮箱功能发送电子邮件-中级实战篇【新增附件发送方法】(javamail API电子邮件实例)

    引言: JavaMail jar包下载地址:http://java.sun.com/products/javamail/downloads/index.html 此篇是紧随上篇文章而封装出来的,阅读本 ...

  7. javamail模拟邮箱功能--邮件删除-中级实战篇【邮件标记方法】(javamail API电子邮件实例)

    前言: JavaMail jar包下载地址:http://java.sun.com/products/javamail/downloads/index.html 本章可能是讲解javamail的最后一 ...

  8. Systemd 入门教程:实战篇

    Systemd 入门教程:实战篇 上一篇文章,介绍了 Systemd 的主要命令,这篇文章主要介绍如何使用 Systemd 来管理我们的服务,以及各项的含义: 一.开机启动 对于那些支持 System ...

  9. 工作经常使用的SQL整理,实战篇(二)

    原文:工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实 ...

随机推荐

  1. MindManager2018试用期过后 修改过期时间 破解使用

    MindManager2018试用期过后 修改过期时间 破解使用 2019年06月13日 15:58:11 一生中所爱 阅读数 1991更多 分类专栏: 工具软件使用   1.找到路径:C:\User ...

  2. ISO/IEC 9899:2011 条款6.2.8——对象的对齐

    6.2.8 对象的对齐 1.完整的对象类型具有对齐要求,对齐要求是对该类型对象可以在哪个地址进行分配的放置限制.一个对齐是一个实现定义的整数值,表示一个给定对象可以分配在相继两个地址之间跨多少字节的位 ...

  3. Qt编写自定义控件42-开关按钮

    一.前言 从2010年进入互联网+智能手机时代以来,各种各样的APP大行其道,手机上面的APP有很多流行的元素,开关按钮个人非常喜欢,手机QQ.360卫士.金山毒霸等,都有很多开关控制一些操作,在Qt ...

  4. 点击链接,取得href的值,但是不转向

    点击链接,取得href的值,但是不转向 $('.list a').click(function (e) {   e.preventDefault();//取消事件的默认动作.   $.ajax({   ...

  5. 《Fluid Engine Development》 学习笔记4-预测校正不可压缩SPH-PCISPH

    传统SPH方案的主要问题之一是时间步长限制.在原始的SPH中,我们首先从当前设置计算密度,使用EOS计算压强,应用压力梯度,然后运行时间积分.这个过程意味着只需要一定的压缩量就可以触发内核半径内的压力 ...

  6. MacOS这idea快捷键

    Control + Space 基本的代码补全(补全任何类.方法.变量) Control + Shift + Space 智能代码补全(过滤器方法列表和变量的预期类型) Command + Shift ...

  7. selenium + python 环境配置 (四)之启动Firefox

    火狐浏览器自身适配selenium   因此不需要再安装 直接代码启动: __author__ = 'admin' #作者 # -*- coding:utf-8 -*- # 建议所有都加编码 from ...

  8. K8S 从入门到放弃系列文章目录(Kubernetes 1.14)

    1)软件环境 软件 版本 系统 Centos7.5 Kubernetes 1.14.1 Docker 18.09 Calico 3.6 Etcd 3.3.12 2)部署过程简单概要 三台master节 ...

  9. LC 417. Linked List Cycle II

    题目描述 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

  10. 病毒 | wordpress网站内容被篡改、自动跳转、变全英文的解决办法

    去年10月开始,网站经常有文章被莫名其妙的篡改,而且后面还经常出现跳转到色情网站的问题,让人烦不胜烦,困扰了好几个月,最后终于解决了.这里特次记录和总结下此次恼人的事件. 时间:2018年10月 问题 ...