0.18.2 • Published 5 years ago

graphql-codegen-typescript-mongodb-template v0.18.2

Weekly downloads
3,032
License
-
Repository
-
Last release
5 years ago

TypeScript MongoDB Typings

Overview

This package generates TypeScript types for MongoDB server-side developers.

It uses GraphQL directives to declare the types you want to generate and use in your MongoDB.

It take a declaration like this:

type User @entity {
    id: String @id
    username: String! @column
    email: @column
}

And generates a TypeScript interface like this:

import { ObjectID } from 'mongodb';

export interface UserDbObject {
  _id: ObjectID;
  username: string;
  email?: string | null;
}

So you can use it when you read/write to your database, and expect a specific structure.

How to use?

To use this package, install is using Yarn into your project, as a dependency:

yarn add graphql-codegen-typescript-mongodb-template

Then, you need to add the directives declaration to your GraphQL Schema definition:

import { makeExecutableSchema } from 'graphql-tools';
import { DIRECTIVES } from 'graphql-codegen-typescript-mongodb-template';

const schema = makeExecutableSchema({
  typeDefs: [
    DIRECTIVES
    // the rest of your GraphQL types
  ],
  resolvers
});

Now, use this template with the GraphQL code generator:

gql-gen --template graphql-codegen-typescript-mongodb-template --schema ./src/my-schema.js

IMPORTANT: Because of GraphQL behavior, you have to use --schema and provide a JavaScript export, otherwise you will lose your GraphQL Directives usage

Now, add the directives to your GraphQL definitions, and generate your MongoDB models file.

Directives

@entity(embedded: Boolean) (on TYPE)

Use this directive to declare that a specific GraphQL type should have a generated MongoDB models.

  • embedded: Boolean - specify this to declare that this entity turns into an object inside another entity. For example, if you want your structure to be { _id: string, username: string, profile: { name: string }}, then your GraphQL type Profile should be an embedded type.

@column(overrideType: String, overrideIsArray: Boolean) (on FIELD)

Use this directive to declare that a specific GraphQL field should be part of your generated MongoDB type.

  • overrideType: String - use this to override the type of the field.
  • overrideIsArray: Boolean - specify true to override the generated result and force it to generate an array type.

Note that if your property is an embedded entity, you should use @embedded instead.

@id (on FIELD)

Use this directive on your field that turns into your MongoDB _id. Usually it's your id field of the GraphQL type.

@link (on FIELD)

Use this directive to declare that a specific field is a link to another type in another table. This will use the ObjectID type in your generated result.

@embedded (on FIELD)

Use this directive to declare that a specific field is integrated into the parent entity, and should be an object inside it.

Example

For example, if you have a User (that matches to users table), Profile (which is part of your User) and Friends which is an array of ObjectID inside your user, use the following schema:

type User @entity {
    id: String! @id
    username: String! @column
    email: String! @column
    profile: Profile! @embedded
    friendsCount: Int! # this field won't get a generated MongoDB field
    friends: [User]! @link
}

type Profile @entity(embedded: true) {
    name: String! @column
    age: Int! @column
}

The output will be:

import { ObjectID } from 'mongodb';

export interface UserDbObject {
  _id: ObjectID;
  username: string;
  email: string;
  profile: ProfileDbObject;
  friends: ObjectID[];
}

export interface ProfileDbObject {
  name: string;
  age: string;
}
0.18.2

5 years ago

0.18.1

5 years ago

0.18.1-alpha.16

5 years ago

0.18.1-alpha.6

5 years ago

0.18.0

5 years ago

0.17.0

5 years ago

0.16.1

5 years ago

0.16.0

5 years ago

0.15.2

5 years ago

0.15.1

5 years ago

0.15.0

5 years ago

0.14.5

5 years ago

0.14.4

5 years ago

0.14.3

5 years ago

0.14.2

5 years ago

0.14.1

5 years ago

0.14.0

5 years ago

0.13.0

6 years ago

0.12.6

6 years ago

0.12.5

6 years ago

0.12.4

6 years ago

0.12.3

6 years ago

0.12.2

6 years ago

0.12.1

6 years ago

0.12.0

6 years ago

0.11.0

6 years ago

0.11.0-alpha.0

6 years ago

0.10.7

6 years ago

0.10.6

6 years ago

0.10.5

6 years ago

0.10.4

6 years ago

0.10.3

6 years ago

0.10.2

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.4

6 years ago

0.9.3

6 years ago

0.9.2

6 years ago

0.9.1

6 years ago

0.9.0

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago