@adamvr/joiql 中文文档教程
joiql
使用 JoiGraphQL 模式创建和数据验证变得容易>。
Example
使用 node example
运行它……
const joiql = require('joiql')
const { object, string, number, array, date } = require('joi')
const app = require('express')()
const graphqlHTTP = require('express-graphql')
// Joi Schemas
const Film = object({
title: string(),
producers: array().items(string()),
release_date: date()
})
const Person = object({
name: string(),
films: array().items(Film)
}).meta({
args: { id: number().required() },
resolve: (root, args, req, ast) => ({ name: 'Spike Jonze' })
})
// Convert Joi schemas to GraphQL
const schema = joiql({
query: {
person: Person,
film: Film
}
})
// Mount schema to express
app.use('/', graphqlHTTP({ schema: schema, graphiql: true }))
app.listen(3000, () => console.log('listening on 3000'))
Breaking it down
首先,使用 Joi 定义一些模式。
const { object, string, number, array, date } = require('joi')
const Film = object({
title: string(),
producers: array().items(string()),
releaseDate: date()
})
const Person = object({
name: string(),
films: array().items(Film)
})
JoiQL 使用 meta
属性 来扩展 GraphQL 字段. 使用 meta.args
定义 GraphQL 参数(添加自动 输入验证)和 meta.name
来声明 GraphQLObjectType
类型名称(没有它 JoiQL 会自动 添加“Anon
Person.meta({
name: 'Person',
args: { id: number().required() }
})
然后从 Joi 模式创建一个 JoiQL api
对象,并公开一个 GraphQL.js 模式对象以安装到像 Express 这样的服务器中。
const { graphql } = require('graphql')
const joiql = require('../')
const api = joiql({
query: {
person: Person,
film: Film
}
})
graphql(api.schema, ...)
TODO
- Figure out how to do circular dependencies (ideally with Joi
lazy
)
Contributing
请分叉该项目并提交带有测试的拉取请求。 安装节点模块 npm install
并使用 npm test
运行测试。
License
麻省理工学院
joiql
Make GraphQL schema creation and data validation easy with Joi.
Example
Run this using node example
…
const joiql = require('joiql')
const { object, string, number, array, date } = require('joi')
const app = require('express')()
const graphqlHTTP = require('express-graphql')
// Joi Schemas
const Film = object({
title: string(),
producers: array().items(string()),
release_date: date()
})
const Person = object({
name: string(),
films: array().items(Film)
}).meta({
args: { id: number().required() },
resolve: (root, args, req, ast) => ({ name: 'Spike Jonze' })
})
// Convert Joi schemas to GraphQL
const schema = joiql({
query: {
person: Person,
film: Film
}
})
// Mount schema to express
app.use('/', graphqlHTTP({ schema: schema, graphiql: true }))
app.listen(3000, () => console.log('listening on 3000'))
Breaking it down
First, define some schemas using Joi.
const { object, string, number, array, date } = require('joi')
const Film = object({
title: string(),
producers: array().items(string()),
releaseDate: date()
})
const Person = object({
name: string(),
films: array().items(Film)
})
JoiQL uses the meta
property to extend GraphQL fields. Use meta.args
to define GraphQL arguments (adding automatic input validation), and meta.name
to declare the GraphQLObjectType
type name (without it JoiQL will automatically add a "Anon
Person.meta({
name: 'Person',
args: { id: number().required() }
})
Then create a JoiQL api
object from the Joi schemas and expose a GraphQL.js schema object for mounting into a server like Express.
const { graphql } = require('graphql')
const joiql = require('../')
const api = joiql({
query: {
person: Person,
film: Film
}
})
graphql(api.schema, ...)
TODO
- Figure out how to do circular dependencies (ideally with Joi
lazy
)
Contributing
Please fork the project and submit a pull request with tests. Install node modules npm install
and run tests with npm test
.
License
MIT