# gql-mongo

> gql-mongo is a simple converter which converts your `graphql` query to `mongodb` query

Latest version **1.1.4** (published 2018-04-28) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install gql-mongo
pnpm add gql-mongo
yarn add gql-mongo
bun add gql-mongo
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.4 |
| Published | 2018-04-28 |
| First published | 2018-04-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | tndgroup |

## Links

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

## Recent versions

- 1.1.4 (latest) — 2018-04-28
- 1.1.2 — 2018-04-28
- 1.1.0 — 2018-04-28
- 1.0.8 — 2018-04-28
- 1.0.6 — 2018-04-28
- 1.0.4 — 2018-04-28
- 1.0.2 — 2018-04-28
- 1.0.1 — 2018-04-28
- 1.0.0 — 2018-04-28

## README

# What is gql-mongo 

gql-mongo is a simple converter which converts your `graphql` query to `mongodb` query

## Get Started 

let define your simple query: 
```graphql
  input UserFilter {
    id: ID
    username: String
    age: Integer
    age_gt: Integer
    age_lt: Integer
  }
  type Query {
    // search for users in your mongodb database
    users: (filter: UserFilter!, sortBy: UserSortBy!, limit: Integer!, skip: Integer!): UserConnection!
  }
```
GqlMongo converts your `filter` query to 
```javascript
  {
    $and: [
      { id: { $eq: <value> } },
      { username: { $eq: <value> }},
      { age: {$eq: <value>} },
      { age: {$gt: <value>} },
      { age: {$lt: <value>} }
    ]
  }
```
check `MetaFilter` of https://www.graph.cool/docs/reference/graphql-api/query-api-nia9nushae for standard filter.

## Support operators

`$eq`, `$gt`, `$gte`, `$in`, `$lt`, `$lte`, `$ne`, `$nin`, `$regex`,

## Usage 

Use in your resolver `users.js`
```javascript
import { GqlMongo } from 'qgl-mongo'
const gqlMongo = new GqlMongo()

export default ({_, { filter = {}, orderBy, skip, limit }, { Users }}) => {
  const query = gqlMongo.parseQuery(filter);
  const users = Users.find(query);
  // handle skip limit
}
```

## Custom query
for exmaple, you want to search username which starts with `taind`. Firstly, adding new field to query
```graphql
  input UserFilter {
    id: ID
    username: String
    username_starts_with: String
    age: Integer
    age_gt: Integer
    age_lt: Integer
  }
```
then in resolver `users.js`
```javascript
import { GqlMongo } from 'qgl-mongo'
const gqlMongo = new GqlMongo({
  $starts_with: val => ({$regex: new RegExp(`^${val}`, 'gi')})
})
```
## Additional Features
GqlMongo generate query fields:

```gql
import { GqlField } from 'gql-mongo'
const gqlField = new GqlField()
const r = gqlField.parse({ age: 'Int'}).toFilterString()
// { age: 'Int', age_ne: 'Int', age_in: '[Int!]', age_nin: '[Int!]', age_lt: 'Int', age_lte: 'Int', age_gt: 'Int', age_gte: 'Int' }

```

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