Skip to content

Commit

Permalink
feat: add TypeScript definition files (thanks @toverux)
Browse files Browse the repository at this point in the history
* chore(package, typescript): add typescript and tslint configuration(s)

* feat(typescript): add a few .d.ts files

* chore(typescript): mark TS-specific comments as @ts-todo
  • Loading branch information
toverux authored and nodkz committed Aug 9, 2018
1 parent 504aec0 commit 723ae7c
Show file tree
Hide file tree
Showing 35 changed files with 680 additions and 4 deletions.
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"main": "lib/index.js",
"module": "mjs/index.mjs",
"types": "lib/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/graphql-compose/graphql-compose-mongoose.git"
Expand Down Expand Up @@ -39,6 +40,8 @@
"mongoose": ">=4.0.0 || >=5.0.0"
},
"devDependencies": {
"@types/graphql": "^0.13.4",
"@types/mongoose": "^5.2.4",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.6",
Expand Down Expand Up @@ -66,7 +69,9 @@
"prettier": "^1.14.2",
"request": "^2.87.0",
"rimraf": "^2.6.2",
"semantic-release": "^15.9.6"
"semantic-release": "^15.9.6",
"tslint": "^5.11.0",
"typescript": "^3.0.1"
},
"config": {
"commitizen": {
Expand All @@ -86,11 +91,15 @@
"build-mjs": "rimraf mjs && BABEL_ENV=mjs babel src --ignore __tests__,__mocks__ -d mjs && yarn build-mjs-rename && COPY_TO_FOLDER=mjs npm run build-flow",
"build-mjs-rename": "find ./mjs -name \"*.js\" -exec bash -c 'mv \"$1\" \"${1%.js}\".mjs' - '{}' \\;",
"build-flow": "echo `$1` && find ./src -name '*.js' -not -path '*/__*' | while read filepath; do cp $filepath `echo ./${COPY_TO_FOLDER:-lib}$filepath | sed 's/.\\/src\\//\\//g'`.flow; done",
"build-ts": "find ./src -name '*.d.ts' -not -path '*/__*' | while read filepath; do cp $filepath `echo ./${COPY_TO_FOLDER:-lib}$filepath | sed 's/.\\/src\\//\\//g'`; done",
"watch": "jest --watch",
"coverage": "jest --coverage --maxWorkers 2",
"lint": "eslint --ext .js ./src",
"lint": "npm run eslint && npm run tslint",
"eslint": "eslint --ext .js ./src",
"tslint": "tslint -p . \"src/**/*.d.ts\"",
"tscheck": "tsc",
"flow": "./node_modules/.bin/flow",
"test": "npm run coverage && npm run lint && npm run flow",
"test": "npm run coverage && npm run lint && npm run flow && npm run tscheck",
"link": "yarn build && yarn link graphql-compose && yarn link graphql-compose-connection && yarn link mongoose && yarn link",
"unlink": "yarn unlink graphql-compose && yarn unlink graphql-compose-connection && yarn unlink mongoose && yarn add graphql-compose graphql-compose-connection mongoose --dev",
"semantic-release": "semantic-release"
Expand Down
121 changes: 121 additions & 0 deletions src/composeWithMongoose.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { InputTypeComposer, SchemaComposer, TypeComposer } from 'graphql-compose';
import { Document, Model } from 'mongoose';
import { ConnectionSortMapOpts } from './resolvers/connection';
import {
FilterHelperArgsOpts, LimitHelperArgsOpts, RecordHelperArgsOpts, SortHelperArgsOpts,
} from './resolvers/helpers';
import { PaginationResolverOpts } from './resolvers/pagination';

export type TypeConverterOpts = {
schemaComposer?: SchemaComposer<any>,
name?: string,
description?: string,
fields?: {
only?: string[],
remove?: string[],
},
inputType?: TypeConverterInputTypeOpts,
resolvers?: false | TypeConverterResolversOpts,
};

export type TypeConverterInputTypeOpts = {
name?: string,
description?: string,
fields?: {
only?: string[],
remove?: string[],
required?: string[],
},
};

export type TypeConverterResolversOpts = {
findById?: false,
findByIds?:
| false
| {
limit?: LimitHelperArgsOpts | false,
sort?: SortHelperArgsOpts | false,
},
findOne?:
| false
| {
filter?: FilterHelperArgsOpts | false,
sort?: SortHelperArgsOpts | false,
skip?: false,
},
findMany?:
| false
| {
filter?: FilterHelperArgsOpts | false,
sort?: SortHelperArgsOpts | false,
limit?: LimitHelperArgsOpts | false,
skip?: false,
},
updateById?:
| false
| {
record?: RecordHelperArgsOpts | false,
},
updateOne?:
| false
| {
input?: RecordHelperArgsOpts | false,
filter?: FilterHelperArgsOpts | false,
sort?: SortHelperArgsOpts | false,
skip?: false,
},
updateMany?:
| false
| {
record?: RecordHelperArgsOpts | false,
filter?: FilterHelperArgsOpts | false,
sort?: SortHelperArgsOpts | false,
limit?: LimitHelperArgsOpts | false,
skip?: false,
},
removeById?: false,
removeOne?:
| false
| {
filter?: FilterHelperArgsOpts | false,
sort?: SortHelperArgsOpts | false,
},
removeMany?:
| false
| {
filter?: FilterHelperArgsOpts | false,
},
createOne?:
| false
| {
record?: RecordHelperArgsOpts | false,
},
count?:
| false
| {
filter?: FilterHelperArgsOpts | false,
},
connection?: ConnectionSortMapOpts | false,
pagination?: PaginationResolverOpts | false,
};

export function composeWithMongoose(
model: Model<any>,
opts?: TypeConverterOpts): TypeComposer<any>;

export function prepareFields(
tc: TypeComposer<any>,
opts: { only?: string[], remove?: string[] }): void;

export function prepareInputFields(
inputTypeComposer: InputTypeComposer,
inputFieldsOpts: { only?: string[], remove?: string[], required?: string[] }): void;

export function createInputType(
tc: TypeComposer<any>,
inputTypeOpts?: TypeConverterInputTypeOpts): void;

export function createResolvers<TDocument extends Document>(
model: Model<TDocument>,
tc: TypeComposer<any>,
opts: TypeConverterResolversOpts): void;
6 changes: 6 additions & 0 deletions src/composeWithMongooseDiscriminators.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { TypeComposerClass } from 'graphql-compose';
import { Model } from 'mongoose';

export function composeWithMongooseDiscriminators(
baseModel: Model<any>,
opts?: { [opts: string]: any }): TypeComposerClass<any>;
74 changes: 74 additions & 0 deletions src/fieldsConverter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { EnumTypeComposer, SchemaComposer, TypeComposer } from 'graphql-compose';
import { GraphQLScalarType } from 'graphql-compose/lib/graphql';
import { Model, Schema } from 'mongoose';

// @ts-todo MongooseSchemaField<any> in the Flow version, MongooseSchemaField isn't there in mongoose's .d.ts
type MongooseFieldT = any;

type MongooseFieldMapT = { [fieldName: string]: MongooseFieldT };
type ComposeScalarType = string | GraphQLScalarType;

type ComposeOutputType =
| TypeComposer<any> | ComposeScalarType | EnumTypeComposer
| [TypeComposer<any> | ComposeScalarType | EnumTypeComposer];

export type MongoosePseudoModelT = {
schema: Schema,
};

export const ComplexTypes: {
ARRAY: 'ARRAY',
EMBEDDED: 'EMBEDDED',
DOCUMENT_ARRAY: 'DOCUMENT_ARRAY',
ENUM: 'ENUM',
REFERENCE: 'REFERENCE',
SCALAR: 'SCALAR',
MIXED: 'MIXED',
};

export function dotPathsToEmbedded(fields: MongooseFieldMapT): MongooseFieldMapT;

export function getFieldsFromModel(model: Model<any> | MongoosePseudoModelT): MongooseFieldMapT;

export function convertModelToGraphQL(
model: Model<any> | MongoosePseudoModelT,
typeName: string,
sc?: SchemaComposer<any>): TypeComposer<any>;

export function convertSchemaToGraphQL(
schema: Schema,
typeName: string,
sc?: SchemaComposer<any>): TypeComposer<any>;

export function convertFieldToGraphQL(
field: MongooseFieldT,
prefix: string | undefined,
schemaComposer: SchemaComposer<any>
): ComposeOutputType;

export function deriveComplexType(field: MongooseFieldT): keyof typeof ComplexTypes;

export function scalarToGraphQL(field: MongooseFieldT): ComposeScalarType;

export function arrayToGraphQL(
field: MongooseFieldT,
prefix: string | undefined,
schemaComposer: SchemaComposer<any>
): ComposeOutputType;

export function embeddedToGraphQL(
field: MongooseFieldT,
prefix: string | undefined,
schemaComposer: SchemaComposer<any>): TypeComposer<any>;

export function enumToGraphQL(
field: MongooseFieldT,
prefix: string | undefined,
schemaComposer: SchemaComposer<any>): EnumTypeComposer;

export function documentArrayToGraphQL(
field: MongooseFieldT,
prefix: string | undefined,
schemaComposer: SchemaComposer<any>): [TypeComposer<any>];

export function referenceToGraphQL(field: MongooseFieldT): ComposeScalarType;
10 changes: 10 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { composeWithMongoose } from './composeWithMongoose';
import { composeWithMongooseDiscriminators } from './composeWithMongooseDiscriminators';
import GraphQLMongoID from './types/mongoid';

export default composeWithMongoose;

export * from './fieldsConverter';
// @ts-todo export * from './discriminators'; // untyped yet

export { composeWithMongoose, composeWithMongooseDiscriminators, GraphQLMongoID };
21 changes: 21 additions & 0 deletions src/resolvers/connection.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Resolver, TypeComposer } from 'graphql-compose';
// import { ConnectionSortMapOpts } from 'graphql-compose-connection';
import { Model } from 'mongoose';
import { IndexT } from '../utils';

// @ts-todo The ConnectionSortMapOpts is not available yet since graphql-compose-connection doesn't have types for now,
// fallback to a simple object.
export type ConnectionSortMapOpts = { [opt: string]: any };

export default function connection(
model: Model<any>,
tc: TypeComposer<any>,
opts?: ConnectionSortMapOpts): Resolver<any, any> | undefined;

export function prepareCursorQuery(
rawQuery: object,
cursorData: object,
indexKeys: string[],
indexData: IndexT,
nextOper: '$gt' | '$lt',
prevOper: '$lt' | '$gt'): void;
8 changes: 8 additions & 0 deletions src/resolvers/count.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Resolver, TypeComposer } from 'graphql-compose';
import { Model } from 'mongoose';
import { GenResolverOpts } from './index';

export default function count(
model: Model<any>,
tc: TypeComposer<any>,
opts?: GenResolverOpts): Resolver<any, any>;
8 changes: 8 additions & 0 deletions src/resolvers/createOne.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Resolver, TypeComposer } from 'graphql-compose';
import { Model } from 'mongoose';
import { GenResolverOpts } from './index';

export default function createOne(
model: Model<any>,
tc: TypeComposer<any>,
opts?: GenResolverOpts): Resolver<any, any>;
8 changes: 8 additions & 0 deletions src/resolvers/findById.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Resolver, TypeComposer } from 'graphql-compose';
import { Model } from 'mongoose';
import { GenResolverOpts } from './index';

export default function findById(
model: Model<any>,
tc: TypeComposer<any>,
opts?: GenResolverOpts): Resolver<any, any>;
8 changes: 8 additions & 0 deletions src/resolvers/findByIds.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Resolver, TypeComposer } from 'graphql-compose';
import { Model } from 'mongoose';
import { GenResolverOpts } from './index';

export default function findByIds(
model: Model<any>,
tc: TypeComposer<any>,
opts?: GenResolverOpts): Resolver<any, any>;
8 changes: 8 additions & 0 deletions src/resolvers/findMany.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Resolver, TypeComposer } from 'graphql-compose';
import { Model } from 'mongoose';
import { GenResolverOpts } from './index';

export default function findMany(
model: Model<any>,
tc: TypeComposer<any>,
opts?: GenResolverOpts): Resolver<any, any>;
8 changes: 8 additions & 0 deletions src/resolvers/findOne.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Resolver, TypeComposer } from 'graphql-compose';
import { Model } from 'mongoose';
import { GenResolverOpts } from './index';

export default function findOne(
model: Model<any>,
tc: TypeComposer<any>,
opts?: GenResolverOpts): Resolver<any, any>;
22 changes: 22 additions & 0 deletions src/resolvers/helpers/filter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ComposeFieldConfigArgumentMap, TypeComposer } from 'graphql-compose';
import { Model } from 'mongoose';
import { ExtendedResolveParams } from '../index';
import { FilterOperatorsOpts } from './filterOperators';

export type FilterHelperArgsOpts = {
filterTypeName?: string,
isRequired?: boolean,
onlyIndexed?: boolean,
requiredFields?: string | string[],
operators?: FilterOperatorsOpts | false,
removeFields?: string | string[],
};

export function getFilterHelperArgOptsMap(): Partial<Record<keyof FilterHelperArgsOpts, string | string[]>>;

export function filterHelperArgs(
typeComposer: TypeComposer<any>,
model: Model<any>,
opts?: FilterHelperArgsOpts): ComposeFieldConfigArgumentMap;

export function filterHelper(resolveParams: ExtendedResolveParams): void;
21 changes: 21 additions & 0 deletions src/resolvers/helpers/filterOperators.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { InputTypeComposer } from 'graphql-compose';
import { Model } from 'mongoose';
import { ExtendedResolveParams } from '../index';
import { FilterHelperArgsOpts } from './filter';

export type FilterOperatorNames = 'gt' | 'gte' | 'lt' | 'lte' | 'ne' | 'in[]' | 'nin[]';

export const OPERATORS_FIELDNAME: string;

export type FilterOperatorsOpts = {
[fieldName: string]: FilterOperatorNames[] | false,
};

export function addFilterOperators(
itc: InputTypeComposer,
model: Model<any>,
opts: FilterHelperArgsOpts): void;

export function processFilterOperators(
filter: object,
resolveParams: ExtendedResolveParams): void;
Loading

0 comments on commit 723ae7c

Please sign in to comment.