Skip to content

Commit 723ae7c

Browse files
toveruxnodkz
authored andcommitted
feat: add TypeScript definition files (thanks @toverux)
* 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
1 parent 504aec0 commit 723ae7c

35 files changed

+680
-4
lines changed

package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
],
1111
"main": "lib/index.js",
1212
"module": "mjs/index.mjs",
13+
"types": "lib/index.d.ts",
1314
"repository": {
1415
"type": "git",
1516
"url": "https://github.com/graphql-compose/graphql-compose-mongoose.git"
@@ -39,6 +40,8 @@
3940
"mongoose": ">=4.0.0 || >=5.0.0"
4041
},
4142
"devDependencies": {
43+
"@types/graphql": "^0.13.4",
44+
"@types/mongoose": "^5.2.4",
4245
"babel-cli": "^6.26.0",
4346
"babel-core": "^6.26.3",
4447
"babel-eslint": "^8.2.6",
@@ -66,7 +69,9 @@
6669
"prettier": "^1.14.2",
6770
"request": "^2.87.0",
6871
"rimraf": "^2.6.2",
69-
"semantic-release": "^15.9.6"
72+
"semantic-release": "^15.9.6",
73+
"tslint": "^5.11.0",
74+
"typescript": "^3.0.1"
7075
},
7176
"config": {
7277
"commitizen": {
@@ -86,11 +91,15 @@
8691
"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",
8792
"build-mjs-rename": "find ./mjs -name \"*.js\" -exec bash -c 'mv \"$1\" \"${1%.js}\".mjs' - '{}' \\;",
8893
"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",
94+
"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",
8995
"watch": "jest --watch",
9096
"coverage": "jest --coverage --maxWorkers 2",
91-
"lint": "eslint --ext .js ./src",
97+
"lint": "npm run eslint && npm run tslint",
98+
"eslint": "eslint --ext .js ./src",
99+
"tslint": "tslint -p . \"src/**/*.d.ts\"",
100+
"tscheck": "tsc",
92101
"flow": "./node_modules/.bin/flow",
93-
"test": "npm run coverage && npm run lint && npm run flow",
102+
"test": "npm run coverage && npm run lint && npm run flow && npm run tscheck",
94103
"link": "yarn build && yarn link graphql-compose && yarn link graphql-compose-connection && yarn link mongoose && yarn link",
95104
"unlink": "yarn unlink graphql-compose && yarn unlink graphql-compose-connection && yarn unlink mongoose && yarn add graphql-compose graphql-compose-connection mongoose --dev",
96105
"semantic-release": "semantic-release"

src/composeWithMongoose.d.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { InputTypeComposer, SchemaComposer, TypeComposer } from 'graphql-compose';
2+
import { Document, Model } from 'mongoose';
3+
import { ConnectionSortMapOpts } from './resolvers/connection';
4+
import {
5+
FilterHelperArgsOpts, LimitHelperArgsOpts, RecordHelperArgsOpts, SortHelperArgsOpts,
6+
} from './resolvers/helpers';
7+
import { PaginationResolverOpts } from './resolvers/pagination';
8+
9+
export type TypeConverterOpts = {
10+
schemaComposer?: SchemaComposer<any>,
11+
name?: string,
12+
description?: string,
13+
fields?: {
14+
only?: string[],
15+
remove?: string[],
16+
},
17+
inputType?: TypeConverterInputTypeOpts,
18+
resolvers?: false | TypeConverterResolversOpts,
19+
};
20+
21+
export type TypeConverterInputTypeOpts = {
22+
name?: string,
23+
description?: string,
24+
fields?: {
25+
only?: string[],
26+
remove?: string[],
27+
required?: string[],
28+
},
29+
};
30+
31+
export type TypeConverterResolversOpts = {
32+
findById?: false,
33+
findByIds?:
34+
| false
35+
| {
36+
limit?: LimitHelperArgsOpts | false,
37+
sort?: SortHelperArgsOpts | false,
38+
},
39+
findOne?:
40+
| false
41+
| {
42+
filter?: FilterHelperArgsOpts | false,
43+
sort?: SortHelperArgsOpts | false,
44+
skip?: false,
45+
},
46+
findMany?:
47+
| false
48+
| {
49+
filter?: FilterHelperArgsOpts | false,
50+
sort?: SortHelperArgsOpts | false,
51+
limit?: LimitHelperArgsOpts | false,
52+
skip?: false,
53+
},
54+
updateById?:
55+
| false
56+
| {
57+
record?: RecordHelperArgsOpts | false,
58+
},
59+
updateOne?:
60+
| false
61+
| {
62+
input?: RecordHelperArgsOpts | false,
63+
filter?: FilterHelperArgsOpts | false,
64+
sort?: SortHelperArgsOpts | false,
65+
skip?: false,
66+
},
67+
updateMany?:
68+
| false
69+
| {
70+
record?: RecordHelperArgsOpts | false,
71+
filter?: FilterHelperArgsOpts | false,
72+
sort?: SortHelperArgsOpts | false,
73+
limit?: LimitHelperArgsOpts | false,
74+
skip?: false,
75+
},
76+
removeById?: false,
77+
removeOne?:
78+
| false
79+
| {
80+
filter?: FilterHelperArgsOpts | false,
81+
sort?: SortHelperArgsOpts | false,
82+
},
83+
removeMany?:
84+
| false
85+
| {
86+
filter?: FilterHelperArgsOpts | false,
87+
},
88+
createOne?:
89+
| false
90+
| {
91+
record?: RecordHelperArgsOpts | false,
92+
},
93+
count?:
94+
| false
95+
| {
96+
filter?: FilterHelperArgsOpts | false,
97+
},
98+
connection?: ConnectionSortMapOpts | false,
99+
pagination?: PaginationResolverOpts | false,
100+
};
101+
102+
export function composeWithMongoose(
103+
model: Model<any>,
104+
opts?: TypeConverterOpts): TypeComposer<any>;
105+
106+
export function prepareFields(
107+
tc: TypeComposer<any>,
108+
opts: { only?: string[], remove?: string[] }): void;
109+
110+
export function prepareInputFields(
111+
inputTypeComposer: InputTypeComposer,
112+
inputFieldsOpts: { only?: string[], remove?: string[], required?: string[] }): void;
113+
114+
export function createInputType(
115+
tc: TypeComposer<any>,
116+
inputTypeOpts?: TypeConverterInputTypeOpts): void;
117+
118+
export function createResolvers<TDocument extends Document>(
119+
model: Model<TDocument>,
120+
tc: TypeComposer<any>,
121+
opts: TypeConverterResolversOpts): void;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { TypeComposerClass } from 'graphql-compose';
2+
import { Model } from 'mongoose';
3+
4+
export function composeWithMongooseDiscriminators(
5+
baseModel: Model<any>,
6+
opts?: { [opts: string]: any }): TypeComposerClass<any>;

src/fieldsConverter.d.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { EnumTypeComposer, SchemaComposer, TypeComposer } from 'graphql-compose';
2+
import { GraphQLScalarType } from 'graphql-compose/lib/graphql';
3+
import { Model, Schema } from 'mongoose';
4+
5+
// @ts-todo MongooseSchemaField<any> in the Flow version, MongooseSchemaField isn't there in mongoose's .d.ts
6+
type MongooseFieldT = any;
7+
8+
type MongooseFieldMapT = { [fieldName: string]: MongooseFieldT };
9+
type ComposeScalarType = string | GraphQLScalarType;
10+
11+
type ComposeOutputType =
12+
| TypeComposer<any> | ComposeScalarType | EnumTypeComposer
13+
| [TypeComposer<any> | ComposeScalarType | EnumTypeComposer];
14+
15+
export type MongoosePseudoModelT = {
16+
schema: Schema,
17+
};
18+
19+
export const ComplexTypes: {
20+
ARRAY: 'ARRAY',
21+
EMBEDDED: 'EMBEDDED',
22+
DOCUMENT_ARRAY: 'DOCUMENT_ARRAY',
23+
ENUM: 'ENUM',
24+
REFERENCE: 'REFERENCE',
25+
SCALAR: 'SCALAR',
26+
MIXED: 'MIXED',
27+
};
28+
29+
export function dotPathsToEmbedded(fields: MongooseFieldMapT): MongooseFieldMapT;
30+
31+
export function getFieldsFromModel(model: Model<any> | MongoosePseudoModelT): MongooseFieldMapT;
32+
33+
export function convertModelToGraphQL(
34+
model: Model<any> | MongoosePseudoModelT,
35+
typeName: string,
36+
sc?: SchemaComposer<any>): TypeComposer<any>;
37+
38+
export function convertSchemaToGraphQL(
39+
schema: Schema,
40+
typeName: string,
41+
sc?: SchemaComposer<any>): TypeComposer<any>;
42+
43+
export function convertFieldToGraphQL(
44+
field: MongooseFieldT,
45+
prefix: string | undefined,
46+
schemaComposer: SchemaComposer<any>
47+
): ComposeOutputType;
48+
49+
export function deriveComplexType(field: MongooseFieldT): keyof typeof ComplexTypes;
50+
51+
export function scalarToGraphQL(field: MongooseFieldT): ComposeScalarType;
52+
53+
export function arrayToGraphQL(
54+
field: MongooseFieldT,
55+
prefix: string | undefined,
56+
schemaComposer: SchemaComposer<any>
57+
): ComposeOutputType;
58+
59+
export function embeddedToGraphQL(
60+
field: MongooseFieldT,
61+
prefix: string | undefined,
62+
schemaComposer: SchemaComposer<any>): TypeComposer<any>;
63+
64+
export function enumToGraphQL(
65+
field: MongooseFieldT,
66+
prefix: string | undefined,
67+
schemaComposer: SchemaComposer<any>): EnumTypeComposer;
68+
69+
export function documentArrayToGraphQL(
70+
field: MongooseFieldT,
71+
prefix: string | undefined,
72+
schemaComposer: SchemaComposer<any>): [TypeComposer<any>];
73+
74+
export function referenceToGraphQL(field: MongooseFieldT): ComposeScalarType;

src/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { composeWithMongoose } from './composeWithMongoose';
2+
import { composeWithMongooseDiscriminators } from './composeWithMongooseDiscriminators';
3+
import GraphQLMongoID from './types/mongoid';
4+
5+
export default composeWithMongoose;
6+
7+
export * from './fieldsConverter';
8+
// @ts-todo export * from './discriminators'; // untyped yet
9+
10+
export { composeWithMongoose, composeWithMongooseDiscriminators, GraphQLMongoID };

src/resolvers/connection.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Resolver, TypeComposer } from 'graphql-compose';
2+
// import { ConnectionSortMapOpts } from 'graphql-compose-connection';
3+
import { Model } from 'mongoose';
4+
import { IndexT } from '../utils';
5+
6+
// @ts-todo The ConnectionSortMapOpts is not available yet since graphql-compose-connection doesn't have types for now,
7+
// fallback to a simple object.
8+
export type ConnectionSortMapOpts = { [opt: string]: any };
9+
10+
export default function connection(
11+
model: Model<any>,
12+
tc: TypeComposer<any>,
13+
opts?: ConnectionSortMapOpts): Resolver<any, any> | undefined;
14+
15+
export function prepareCursorQuery(
16+
rawQuery: object,
17+
cursorData: object,
18+
indexKeys: string[],
19+
indexData: IndexT,
20+
nextOper: '$gt' | '$lt',
21+
prevOper: '$lt' | '$gt'): void;

src/resolvers/count.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Resolver, TypeComposer } from 'graphql-compose';
2+
import { Model } from 'mongoose';
3+
import { GenResolverOpts } from './index';
4+
5+
export default function count(
6+
model: Model<any>,
7+
tc: TypeComposer<any>,
8+
opts?: GenResolverOpts): Resolver<any, any>;

src/resolvers/createOne.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Resolver, TypeComposer } from 'graphql-compose';
2+
import { Model } from 'mongoose';
3+
import { GenResolverOpts } from './index';
4+
5+
export default function createOne(
6+
model: Model<any>,
7+
tc: TypeComposer<any>,
8+
opts?: GenResolverOpts): Resolver<any, any>;

src/resolvers/findById.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Resolver, TypeComposer } from 'graphql-compose';
2+
import { Model } from 'mongoose';
3+
import { GenResolverOpts } from './index';
4+
5+
export default function findById(
6+
model: Model<any>,
7+
tc: TypeComposer<any>,
8+
opts?: GenResolverOpts): Resolver<any, any>;

src/resolvers/findByIds.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Resolver, TypeComposer } from 'graphql-compose';
2+
import { Model } from 'mongoose';
3+
import { GenResolverOpts } from './index';
4+
5+
export default function findByIds(
6+
model: Model<any>,
7+
tc: TypeComposer<any>,
8+
opts?: GenResolverOpts): Resolver<any, any>;

src/resolvers/findMany.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Resolver, TypeComposer } from 'graphql-compose';
2+
import { Model } from 'mongoose';
3+
import { GenResolverOpts } from './index';
4+
5+
export default function findMany(
6+
model: Model<any>,
7+
tc: TypeComposer<any>,
8+
opts?: GenResolverOpts): Resolver<any, any>;

src/resolvers/findOne.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Resolver, TypeComposer } from 'graphql-compose';
2+
import { Model } from 'mongoose';
3+
import { GenResolverOpts } from './index';
4+
5+
export default function findOne(
6+
model: Model<any>,
7+
tc: TypeComposer<any>,
8+
opts?: GenResolverOpts): Resolver<any, any>;

src/resolvers/helpers/filter.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ComposeFieldConfigArgumentMap, TypeComposer } from 'graphql-compose';
2+
import { Model } from 'mongoose';
3+
import { ExtendedResolveParams } from '../index';
4+
import { FilterOperatorsOpts } from './filterOperators';
5+
6+
export type FilterHelperArgsOpts = {
7+
filterTypeName?: string,
8+
isRequired?: boolean,
9+
onlyIndexed?: boolean,
10+
requiredFields?: string | string[],
11+
operators?: FilterOperatorsOpts | false,
12+
removeFields?: string | string[],
13+
};
14+
15+
export function getFilterHelperArgOptsMap(): Partial<Record<keyof FilterHelperArgsOpts, string | string[]>>;
16+
17+
export function filterHelperArgs(
18+
typeComposer: TypeComposer<any>,
19+
model: Model<any>,
20+
opts?: FilterHelperArgsOpts): ComposeFieldConfigArgumentMap;
21+
22+
export function filterHelper(resolveParams: ExtendedResolveParams): void;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { InputTypeComposer } from 'graphql-compose';
2+
import { Model } from 'mongoose';
3+
import { ExtendedResolveParams } from '../index';
4+
import { FilterHelperArgsOpts } from './filter';
5+
6+
export type FilterOperatorNames = 'gt' | 'gte' | 'lt' | 'lte' | 'ne' | 'in[]' | 'nin[]';
7+
8+
export const OPERATORS_FIELDNAME: string;
9+
10+
export type FilterOperatorsOpts = {
11+
[fieldName: string]: FilterOperatorNames[] | false,
12+
};
13+
14+
export function addFilterOperators(
15+
itc: InputTypeComposer,
16+
model: Model<any>,
17+
opts: FilterHelperArgsOpts): void;
18+
19+
export function processFilterOperators(
20+
filter: object,
21+
resolveParams: ExtendedResolveParams): void;

0 commit comments

Comments
 (0)