dinoQLGraphQL 风格的查询语言
dinoql 是一个可定制化的 GraphQL 风格的查询语言,用于实现与 JavaScript 对象进行交互。使用 dinoQL 可以像用 GraphQL 遍历 API 一样遍历 JavaScript 对象。
使用和 GraphQL 同样的做法的目的是因为:
- GraphQL syntax.
- Safe access (no runtime errors to keys that does not exist).
- Aliases support (You can rename your keys in the query).
- Many resolvers implemented by default.
- Build your own resolver.
- Fragments support(share piece of query logic).
- Parse your queries in build time. (Example)
- Filter values according to the value.
- Caching support
- Customizable.
使用示例
以下例子都使用如下数据:
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 }] } }
Getting only name from users
import dinoql from 'dinoql' const users = dinoql(data)` requests { users { name } } ` console.log(users) //{ users: [{ name: 'Victor Igor' }, { name: 'Kant Jonas' }] }
Get user by id
import dinoql from 'dinoql' const users = dinoql(data)` requests { users(id: "200") { name } } ` console.log(users) //{ users: [{ name: 'Kant Jonas' }] }
Aliases - Renaming keys
import dinoql from 'dinoql' const users = dinoql(data)` requests { changeUsers: users(id: "200") { name } } ` console.log(users) //{ changeUsers: [{ name: 'Kant Jonas' }] }
Resolvers
Resolvers provide the instructions for turning a dinoQL operation into data.
Order by
import dinoql from 'dinoql' const users = dinoql(data)` requests { users(orderBy: age) { name, age } } ` console.log(users) //{ users: [{ name: 'Kant Jonas', age: 35 }, { name: 'Victor Igor', age: 40 }] }
Default value
import dinoql from 'dinoql' const users = dinoql(data)` requests { notfound(defaultValue: "Hello") } ` console.log(users) // {notfound: "Hello"}
Parse to Number
import dinoql from 'dinoql' const users = dinoql(data)` requests { users { id(toNumber: 1) } } ` console.log(users) //{ users: [{ id: 100 }, { id: 200 }] }
First
import dinoql from 'dinoql' const users = dinoql(data)` requests { users(first: true) { name } } ` console.log(users) //{ users: { name: 'Victor Igor' } }
Last
import dinoql from 'dinoql' const users = dinoql(data)` requests { users(last: true) { name } } ` console.log(users) //{ users: { name: 'Kant Jonas' } }
Building your own resolver
You can create a function to change a value in query.
import dql, { addResolvers } from 'dinoql'; const incAge = (list, right) => { const valueToInc = Number(right); return list.map(item => ({ ...item, age: item.age + valueToInc })); }; addResolvers(({ incAge })); const value = dql(data)` requests { users(incAge: 2) { name, age } } `; // { users: [{ name: 'Victor Igor', age: 42 }, { name: 'Kant Jonas', age: 37 }] }
Custom options
Keep structure
import dinoql from 'dinoql' const users = dinoql(data, { keep: true })` requests { users(id: "200") { name } } ` console.log(users) /* { requests: { users: [{ name: 'Kant Jonas' }] } } */
Improve performance
You can improve performance parsing in build time your queries.
How ?
-
Create files
.graphql
or.gql
and add your queries. -
Import your queries from
.graphql|.gql
# your queries query MyQuery { requests { users } }
//your js import dinoql from 'dinoql' import { MyQuery } from './MyQueries'; const users = dinoql(data)(MyQuery)
- Setup your webpack - example
Fragments support
You can share piece of query logic.
fragment queryOne on Query { users { name } } fragment queryTwo on Query { products } query Form { requests { ...queryOne, ...queryTwo, friends } }
评论