• Stars
    star
    314
  • Rank 133,353 (Top 3 %)
  • Language
    TypeScript
  • Created about 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Allows for generic run-time generation of filter types for existing graphql types and parsing client requests to mongodb find queries

graphql-to-mongodb

Build Status

If you want to grant your NodeJS GraphQL service a whole lot of the power of the MongoDB database standing behind it with very little hassle, you've come to the right place!

Examples

Change Log

Blog Post

Let's take a look at the most common use case, getMongoDbQueryResolver and getGraphQLQueryArgs:

Given a simple GraphQL type:

new GraphQLObjectType({
    name: 'PersonType',
    fields: () => ({
        age: { type: GraphQLInt },
        name: { type: new GraphQLObjectType({
            name: 'NameType',
            fields: () => ({
                first: { type: GraphQLString },
                last: { type: GraphQLString }
            })
        }),
        fullName: {
            type: GraphQLString,
            resolve: (obj, args, { db }) => `${obj.name.first} ${obj.name.last}`
        }
    })
})

An example GraphQL query supported by the package:

Queries the first 50 people, oldest first, over the age of 18, and whose first name is John.

{
    people (
        filter: {
            age: { GT: 18 },
            name: { 
                first: { EQ: "John" } 
            }
        },
        sort: { age: DESC },
        pagination: { limit: 50 }
    ) {
        fullName
        age
    }
}

To implement, we'll define the people query field in our GraphQL scheme like so:

people: {
    type: new GraphQLList(PersonType),
    args: getGraphQLQueryArgs(PersonType),
    resolve: getMongoDbQueryResolver(PersonType,
        async (filter, projection, options, obj, args, context) => {
            return await context.db.collection('people').find(filter, projection, options).toArray();
        })
}

You'll notice that integrating the package takes little more than adding some fancy middleware over the resolve function. The filter, projection, options added as the first parameters of the callback, can be sent directly to the MongoDB find function as shown. The rest of the parameter are the standard received from the GraphQL api.

  • Additionally, resolve fields' dependencies should be defined in the GraphQL type like so:
    fullName: {
        type: GraphQLString,
        resolve: (obj, args, { db }) => `${obj.name.first} ${obj.name.last}`,
        dependencies: ['name'] // or ['name.first', 'name.Last'], whatever tickles your fancy
    }
    This is needed to ensure that the projection does not omit any necessary fields. Alternatively, if throughput is of no concern, the projection can be replaced with an empty object.
  • As of mongodb package version 3.0, you should implement the resolve callback as:
    return await context.db.collection('people').find(filter, options).toArray();

That's it!

The following field is added to the schema (copied from graphiQl):

people(
    filter: PersonFilterType
    sort: PersonSortType
    pagination: GraphQLPaginationType
): [PersonType]

PersonFilterType:

age: IntFilter
name: NameObjectFilterType
OR: [PersonFilterType]
AND: [PersonFilterType]
NOR: [PersonFilterType]

* Filtering is possible over every none resolve field!

NameObjectFilterType:

first: StringFilter
last: StringFilter
opr: OprExists

OprExists enum type can be EXISTS or NOT_EXISTS, and can be found in nested objects and arrays

StringFilter:

EQ: String
GT: String
GTE: String
IN: [String]
LT: String
LTE: String
NEQ: String
NIN: [String]
NOT: [StringFNotilter]

PersonSortType:

age: SortType

SortType enum can be either ASC or DESC

GraphQLPaginationType:

limit: Int
skip: Int

Functionality galore! Also permits update, insert, and extensiable custom fields.

More Repositories

1

kamus

An open source, git-ops, zero-trust secret encryption and decryption solution for Kubernetes applications
C#
913
star
2

tweek

Tweek - an open source feature manager
C#
345
star
3

oidc-server-mock

Configurable Mock Server for OpenId Connect
C#
204
star
4

python-flask-sklearn-docker-template

A simple example of python api for real time machine learning, using scikit-learn, Flask and Docker
Python
135
star
5

dynamico

Dynamico allows a remote (web-like) code push work-flow for continuous delivery of specific features native or web.
TypeScript
97
star
6

webdriverio-zap-proxy

Demo - how to easily build security testing for Web App, using Zap and Glue
JavaScript
58
star
7

mobsf-ci

All that is required to run MobSF in the ci
Shell
42
star
8

shisell-js

A service agnostic library for building immutable scoped analytic event dispatchers with extra data, identities and lazy filters.
TypeScript
41
star
9

monitored

A utility for monitoring services ๐Ÿ”
TypeScript
37
star
10

dqd

Dequeue daemon
Go
35
star
11

wordpress-plugin-tests-template

A template for a Wordpress plugin with tests that are running using Docker
Shell
35
star
12

golang-docker-healthcheck-example

Simple HEALTHCHECK solution for Go Docker container
Dockerfile
34
star
13

owasp-zap-glue-ci-images

Ready to use images of Zap and Glue, especially for CI integration.
Shell
33
star
14

stitch

Stitch is a no-code GraphQL tool for your existing APIs and data sources
TypeScript
29
star
15

docker-compose-jest-runner

This package allows to run tests that use docker-compose and supports multi-stage setup.
TypeScript
28
star
16

containers-security-project

A place for documenting threats and mitigations related to containers orchestrators (Kubernetes, Swarm etc)
Gherkin
25
star
17

Miro

MIRO - Merge it robot!
C#
25
star
18

airbag

Tiny OAuth2 and metrics sidecar for your docker containers
C#
23
star
19

fluent-plugin-kubernetes-log-level

Dynamic filtering of kubernetes logs according to pod labels
Ruby
23
star
20

simple-fake-server

Small and simple http server for mocking and asserting http calls
TypeScript
17
star
21

react-shisell

React binding for Shisell-js
TypeScript
8
star
22

mustache-async.js

Logic-less {{mustache}} templates with async view function Support
JavaScript
8
star
23

helm-charts

Soluto's Helm Charts Repository
Smarty
8
star
24

testcafe-reporter-teamcity

This is a TeamCity reporter plugin for TestCafe.
JavaScript
7
star
25

tweek-clients

clients for https://www.github.com/soluto/tweek
TypeScript
6
star
26

casbin-nats-watcher

Casbin watcher implementation with Nats.io
Go
5
star
27

flowz

Flowz is a library for writing resumable asynchronous code
JavaScript
4
star
28

soluto-kafka

Java
4
star
29

test-ssl-cipher-suites

A ruby script that use NMap to test SSL cipher suites
Ruby
3
star
30

shisell-python

Shisell is a service agnostic abstraction for analytic dispatchers.
Python
3
star
31

nagios-plugins

JavaScript
3
star
32

fetch-jwk

This library provides methods to fetch jwt keys from jwks url
Go
2
star
33

linkerd-disable-injection-mutation-webhook

Go
2
star
34

Tweek.JPad

JPad is the default rules engine for Tweek.
F#
2
star
35

taking-care-of-quizness-ui

JavaScript
2
star
36

congo-examples

JavaScript
1
star
37

react-tweek-shop-example

JavaScript
1
star
38

react-native-tweek-example

Objective-C
1
star
39

tweek-deploy

TypeScript
1
star
40

IdentityServer.Contrib.JsonWebKeyAdapter

A small nuget package allow to work with JsonWebKey instead of X509Certificate.
C#
1
star
41

tweek-authoring-php-client

NOT OFFICIAL - PHP client for Tweek Authoring, auto-generated from swagger
PHP
1
star
42

taking-care-of-quizness-api

JavaScript
1
star