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

GraphQL实战篇(一)

程序员文章站 2022-06-19 16:47:31
看过基础篇的都知道,GraphQL创建Schema有两种方式,Schema First和Graph Type,前者使用GraphQL Schema Language类似于EF的DB First;后者和EF 的Code First相似。使用GraphQL请求流程大致如下图所示,今天我就用Graph T ......

看过基础篇的都知道,graphql创建schema有两种方式,schema first和graph type,前者使用graphql schema language类似于ef的db first;后者和ef 的code first相似。使用graphql请求流程大致如下图所示,今天我就用graph type通过控制台程序简单的介绍下大致使用过程:

GraphQL实战篇(一)

1、安装graphql包

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

2、自定义数据模型 

 1     /// <summary>
 2     /// 自定义客户类
 3     /// </summary>
 4     [table("t_customer")]
 5     public partial class customerentity
 6     {
 7         public customerentity()
 8         {
 9 
10         } 
11         /// <summary>
12         /// 主键guid
13         /// </summary>
14         public string guid { get; set; }
15         /// <summary>
16         /// 中文性
17         /// </summary> 
18         [required, column("code_cn", typename = constants.nvarchar255)]
19         public string code_cn { get; set; }
20         /// <summary>
21         /// 中文名
22         /// </summary>
23         [required, column("name_cn", typename = constants.nvarchar255)]
24         public string name_cn { get; set; }
25         /// <summary>
26         /// 英文性
27         /// </summary>
28         [required, column("code_en", typename = constants.nvarchar255)]
29         public string code_en { get; set; }
30         /// <summary>
31         /// 英文名
32         /// </summary>
33         [required, column("name_en", typename = constants.nvarchar255)]
34         public string name_en { get; set; }
35         //中文姓,中文名,英文姓,英文名,性别,生日,默认出票方式,国籍,电子邮箱,工作单位,备注
36 
37         /// <summary>
38         /// 性别
39         /// </summary>
40         [required, column("sex", typename = constants.nvarchar255)]
41         public string sex { get; set; }
42         /// <summary>
43         /// 生日
44         /// </summary>
45         [required, column("birthday", typename = constants.nvarchar255)]
46         public string birthday { get; set; }
47         /// <summary>
48         /// 出票方式
49         /// </summary>
50         [required, column("drawerway", typename = constants.nvarchar255)]
51         public string drawerway { get; set; }
52         /// <summary>
53         /// 国籍
54         /// </summary>
55         [required, column("country", typename = constants.nvarchar255)]
56         public string country { get; set; }
57         /// <summary>
58         /// 电子邮箱
59         /// </summary>
60         [required, column("email", typename = constants.nvarchar255)]
61         public string email { get; set; }
62         /// <summary>
63         /// 工作单位
64         /// </summary>
65         [required, column("workunit", typename = constants.nvarchar255)]
66         public string workunit { get; set; }
67         /// <summary>
68         /// 备注
69         /// </summary>
70         [required, column("remark", typename = constants.text)]
71         public string remark { get; set; }
72     }

3、graphql的数据模型

定义graphql的数据模型该模型继承自objectgraphtype,对自定义数据模型customerentity的属性进行相应映射。

 1     /// <summary>
 2     /// graphql的数据模型:继承自objectgraphtype,并传递范型customerentity
 3     /// </summary>
 4     public class customerentitytype : objectgraphtype<customerentity>
 5     {
 6         //在构造函数中,对属性作影射
 7         public customerentitytype()
 8         {
 9             name = "customers";
10             field(x => x.guid);
11             field(x=>x.birthday);
12             field(x => x.code_cn);
13             field(x => x.code_en);
14             field(x => x.country);
15             field(x => x.drawerway);
16             field(x => x.email);
17             field(x => x.name_cn);
18             field(x => x.name_en);
19             field(x => x.remark);
20             field(x => x.sex);
21             field(x => x.workunit);
22             field<listgraphtype<customerentitytype>>("customers");
23         }
24 
25     }

4、定义操作模型

在这里我在啰嗦一遍graphql的操作包括 query(select), mutation (create,update,delete),subscription (订阅),在这里我把所有请求查询的字段映射到了customerrepository的调用上。

 1     public class customerrepository : icustomerrepository
 2     {
 3         //public list<customerentity> customerslist { get; set; }
 4         public ienumerable<customerentity> getall()
 5         {
 6             var jack = new customerentity
 7             {
 8                 guid = guid.newguid().tostring(),
 9                 code_cn = "张",
10                 name_cn = "三",
11                 code_en = "jack",
12                 name_en = "jack-jie",
13                 sex = "男"
14             };
15             var lx = new customerentity
16             {
17                 guid = guid.newguid().tostring(),
18                 code_cn = "李",
19                 name_cn = "五",
20                 code_en = "lx",
21                 name_en = "lx-jie",
22                 sex = "男"
23             };
24             var wz = new customerentity
25             {
26                 guid = guid.newguid().tostring(),
27                 code_cn = "王",
28                 name_cn = "三",
29                 code_en = "wz",
30                 name_en = "wz-jie",
31                 sex = "女"
32             };
33 
34             var query = new list<customerentity> { jack, lx, wz };
35             return query;
36         }
37         public customerentity getbyid(string guid)
38         {
39             return getall().firstordefault(x=>x.guid == guid);
40         }
41     }

 查询操作:

  1     /// <summary>
  2     /// graphql查询
  3     /// </summary>
  4     public class queryaction : objectgraphtype
  5     { 
  6         ienumerable<customerentity> customers = null;
  7         icustomerrepository repository = new customerrepository();
  8 
  9         public queryaction()
 10         {
 11             field<listgraphtype<customerentitytype>>(//在构造函数中定义查询操作
 12                 name: "customers", //注意这个名字,后边查询的时候需要对应
 13                 arguments: new queryarguments //定义查询参数
 14                 {
 15                      new queryargument<stringgraphtype>
 16                     {
 17                         name = "guid",
 18                         description = "the guid for the customerentity"
 19                     },
 20                     new queryargument<stringgraphtype>
 21                     {
 22                         name = "birthday",
 23                         description = "the birthday for the customerentity"
 24                     },
 25                     new queryargument<stringgraphtype>
 26                     {
 27                         name = "code_cn",
 28                         description = "the code_cn for the customerentity"
 29                     },
 30                     new queryargument<stringgraphtype>
 31                     {
 32                         name = "code_en",
 33                         description = "the code_en for customerentity"
 34                     },
 35                     new queryargument<stringgraphtype>
 36                     {
 37                         name = "country",
 38                         description = "the country for customerentity"
 39                     },
 40                     new queryargument<stringgraphtype>
 41                     {
 42                         name = "drawerway",
 43                         description = "the drawerway for customerentity"
 44                     },
 45                     new queryargument<stringgraphtype>
 46                     {
 47                         name = "email",
 48                         description = "the email for customerentity"
 49                     },
 50                     new queryargument<stringgraphtype>
 51                     {
 52                         name = "name_cn",
 53                         description = "the name_cn for customerentity"
 54                     },
 55                     new queryargument<stringgraphtype>
 56                     {
 57                         name = "name_en",
 58                         description = "the name_en for customerentity"
 59                     },
 60                     new queryargument<stringgraphtype>
 61                     {
 62                         name = "remark",
 63                         description = "the remark for customerentity"
 64                     },
 65                     new queryargument<stringgraphtype>
 66                     {
 67                         name = "sex",
 68                         description = "the sex for customerentity"
 69                     },
 70                     new queryargument<stringgraphtype>
 71                     {
 72                         name = "workunit",
 73                         description = "the workunit for customerentity"
 74                     }
 75                 },
 76                 resolve: context =>// 定义查询操作的执行
 77                 {
 78                     var customercontext = context.usercontext;// 获取上下文,可在此作用户验证操作
 79                     customers = repository.getall();
 80                     var guid = context.getargument<string>("guid");
 81                     customers = customers.where(u => guid == null || u.birthday == guid);
 82                     var birthday = context.getargument<string>("birthday");
 83                     customers = customers.where(u => birthday == null || u.birthday == birthday);
 84                     var code_cn = context.getargument<string>("code_cn");
 85                     customers = customers.where(u => code_cn == null || u.code_cn == code_cn);
 86                     var code_en = context.getargument<string>("code_en");
 87                     customers = customers.where(u => code_en == null || u.code_en == code_en);
 88                     var country = context.getargument<string>("country");
 89                     customers = customers.where(u => country == null || u.country == country);
 90                     var drawerway = context.getargument<string>("drawerway");
 91                     customers = customers.where(u => drawerway == null || u.drawerway == drawerway);
 92                     var email = context.getargument<string>("email");
 93                     customers = customers.where(u => email == null || u.email == email);
 94                     var name_cn = context.getargument<string>("name_cn");
 95                     customers = customers.where(u => name_cn == null || u.name_cn == name_cn);
 96                     var name_en = context.getargument<string>("name_en");
 97                     customers = customers.where(u => name_en == null || u.name_en == name_en);
 98                     var remark = context.getargument<string>("remark");
 99                     customers = customers.where(u => remark == null || u.remark == remark);
100                     var sex = context.getargument<string>("sex");
101                     customers = customers.where(u => sex == null || u.sex == sex);
102                     var workunit = context.getargument<string>("workunit");
103                     customers = customers.where(u => workunit == null || u.workunit == workunit);
104 
105                     return customers;
106                 });
107 
108             field<customerentitytype>(
109                name: "addcustomer", //注意这个名字,后边查询的时候需要对应
110                arguments: new queryarguments //定义查询参数
111                {
112                     new queryargument<stringgraphtype>
113                     {
114                         name = "guid",
115                         description = "the guid for the customerentity"
116                     },
117                     new queryargument<stringgraphtype>
118                     {
119                         name = "birthday",
120                         description = "the birthday for the customerentity"
121                     },
122                     new queryargument<stringgraphtype>
123                     {
124                         name = "code_cn",
125                         description = "the code_cn for the customerentity"
126                     },
127                     new queryargument<stringgraphtype>
128                     {
129                         name = "code_en",
130                         description = "the code_en for customerentity"
131                     },
132                     new queryargument<stringgraphtype>
133                     {
134                         name = "country",
135                         description = "the country for customerentity"
136                     },
137                     new queryargument<stringgraphtype>
138                     {
139                         name = "drawerway",
140                         description = "the drawerway for customerentity"
141                     },
142                     new queryargument<stringgraphtype>
143                     {
144                         name = "email",
145                         description = "the email for customerentity"
146                     },
147                     new queryargument<stringgraphtype>
148                     {
149                         name = "name_cn",
150                         description = "the name_cn for customerentity"
151                     },
152                     new queryargument<stringgraphtype>
153                     {
154                         name = "name_en",
155                         description = "the name_en for customerentity"
156                     },
157                     new queryargument<stringgraphtype>
158                     {
159                         name = "remark",
160                         description = "the remark for customerentity"
161                     },
162                     new queryargument<stringgraphtype>
163                     {
164                         name = "sex",
165                         description = "the sex for customerentity"
166                     },
167                     new queryargument<stringgraphtype>
168                     {
169                         name = "workunit",
170                         description = "the workunit for customerentity"
171                     }
172                },
173                resolve: context =>// 定义查询操作的执行
174                 { 
175                     string guid = context.getargument<string>("guid");
176                     return repository.getbyid(guid);
177                 });
178         }
179     }

 更新操作:

 1     /// <summary>
 2     /// graphql修改更新
 3     /// </summary>
 4     public class mutationaction : objectgraphtype
 5     {
 6         private icustomerrepository _repository = new customerrepository();
 7         ienumerable<customerentity> customers = null;
 8 
 9         public mutationaction()
10         {
11             field<stringgraphtype>(
12                "run", 
13                arguments: new queryarguments(new queryargument<customerentitytype> { name = "customer" }),
14                resolve: ctx => ctx.getargument<customerentity>("customer").guid);
15         }
16     }

5、定义graphschema模型

graphschema是graphql重中之重,它是所有操作的枢纽。

1   public class graphschema : schema
2     {
3         public graphschema()
4         {
5             query = new queryaction();
6             mutation = new mutationaction();
7         }
8     }

6、测试调用

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

1    public async void querycustomers(string code_cn, string code_en)
2         {
3             var querystr = @"{customers(code_cn:" + code_cn + ",code_en:" + "\"" + code_en + "\"" + "){code_cn code_en name_cn name_en}}";
4             var result = await execute.executeaction(new graphqlquery { query = querystr, customerentitycontext = "add customer" });
5             var data = result.data;
6             assert.isnull(result.errors?.count);
7         }

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

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

 1         public async void createcustomer(string code_cn, string code_en)
 2         {
 3             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 + @"}}}";
 4 
 5             var query = new graphqlquery
 6             {
 7                 query = "mutation ($customer: userinput!){addcustomer(customer:$customer){code_cn code_en name_cn name_en}}",
 8                 variables = jobject.parse("{customer:{\"code_cn\": \"" + code_cn + "\",\"code_en\":" + code_en + "}}")
 9             };
10             var result = await execute.executeaction(query);
11             assert.isnull(result.errors.count);
12         }

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