看过基础篇的都知道,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. 【转载】 什么是P问题、NP问题和NPC问题

    原文地址: http://www.matrix67.com/blog/archives/105 转载地址: https://www.cnblogs.com/marsggbo/p/9360324.htm ...

  2. selenium元素input的value值设置【node.js版本】

    driver.executeScript(‘document.getElementById(“id”).value=“value”’); 这个操作就类似于//$("#id").va ...

  3. Git——起步(待续)

    原文链接:Getting Started with Git

  4. 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_01-用户认证-用户认证流程分析

    1 用户认证 1.1 用户认证流程分析 用户认证流程如下: 访问下面的资源需要携带身份令牌和jwt令牌,客户端可以通过身份认证的令牌从服务端拿到长令牌, 一会要实现认证服务请求用户中心从数据库内来查询 ...

  5. Day7作业:选课系统

    这周的作业有点糙,迁就看吧,给大家点思路: readme: 需要安装模块: prettytable 测试帐号: 1.后台管理:admin/admin 只设定了这个后台管理帐号,没有写到数据库中 2.学 ...

  6. PAT 甲级 1062 Talent and Virtue (25 分)(简单,结构体排序)

    1062 Talent and Virtue (25 分)   About 900 years ago, a Chinese philosopher Sima Guang wrote a histor ...

  7. mysql定时任务event——清理过期数据 (转)

    1.查询是否开启事件调度 SHOW VARIABLES LIKE 'event_scheduler'; 2.开启事件调度 临时: SET GLOBAL event_scheduler = 1; 永久: ...

  8. oracle数据库死锁原因及分析

    定义: 当两个用户希望持有对方的资源时就会发生死锁. 即两个用户互相等待对方释放资源时,oracle认定为产生了死锁,在这种情况下,将以牺牲一个用户作为代价,另一个用户继续执行,牺牲的用户的事务将回滚 ...

  9. SQL Server 2008 R2如何使用正则表达式搜索

    正则表达式是简明而灵活的表示法,用于查找和替换各种模式的文本.在 SQL Server Management Studio 的“查找和替换”对话框中的“查找内容”字段中,可以使用一组特定的正则表达式. ...

  10. centos7配置hadoop

    hadoop压缩包下载: 链接:https://pan.baidu.com/s/1dz0Hh75VNKEebcYcbN-4Hw 提取码:g2e3 java压缩包下载: 链接:https://pan.b ...