TypeScript MongoDB
Installation
Config API Reference
type: default:
Customize the suffix for the generated GraphQL s.
Usage Examples
type: default:
Customize the suffix for the generated GraphQL s.
Usage Examples
type: default:
Customize the type of fields. You can either specify a type name, or specify .
Usage Examples
type: default:
Customize the name of the id field generated after using directive over a GraphQL field.
Usage Examples
type: default:
Replaces generated values with .
Usage Examples
type: default:
This will cause the generator to avoid using TypeScript optionals (), so the following definition: will output instead of .
Usage Examples
type: default:
Makes scalars strict.
If scalars are found in the schema that are not defined in an error will be thrown during codegen.
Usage Examples
type: default:
Allows you to override the type that unknown scalars will have.
Usage Examples
type:
Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.
Usage Examples
type: default:
Allow you to override the naming convention of the output. You can either override all namings, or specify an object with specific custom naming convention per output. The format of the converter must be a valid . Allowed values for specific output are: , . You can also use “keep” to keep all GraphQL names as-is. Additionally, you can set to if you want to override the default behavior, which is to preserve underscores.
Available case functions in are , , , , , , , , , , , , , , , , , and See more
Usage Examples
Override All Names
Upper-case enum values
Keep names as is
Remove Underscores
type: default:
Prefixes all the generated types.
Usage Examples
type: default:
Suffixes all the generated types.
Usage Examples
type: default:
Does not add to the generated types, unless it was specified in the selection set.
Usage Examples
type: default:
Automatically adds field to the generated types, even when they are not specified in the selection set, and makes it non-optional
Usage Examples
type: default:
Will use rather than when importing only types. This gives compatibility with TypeScript’s “importsNotUsedAsValues”: “error” option
Usage Examples
type: default:
Removes fragment duplicates for reducing data transfer. It is done by removing sub-fragments imports from fragment definition Instead - all of them are imported to the Operation node.
type: default:
Whether fragment types should be inlined into other operations. “inline” is the default behavior and will perform deep inlining fragment types within operation type definitions. “combine” is the previous behavior that uses fragment type references without inlining the types (and might cause issues with deeply nested fragment that uses list types).
type: default:
Emit legacy common js imports. Default it will be this way it ensure that generated code works with non-compliant bundlers.
This plugin generates TypeScript types for MongoDB models, which makes it relevant for server-side development only. It uses GraphQL directives to declare the types you want to generate and use in your MongoDB backend.
What this plugin does?
Given the following GraphQL declaration:
We can have the following TypeScript output:
This interface can be used for db read/write purposes, thus making communication with the db much more consistent.
Usage Example
Once installed, add the directives’ declaration to your GraphQL Schema definition:
And generate code using :
At this point, you can add the directives to your GraphQL definitions, and generate your MongoDB models file.
Directives
(on )
Use this directive to specify which GraphQL type should have generated MongoDB models.
- - use this option to declare target entity as child of a greater entity. For example, given the following structure , the GraphQL type should be declared as embedded.
- - specify any additional fields that you would like to add to your MongoDB object, and are not a part of your public GraphQL schema.
(on )
Use this directive to declare a specific GraphQL field as part of your generated MongoDB type.
- - use this to override the type of the field; for example, if you store dates as but expose them as .
(on )
Use this directive on the filed that should be mapped to a MongoDB . By default, it should be the field of the GraphQL .
(on )
Use this directive to declare that a specific field is a link to another type in another table. This will use the type in the generated result.
(on )
use this option to declare target entity as child of a greater entity.
(on )
Use this directive to override the path or the name of the target field. This would come in handy whenever we would like to create a more complex object structure in the database; for example, if you wish to project a field as on your schema, but store it as in your DB. You can either specify the name of the field, or a path to which will lead to its corresponding field in the DB.
Given the following GraphQL schema:
The output should be:
(on )
Use this directive on a GraphQL interface to mark it as a basis for other database types. The argument is mandatory and will tell the generator what field name in the database determines what interface the target object is implementing.
For example:
This way, you will get:
(on )
This directive is similar to , but for unions (that don’t necessarily have any common fields). The argument is mandatory and will tell the generator what field name in the database determines what interface the target object is implementing.
Given the following GraphQL schema:
The output should be:
Example
Given the following GraphQL types:
The generated MongoDB models should look like so: