npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@prinzdezibel/graphql-to-mongodb

v1.3.6

Published

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

Downloads

6

Readme

graphql-to-mongodb

Build Status

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

Change Log

Blog Post

Lets 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: () => ({
                firstName: { type: GraphQLString },
                lastName: { type: GraphQLString }
            })
        }),
        fullName: {
            type: GraphQLString,
            resolve: (obj, args, { db }) => `${obj.name.firstName} ${obj.name.lastName}`
        }
    })
})

We'll define the peron query in our GraphQL scheme like so:

person: {
    type: new GraphQLList(PersonType),
    args: getGraphQLQueryArgs(PersonType),
    resolve: getMongoDbQueryResolver(PersonType,
        async (filter, projection, options, obj, args, context) => {
            return await context.db.collection('persons').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 paraneters of the callback, can be sent directly to the MongoDB find function as shown. The rest of the parameter are the standard recieved 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.firstName} ${obj.name.lastName}`,
        dependencies: ['name'] // or ['name.firstName', 'name.LastName'], whatever tickles your fancy
    }
    This is needed to ensure that the projection does not omit any neccessary 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:
    options.projection = projection;
    return await context.db.collection('persons').find(filter, options).toArray();

That's it!

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

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

PersonFilterType:

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

* Filtering is possible over every none resolve field!

NameObjectFilterType:

firstName: StringFilter
lastName: StringFilter
opr: OprExists

OprExists enum tyoe 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]

PersonSortType:

age: SortType

SortType enum can be either ASC or DESC

GraphQLPaginationType:

limit: Int
skip: Int

Example GraphQL Query:

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

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

Aside from the mentioned above, the package comes with functionality galore!

  • getGraphQLFilterType
  • getGraphQLSortType
  • getGraphQLUpdateType
  • getGraphQLInsertType
  • getGraphQLQueryArgs
  • getGraphQLUpdateArgs
  • GraphQLPaginationType
  • getMongoDbFilter
  • getMongoDbProjection
  • getMongoDbUpdate
  • getMongoDbSort
  • getMongoDbQueryResolver
  • getMongoDbUpdateResolver
  • setLogger