{"id":13679737,"url":"https://github.com/OS-Guild/graphql-to-mongodb","last_synced_at":"2025-04-29T19:31:48.203Z","repository":{"id":37447733,"uuid":"103812521","full_name":"OS-Guild/graphql-to-mongodb","owner":"OS-Guild","description":"Allows for generic run-time generation of filter types for existing graphql types and parsing client requests to mongodb find queries","archived":false,"fork":false,"pushed_at":"2023-11-12T10:30:13.000Z","size":297,"stargazers_count":325,"open_issues_count":20,"forks_count":38,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-17T22:35:21.629Z","etag":null,"topics":["filter","graphql","graphql-js","graphql-server","mongodb","nodejs","pagination","query","sort","update"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OS-Guild.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-09-17T07:57:25.000Z","updated_at":"2025-03-01T15:20:58.000Z","dependencies_parsed_at":"2024-01-14T15:17:41.866Z","dependency_job_id":null,"html_url":"https://github.com/OS-Guild/graphql-to-mongodb","commit_stats":null,"previous_names":["soluto/graphql-to-mongodb","soluto/graphql-to-mongodb-query"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OS-Guild%2Fgraphql-to-mongodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OS-Guild%2Fgraphql-to-mongodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OS-Guild%2Fgraphql-to-mongodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OS-Guild%2Fgraphql-to-mongodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OS-Guild","download_url":"https://codeload.github.com/OS-Guild/graphql-to-mongodb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251569549,"owners_count":21610575,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filter","graphql","graphql-js","graphql-server","mongodb","nodejs","pagination","query","sort","update"],"created_at":"2024-08-02T13:01:08.871Z","updated_at":"2025-04-29T19:31:43.193Z","avatar_url":"https://github.com/OS-Guild.png","language":"TypeScript","readme":"# graphql-to-mongodb \n[![Build Status](https://travis-ci.org/Soluto/graphql-to-mongodb.svg?branch=master)](https://travis-ci.org/Soluto/graphql-to-mongodb)\n\nIf 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!\n\n### [Examples](./examples)\n### [Change Log](./CHANGELOG.md)\n### [Blog Post](https://blog.solutotlv.com/graphql-to-mongodb-or-how-i-learned-to-stop-worrying-and-love-generated-query-apis/?utm_source=README)\n\n### Let's take a look at the most common use case, ```getMongoDbQueryResolver``` and ```getGraphQLQueryArgs```:\n\n**Given a simple GraphQL type:**\n```js\nnew GraphQLObjectType({\n    name: 'PersonType',\n    fields: () =\u003e ({\n        age: { type: GraphQLInt },\n        name: { type: new GraphQLObjectType({\n            name: 'NameType',\n            fields: () =\u003e ({\n                first: { type: GraphQLString },\n                last: { type: GraphQLString }\n            })\n        }),\n        fullName: {\n            type: GraphQLString,\n            resolve: (obj, args, { db }) =\u003e `${obj.name.first} ${obj.name.last}`\n        }\n    })\n})\n```\n#### An example GraphQL query supported by the package:\n\nQueries the first 50 people, oldest first, over the age of 18, and whose first name is John.\n\n```\n{\n    people (\n        filter: {\n            age: { GT: 18 },\n            name: { \n                first: { EQ: \"John\" } \n            }\n        },\n        sort: { age: DESC },\n        pagination: { limit: 50 }\n    ) {\n        fullName\n        age\n    }\n}\n```\n\n**To implement, we'll define the people query field in our GraphQL scheme like so:**\n\n\n```js\npeople: {\n    type: new GraphQLList(PersonType),\n    args: getGraphQLQueryArgs(PersonType),\n    resolve: getMongoDbQueryResolver(PersonType,\n        async (filter, projection, options, obj, args, context) =\u003e {\n            return await context.db.collection('people').find(filter, projection, options).toArray();\n        })\n}\n```\nYou'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. \n\n* Additionally, resolve fields' dependencies should be defined in the GraphQL type like so:\n    ```js \n    fullName: {\n        type: GraphQLString,\n        resolve: (obj, args, { db }) =\u003e `${obj.name.first} ${obj.name.last}`,\n        dependencies: ['name'] // or ['name.first', 'name.Last'], whatever tickles your fancy\n    }\n    ```\n    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.\n*  As of `mongodb` package version 3.0, you should implement the resolve callback as:\n   ```js\n   return await context.db.collection('people').find(filter, options).toArray();\n   ```\n\n### That's it!\n\n**The following field is added to the schema (copied from graphiQl):**\n```\npeople(\n    filter: PersonFilterType\n    sort: PersonSortType\n    pagination: GraphQLPaginationType\n): [PersonType]\n```\n\n**PersonFilterType:**\n```\nage: IntFilter\nname: NameObjectFilterType\nOR: [PersonFilterType]\nAND: [PersonFilterType]\nNOR: [PersonFilterType]\n```\n\\* Filtering is possible over every none resolve field!\n\n**NameObjectFilterType:**\n```\nfirst: StringFilter\nlast: StringFilter\nopr: OprExists\n```\n`OprExists` enum type can be `EXISTS` or `NOT_EXISTS`, and can be found in nested objects and arrays\n\n**StringFilter:**\n```\nEQ: String\nGT: String\nGTE: String\nIN: [String]\nLT: String\nLTE: String\nNEQ: String\nNIN: [String]\nNOT: [StringFNotilter]\n```\n\n**PersonSortType:**\n```\nage: SortType\n```\n`SortType` enum can be either `ASC` or `DESC`\n\n**GraphQLPaginationType:**\n```\nlimit: Int\nskip: Int\n```\n\n### Functionality galore! Also permits update, insert, and extensiable custom fields.\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FOS-Guild%2Fgraphql-to-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FOS-Guild%2Fgraphql-to-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FOS-Guild%2Fgraphql-to-mongodb/lists"}