dinoql 试用
dinoql 前面有过介绍,详细的参考文档即可,这篇主要是简单使用
注意目前dinoql 直接通过node 运行会有window 的问题,有好几种解决方法,后边会说明
环境准备
- 项目初始化
yarn init -y
- 添加dinoql依赖
yarn add dinoql
- 添加工具依赖
主要是解决web端运行的,实际上方法很多,我为了方便使用browserify 构建包,使用live-server 作为静态web 服务器,使用nodemon
做代码变动重新构建
yarn add live-server nodemon browserify --dev
- package.json npm script 编写
{
"name": "dinoql-demo",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"dinoql": "^0.3.4"
},
"scripts": {
"build:js": "npx browserify app.js > index.js && live-server --no-browser",
"build:watch": "nodemon --exec yarn build:js"
},
"nodemonConfig": {
"ignore": [
"node_modules/*",
"index.js"
]
},
"devDependencies": {
"browserify": "^16.2.3",
"live-server": "^1.2.1",
"nodemon": "^1.19.0"
}
}
使用&&测试
- 编写简单查询代码
代码和很简单,来自官方
const dinoql = require("dinoql")
const data = {
requests: {
products: [],
users: [{
name: 'Victor Igor',
id: "100",
age: 40
}, {
name: 'Kant Jonas',
id: "200",
age: 35
}],
friends: [{
name: 'Kátia',
id: "300",
age: 10
}]
}
}
const users = dinoql(data)`
requests {
users {
name
id
}
friends {
name
}
}
`
console.log(users)
- 运行(浏览器端)
yarn build:watch
- node 环境
node app
如果直接运行会有问题,提示信息入如下
(function (exports, require, module, __filename, __dirname) { !function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n
():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.dinoql=n():e.dinoql=n()}(window,function(){return functi
on(e){var n={};function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.ex
ports}return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:r})},t.r=function(e){"undefined"!=typeof Sy
mbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=fu
nction(e,n){if(1&n&&(e=t(e)),8&n)return e;if(4&n&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(t.r(r),Object.defin
eProperty(r,"default",{enumerable:!0,value:e}),2&n&&"string"!=typeof e)for(var o in e)
ReferenceError: window is not defined
at Object.<anonymous> (/Users/dalong/mylearning/dinoql-demo/node_modules/dinoql/dist/dinoql.min.js:1:256)
at Module._compile (internal/modules/cjs/loader.js:678:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
at Module.load (internal/modules/cjs/loader.js:589:32)
at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
at Function.Module._load (internal/modules/cjs/loader.js:520:3)
at Module.require (internal/modules/cjs/loader.js:626:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/Users/dalong/mylearning/dinoql-demo/app.js:3:17)
at Module._compile (internal/modules/cjs/loader.js:678:30)
解决方法:
在代码的首行(建议),添加以下代码
window=global||window
运行效果
{ users:
[ { name: 'Victor Igor', id: '100' },
{ name: 'Kant Jonas', id: '200' } ],
friends: [ { name: 'Kátia' } ] }
说明
以上只是简单的使用,实际上dinoql 对于graphql 语法的集成还是比较全的,想法还是很不错的
参考资料
https://github.com/rongfengliang/dinoql-basic-demo
https://github.com/victorvoid/dinoql