Mongo-To-GQL
Auto-generator for the MongoDB model to GraphQL type definition and query resolvers. Just write your mongoose model code, then I generate gql code for you!
Current - 2.3.3 - (feat): aaronwlee
- enabled the conversion with the list of models and mutations.
- enabled the conversion models and mutations to GQL ready to use
string
andobject
. - changed the mutation naming method because of the unsupported method when built by Webpack. (bug fix)
- test and sample files have rewritten (minor)
2.2.13 - (fix): aaronwlee
- subdirectory supporting has updated (bug fix)
- getting a total query has enhanced (bug fix)
- authentication method for the apollo server has added
- auth boolean option has added into the gqloption
- Now you able to connect with the typescript model and mutation in development mode. Set the 'devWithTs' option and use the nodemon.
- embedded many supported
- embedded search implemented
- customizable apollo server options supported
- customizable resolvers and typeDefs options supported
- file upload support via Upload
Installing
$ npm i mongo-to-gql
Or
$ yarn add mongo-to-gql
Getting Started
Mongo model basic
- just write your basic mongoose model with
gqlOption
ex) src/model/user.model.ts
; ; // JSON typeschema.virtual"fullName".get // JSON typeschema.virtual"extra".get // if you use has many or has one, you must export this option with exact name of const ;
After using our mongo-to-gql, you'll get auto-generated this queries and gql definitions
query getUsers query UserByID
Result
// query getUsers // query UserByID
Let's start it!
Start with ApolloServer
- Development mode | start with nodemon your main ts file
;; ; executeApolloServer.then
- Production Mode - built by Typescript
;; ; executeApolloServer.then
- Production Mode - built by Webpack
;;; // this is important to import an entire module's contents; // more info - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import ; executeApolloServer.then
Start with only Converting
- Development mode | start with nodemon your main ts file
;; ; convertToGQL.then
- Production Mode - built by Typescript
;; ; convertToGQL.then
- Production Mode - built by Webpack
;;; // this is important to import an entire module's contents; // more info - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import ; convertToGQL.then
executeApolloServer({})
options for the
app
is your express appmodelFolderPath
should be your pure js model folder which is starting withpwd
path | but if you setdevWithTs
option to true, then you can able to use the ts model folder.modelList
should be a list of your imported models, and it must be an entire module's contents. ex)import * as
mutationFolderPath
should be your pure js mutation folder which is starting withpwd
path | but if you setdevWithTs
option to true, then you can able to use the ts mutation folder.mutationList
should be a list of your imported mutations, and it must be an entire module's contents. ex)import * as
devWithTs
this option is enable to use your typescript folder path only for development environment. must use nodemon.path
is for graphql path config, default router is/graphql
context
is for authenticating your resolvers. you can basically pass the user parameter or token.apolloOptions
this option for ApolloServer's paramcustomResolvers
is for the extra resolverscustomTypeDefs
is for the extra typeDefs, but it must be string
convertToGQL({})
options for the
modelFolderPath
should be your pure js model folder which is starting withpwd
path | but if you setdevWithTs
option to true, then you can able to use the ts model folder.modelList
should be a list of your imported models, and it must be an entire module's contents. ex)import * as
mutationFolderPath
should be your pure js mutation folder which is starting withpwd
path | but if you setdevWithTs
option to true, then you can able to use the ts mutation folder.mutationList
should be a list of your imported mutations, and it must be an entire module's contents. ex)import * as
devWithTs
this option is enable to use your typescript folder path only for development environment. must use nodemon.customResolvers
is for the extra resolverscustomTypeDefs
is for the extra typeDefs, but it must be string
Promise results
executeApolloServeroptions
- Initializing, building and connect apollo server with
MongoToGQLOptions
- After your express server executed, apollo server will start with
/graphql
router or yourpath
config
- this interface is the promise results of
executeApolloServer
convertToGQLoptions
- it converts your
models
andmutations
to GQL ready to usestring
andobject
.
- this interface is the promise results of
convertToGQL
Examples
Mongo model with mongoosejs
Model sample (in model folder)
Path - src/model/user.model.ts
- exporting the
model
as a default is mandatory! gqlOption
is for joining the foreign table, without this the queries will return the just _ids
Just write your mongoose model code, then I generate gql code for you!
; ; // JSON typeschema.virtual"fullName".get // JSON typeschema.virtual"extra".get // if you use has many or has one, you must export this option with exact name of const ;
model auto-generate results will be
params |
Description |
---|---|
page | Pagination |
limit | page per |
filter | each field name with in (array values are in data), has (regex), ne (array values are not in data); basic field name like name for finding exact data |
filter field JSON |
This field only able to filtering exact same JSON structure |
filter subSearch |
you can search the Embedded structure with a JSON string ex) subSearch: "{'geolocation.latitude': 12.3}" |
sort | each field name with asc or desc, but you can't get sortkey from JSON type |
result |
Description |
---|---|
data | result data object |
page | current page |
total | result data count |
query getUsers query UserByID
Mutation sample (in mutation folder)
Path - src/mutation/addUser.ts
inputType
andresolver
are mandatory! Try to useImutation
interface, it'll be easier.- Your mutation function name will save as starting with a lowercase.
- Make this sure all extra input types must be declared! ex) ProductInputType
- The resolver should be an async method, but it's doesn't matter.
;;; // class name is your mutation's name
resolver return type
mutation auto-generate results will be
params |
Description |
---|---|
input | mutation class's inputType as a gql definition |
result |
Description |
---|---|
done | anything for a result as JSON |
error | error as a JSON type |
mutation AddUser
Authors
- Aaron Wooseok Lee - Initial work - aaronwlee
- Eric Xizheng Ding - Inspiratied - dingxizheng
License
This project is licensed under the MIT License - see the LICENSE.md file for details