# joiql

> Make GraphQL schema creation and data validation easy with Joi.

Latest version **0.1.5** (published 2017-07-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install joiql
pnpm add joiql
yarn add joiql
bun add joiql
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2017-07-20 |
| First published | 2016-07-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | craigspaeth |

## Links

- npm: https://www.npmjs.com/package/joiql
- npm.io page: https://npm.io/package/joiql

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.13.1
- [graphql](https://npm.io/package/graphql.md) ^0.7.2

## Recent versions

- 0.1.5 (latest) — 2017-07-20
- 0.1.4 — 2017-07-07
- 0.1.3 — 2016-12-29
- 0.1.2 — 2016-12-26
- 0.1.1 — 2016-12-26
- 0.1.0 — 2016-12-20
- 0.0.12 — 2016-11-30
- 0.0.10 — 2016-10-27
- 0.0.9 — 2016-10-26
- 0.0.8 — 2016-10-19
- 0.0.7 — 2016-10-17
- 0.0.6 — 2016-08-27
- 0.0.5 — 2016-08-22
- 0.0.4 — 2016-08-20
- 0.0.3 — 2016-07-31
- … 2 more at https://npm.io/package/joiql/versions

## README

# joiql

Make [GraphQL](http://graphql.org/) schema creation and data validation easy with [Joi](https://github.com/hapijs/joi).

## Example

Run this using `node example`...

````javascript
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](https://github.com/hapijs/joi).

````javascript
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](https://github.com/hapijs/joi/blob/v9.0.4/API.md#anymetameta) 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<UID>" type name).

````javascript
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.

````javascript
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

---
_Source: https://npm.io/package/joiql · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
