From ac89f6fd8bcc16bcdab5a13eb2886ab719da6283 Mon Sep 17 00:00:00 2001 From: Harry Brundage Date: Fri, 19 Jun 2020 14:41:54 -0400 Subject: [PATCH] Update TypeScript types to export strongly typed extension interfaces After https://github.com/graphql/graphql-js/pull/2465 , we can now use TypeScript declaration merging to augment the graphql-types nice and cleanly. Woop woop! --- package-lock.json | 8 +- package.json | 4 +- src/index.d.ts | 197 ++++++++++++++++++++++++---------------------- 3 files changed, 109 insertions(+), 100 deletions(-) diff --git a/package-lock.json b/package-lock.json index 776a5f37..7222df7c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "join-monster", - "version": "2.1.2", + "version": "3.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -7114,9 +7114,9 @@ "dev": true }, "graphql": { - "version": "15.1.0", - "resolved": "https://registry.npmjs.org/graphql/-/graphql-15.1.0.tgz", - "integrity": "sha512-0TVyfOlCGhv/DBczQkJmwXOK6fjWkjzY3Pt7wY8i0gcYXq8aogG3weCsg48m72lywKSeOqedEHvVPOvZvSD51Q==", + "version": "15.2.0", + "resolved": "https://registry.npmjs.org/graphql/-/graphql-15.2.0.tgz", + "integrity": "sha512-tsceRyHfgzZo+ee0YK3o8f0CR0cXAXxRlxoORWFo/CoM1bVy3UXGWeyzBcf+Y6oqPvO27BDmOEVATcunOO/MrQ==", "dev": true }, "graphql-relay": { diff --git a/package.json b/package.json index 685b0bf4..f28a0686 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ }, "homepage": "https://github.com/join-monster/join-monster#readme", "peerDependencies": { - "graphql": "15" + "graphql": "^15.2.0" }, "devDependencies": { "@ava/babel": "^1.0.1", @@ -93,7 +93,7 @@ "eslint-config-airbnb-base": "^14.1.0", "eslint-config-prettier": "^6.11.0", "faker": "^4.1.0", - "graphql": "^15.1.0", + "graphql": "^15.2.0", "graphsiql": "0.2.0", "idx": "^2.5.6", "jsdoc-to-markdown": "^5.0.0", diff --git a/src/index.d.ts b/src/index.d.ts index 5c9cfccb..035158c6 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -3,109 +3,118 @@ export type Maybe = null | undefined | T // Extend graphql objects and fields -declare module 'graphql/type/definition' { - type SqlJoin = ( - table1: string, - table2: string, +export type SqlJoin = ( + table1: string, + table2: string, + args: TArgs, + context: TContext, + sqlASTNode: any +) => string +export type Where = ( + usersTable: string, + args: TArgs, + context: TContext, + sqlASTNode: any +) => string | void +export type Order = 'ASC' | 'asc' | 'DESC' | 'desc' +export type OrderBy = string | { [key: string]: Order } +export type ThunkWithArgsCtx = + | ((args: TArgs, context: TContext) => T) + | T + +export interface ObjectTypeExtension { + alwaysFetch?: string + sqlTable?: ThunkWithArgsCtx + uniqueKey?: string | string[] +} + +export interface FieldConfigExtension { + ignoreAll?: boolean + ignoreTable?: boolean + junction?: { + include?: ThunkWithArgsCtx< + { + sqlColumn?: string + sqlExpr?: string + sqlDeps?: string | string[] + }, + TContext, + TArgs + > + orderBy?: ThunkWithArgsCtx + sortKey?: ThunkWithArgsCtx< + { + order: Order + key: string | string[] + }, + TContext, + TArgs + > + sqlBatch?: { + thisKey: string + parentKey: string + sqlJoin: SqlJoin + } + sqlJoins?: [SqlJoin, SqlJoin] + sqlTable: ThunkWithArgsCtx + uniqueKey?: string | string[] + where?: Where + } + limit?: ThunkWithArgsCtx + orderBy?: ThunkWithArgsCtx + sortKey?: ThunkWithArgsCtx< + { + order: Order + key: string | string[] + }, + TContext, + TArgs + > + sqlBatch?: { + thisKey: string + parentKey: string + } + sqlColumn?: string + sqlDeps?: string[] + sqlExpr?: ( + table: string, args: TArgs, context: TContext, sqlASTNode: any ) => string - type Where = ( - usersTable: string, - args: TArgs, - context: TContext, - sqlASTNode: any - ) => string | void - type Order = 'ASC' | 'asc' | 'DESC' | 'desc' - type OrderBy = string | { [key: string]: Order } - type ThunkWithArgsCtx = - | ((args: TArgs, context: TContext) => T) - | T - - export interface GraphQLObjectTypeConfig { - extensions?: Maybe>> & { - alwaysFetch?: string - sqlTable?: ThunkWithArgsCtx - uniqueKey?: string | string[] - } - } + sqlJoin?: SqlJoin + sqlPaginate?: boolean + where?: Where +} - export interface GraphQLFieldConfig { - extensions?: Maybe>> & { - ignoreAll?: boolean - ignoreTable?: boolean - junction?: { - include?: ThunkWithArgsCtx< - { - sqlColumn?: string - sqlExpr?: string - sqlDeps?: string | string[] - }, - TContext, - TArgs - > - orderBy?: ThunkWithArgsCtx - sortKey?: ThunkWithArgsCtx< - { - order: Order - key: string | string[] - }, - TContext, - TArgs - > - sqlBatch?: { - thisKey: string - parentKey: string - sqlJoin: SqlJoin - } - sqlJoins?: [SqlJoin, SqlJoin] - sqlTable: ThunkWithArgsCtx - uniqueKey?: string | string[] - where?: Where - } - limit?: ThunkWithArgsCtx - orderBy?: ThunkWithArgsCtx - sortKey?: ThunkWithArgsCtx< - { - order: Order - key: string | string[] - }, - TContext, - TArgs - > - sqlBatch?: { - thisKey: string - parentKey: string - } - sqlColumn?: string - sqlDeps?: string[] - sqlExpr?: ( - table: string, - args: TArgs, - context: TContext, - sqlASTNode: any - ) => string - sqlJoin?: SqlJoin - sqlPaginate?: boolean - where?: Where - } - } +export interface UnionTypeExtension { + sqlTable?: string + uniqueKey?: string | string[] + alwaysFetch?: string } -export interface GraphQLUnionTypeConfig { - extensions?: Maybe>> & { - sqlTable?: string - uniqueKey?: string | string[] - alwaysFetch?: string - } +export interface InterfaceTypeExtension { + sqlTable?: string + uniqueKey?: string | string[] + alwaysFetch?: string } -export interface GraphQLInterfaceTypeConfig { - extensions: Maybe>> & { - sqlTable?: string - uniqueKey?: string | string[] - alwaysFetch?: string +declare module 'graphql' { + interface GraphQLObjectTypeExtensions { + joinMonster?: ObjectTypeExtension + } + interface GraphQLFieldExtensions< + TSource, + TContext, + TArgs = { [argName: string]: any } + > { + joinMonster?: FieldConfigExtension + } + interface GraphQLUnionTypeExtensions { + joinMonster?: UnionTypeExtension + } + interface GraphQLInterfaceTypeExtensions { + joinMonster?: InterfaceTypeExtension } }