ASP.NET Core中使用GraphQL - 第四章 GraphiQL

ASP.NET Core中使用GraphQL
- ASP.NET Core中使用GraphQL - 第一章 Hello World
- ASP.NET Core中使用GraphQL - 第二章 中间件
- ASP.NET Core中使用GraphQL - 第三章 依赖注入
GraphiQL是一款内置在浏览器中的GraphQL探索工具,这个是开发GraphQL的一个必备工具。它就相当于WebApi中的Swagger, 使用这个工具就可以看到你的GraphQL中所有配置的结构,并可以在浏览器中测试你的query, mutation
现在除了
GraphiQL,graphql-dotnet还提供了另外一个[GraphQL.Server](GraphQL for .NET - Subscription Transport WebSockets)的类库, 它也可以生成一个界面更优雅的探索工具,但是由于作者声明了还未在重型生产环境中测试过,所以这里先不做介绍,后续我会单独写一篇博文来介绍一下。
要想使用GraphiQL, 我们需要借助NodeJs中的npm和webpack.
首先我们在当前Hello World项目中创建一个package.json文件, 内容如下
{
"name": "GraphQLAPI",
"version": "1.0.0",
"main": "index.js",
"author": "Fiyaz Hasan",
"license": "MIT",
"dependencies": {
"graphiql": "^0.11.11",
"graphql": "^0.13.2",
"isomorphic-fetch": "^2.2.1",
"react": "^16.3.1",
"react-dom": "^16.2.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.11",
"extract-text-webpack-plugin": "^3.0.2",
"ignore-loader": "^0.1.2",
"style-loader": "^0.20.3",
"webpack": "^3.11.0"
}
}
然后可以使用一下命令,安装package.json中所有预定义的库
npm install
下一步,我们需要在当前项目目录中创建一个新的文件夹ClientApp, 并在其中添加2个文件app.js和app.css, 其文件内容如下。
app.js
import React from 'react';
import ReactDOM from 'react-dom';
import GraphiQL from 'graphiql';
import fetch from 'isomorphic-fetch';
import 'graphiql/graphiql.css';
import './app.css';
function graphQLFetcher(graphQLParams) {
return fetch(window.location.origin + '/api/graphql', {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(graphQLParams)
}).then(response => response.json());
}
ReactDOM.render(
<GraphiQL fetcher={graphQLFetcher} />,
document.getElementById('app')
);
app.css
html, body {
height: 100%;
margin: 0;
overflow: hidden;
width: 100%
}
#app {
height: 100vh
}
GraphiQL是一个客户端库,它提供了一个React组件<GraphiQL />, 这个组件用来呈现整个用户界面。这个组件有一个fetcher属性, 这个属性可以附加一个function。 附加的function返回了一个HTTP promise对象,它仅仅是模仿了我们在Postman中测试的POST请求。所以这些设置都写在app.js文件中。
下一步我们需要在wwwroot目录中添加一个index.html, 这里我们会将<GraphiQL />组件的内容呈现在id="app"的div中.
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>GraphiQL</title>
<link rel="stylesheet" href="/style.css" />
</head>
<body>
<div id="app"></div>
<script src="/bundle.js" type="text/javascript"></script>
</body>
</html>
在index.html文件中,我们引入了一个bundle.js和一个style.css文件。这2个文件是通过脚本编译出来的,所以这里我们需要添加一个webpack.config.js
webpack.config.js
const webpack = require('webpack');
var path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = [
{
entry: {
'bundle': './ClientApp/app.js',
},
output: {
path: path.resolve('./wwwroot'),
filename: '[name].js'
},
resolve: {
extensions: ['.js', '.json']
},
module: {
rules: [
{ test: /\.js/, use: [{
loader: 'babel-loader'
}], exclude: /node_modules/ },
{
test: /\.css$/, use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
},
{ test: /\.flow/, use: [{
loader: 'ignore-loader'
}] }
]
},
plugins: [
new ExtractTextPlugin('style.css', { allChunks: true })
]
}
];
最后我们还需要添加一个.babelrc文件,其内容如下
.babelrc
{
"presets": ["env", "react"]
}
以上文件添加完成之后,我们可以在在命令行使用webpack命令编译生成这2个文件。
C:\chapter4>webpack
Hash: e8082714ec56e818e1f4
Version: webpack 3.12.0
Child
Hash: e8082714ec56e818e1f4
Time: 6645ms
Asset Size Chunks Chunk Names
bundle.js 2.76 MB 0 [emitted] [big] bundle
style.css 39.7 kB 0 [emitted] bundle
[33] (webpack)/buildin/global.js 509 bytes {0} [built]
[128] ./node_modules/graphql-language-service-interface/dist ^.*$ 807 bytes {0} [built]
[137] ./ClientApp/app.js 996 bytes {0} [built]
[234] (webpack)/buildin/module.js 517 bytes {0} [built]
[292] ./ClientApp/app.css 41 bytes {0} [built]
[297] ./node_modules/css-loader!./ClientApp/app.css 301 bytes [built]
+ 292 hidden modules
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js!node_modules/graphiql/graphiql.css:
2 modules
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js!ClientApp/app.css:
[0] ./node_modules/css-loader!./ClientApp/app.css 301 bytes {0} [built]
+ 1 hidden module
C:\chapter4>
在服务器端,我们需要修改Startup.cs文件,在Configure方法中添加静态文件中间件和默认页中间件,修改后最终的Configure方法代码如下
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMiddleware<GraphQLMiddleware>();
}
现在我们启动项目,你将会看到如下图所示的用户界面。

在右侧的Documentation Explorer面板中,你可以看到定义的所有query, 并且可以了解到哪些字段是可用的,以及它们是干什么用的。
GraphiQL提供了许多很棒的功能
- 语法高亮
- 编写
GraphQL查询时,字段,参数,类型等的自动感知 - 实时错误高亮以及报告
- 自动补全查询
- 可以在浏览器中模拟请求, 运行检查查询结果
本文源代码:https://github.com/lamondlu/GraphQL_Blogs/tree/master/Part%20IV
ASP.NET Core中使用GraphQL - 第四章 GraphiQL的更多相关文章
- ASP.NET Core中使用GraphQL - 第五章 字段, 参数, 变量
ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...
- ASP.NET Core中使用GraphQL - 第六章 使用EF Core作为持久化仓储
ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...
- ASP.NET Core中使用GraphQL - 第七章 Mutation
ASP.NET Core中使用GraphQL - 目录 ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间 ...
- ASP.NET Core中使用GraphQL - 第三章 依赖注入
ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 SOL ...
- ASP.NET Core 中文文档 第四章 MVC(3.8)视图中的依赖注入
原文:Dependency injection into views 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:孟帅洋(书缘) ASP.NET Core 支持在视图中使用 依赖 ...
- ASP.NET Core 中文文档 第四章 MVC(4.2)控制器操作的路由
原文:Routing to Controller Actions 作者:Ryan Nowak.Rick Anderson 翻译:娄宇(Lyrics) 校对:何镇汐.姚阿勇(Dr.Yao) ASP.NE ...
- ASP.NET Core 中文文档 第四章 MVC(4.6)Areas(区域)
原文:Areas 作者:Dhananjay Kumar 和 Rick Anderson 翻译:耿晓亮(Blue) 校对:许登洋(Seay) Areas 是 ASP.NET MVC 用来将相关功能组织成 ...
- ASP.NET Core 中文文档 第四章 MVC(4.5)测试控制器逻辑
原文: Testing Controller Logic 作者: Steve Smith 翻译: 姚阿勇(Dr.Yao) 校对: 高嵩(Jack) ASP.NET MVC 应用程序的控制器应当小巧并专 ...
- ASP.NET Core 中文文档 第四章 MVC(4.4)依赖注入和控制器
原文: Dependency Injection and Controllers 作者: Steve Smith 翻译: 刘浩杨 校对: 孟帅洋(书缘) ASP.NET Core MVC 控制器应通过 ...
随机推荐
- guid.go
] = byte(g) hex.Encode(h[:], b[:]) return h }
- 在使用MFC对话框时遇到的用户交互
有时我们需要点击对话框上的一个按钮的时候,对话框隐藏起来,然后执行完某个操作后,对话框又弹出来, 这时我们可以使用BeginEditorCommand()和CompleteEditorCommand( ...
- 「关于一种处理关于$p$成多项式的数论函数筛法」
张博航原知乎网址 张博航原博客网址 引入: 给一个完全积性函数$f$,求其前缀和 $$S(n)=\sum_{i=1}^nf(i)$$ 初步思考: 考虑由于所求函数为完全积性函数,我们很容易用一个线性筛 ...
- 【gcd+stl】UVa1642 Magical GCD
Description 一个长度为n的数列,选一个连续子序列,使得子序列的公约数*长度最大,求这个最大值.n<=1e5. Solution 连续子序列一般都要用滑动窗口是吧(固定r,快速计算最优 ...
- POJ_2104_K-th Number_主席树
POJ_2104_K-th Number_主席树 题意:给定一个长度为n的序列,m次询问区间第k小 分析: 主席树模板 主席树可以理解成为n棵权值线段树的前缀和 但我们不能建n棵线段树,只需要对于每个 ...
- .Net Remoting 调用远程对象
根据需求,我们的系统必须以C/S方式构建,而且是三层架构,这样一来,就出现了服务器端和客户端通信的问题. 为了解决双方的通信问题,还要考虑效率.性能等方面,经过分析.试验,我们根据效率.移植.开发难易 ...
- linux yum命令 使用
yum -y install 包名(支持*) :自动选择y,全自动 yum install 包名(支持*) :手动选择y or n yum remove 包名(不支持*) rpm -ivh 包名(支持 ...
- 【毕业】-《伯恩茅斯大学毕业证书》BU一模一样原件
☞伯恩茅斯大学毕业证书[微/Q:2544033233◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归 ...
- 在Eclipse上Maven环境配置使用
1. 安装配置Maven: 1.1 从Apache网站 http://maven.apache.org/ 下载并且解压缩安装Apache Maven. Maven下载地址: http://maven. ...
- ASP.NET Core 项目实战(持续更新~~~)
一.前言 准备写这个系列文章的设想开始于今年9月,毫无意外,期间又又又又拖了很长时间,文章主要是为了记录自己学习使用 ASP.NET Core Web API 与 Vue 创建一个前后端分离的项目的整 ...