# graphql-projection

> Create a MongoDB projection from a GraphQL query

Latest version **0.1.2** (published 2017-02-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install graphql-projection
pnpm add graphql-projection
yarn add graphql-projection
bun add graphql-projection
```

## 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.2 |
| Published | 2017-02-06 |
| First published | 2017-02-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Blake Harley |
| Maintainers | harley |
| Keywords | mongo, mongodb, graphql, project, projection, select |

## Links

- npm: https://www.npmjs.com/package/graphql-projection
- Repository: https://github.com/bharley/graphql-projection
- Homepage: https://github.com/bharley/graphql-projection#readme
- Issues: https://github.com/bharley/graphql-projection/issues
- npm.io page: https://npm.io/package/graphql-projection

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.1.2 (latest) — 2017-02-06
- 0.1.1 — 2017-02-03
- 0.1.0 — 2017-02-02

## README

# graphql-projection

Produces a MongoDB projection by parsing the GraphQL query in a resolve function to
help to provide a concise query to MongoDB.

## Installation

Install with yarn:

```bash
$ yarn add graphql-projection
```

or npm:

```bash
$ npm i -S graphql-projection
```

Now you can set create the projection function by feeding in your schema:

```js
import makeProjector from 'graphql-projection';
import schema from './path/to/schema';

const projector = makeProjector(schema);

// ...
```

## Basic Usage

In your resolve functions in your queries, you can use this function on the resolve info (the
4th argument to your resolve function) to produce a MongoDB projection:

```js
// ...
    {
      type: FoodType,
      args: {
        id: {
          name: 'ID of food to look up',
          type: new GraphQLNonNull(GraphQLInt),
        },
      },
      resolve: (root, { id }, request, resolveInfo) => {
        const projection = projector(resolveInfo);
        
        return db.collection('foods').find({ _id: id }, projection);
      },
    }
// ...
```

## Custom Projections

Not all GraphQL fields will map 1-to-1 with their database counterparts. If you have a computed
field, you can specify the projection that should be used for it by adding a `projection`
property alongside the field's type definition:

```js
const FoodType = new GraphQLObjectType({
  name: 'Food',
  fields: () => ({
    id: { type: new GraphQLNonNull(GraphQLInt) },
    name: { type: new GraphQLNonNull(GraphQLString) },
    reviews: { type: new GraphQLList(ReviewType) },
    averageRating: { type: new GraphQLNonNull(GraphQLInt), projection: 'reviews' },
  }),
});
```

Projection can either be an array of strings that will be added to the projection result (e.g.,
`['property_1', 'property_2', 'property_3']`), or a space-separated string (e.g.,
`'property_1 property_2 property_3'`).

## Contributing

Pull requests are welcome. This project uses [eslint] to ensure a standardized code style, [flow]
to take advantage of all of the flow types GraphQL exports, and [Mocha]+[Chai] for tests.

[eslint]: http://eslint.org/
[flow]: https://flowtype.org/
[Mocha]: https://mochajs.org/
[Chai]: http://chaijs.com/

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