Skip to content

Commit

Permalink
Update TypeScript types to export strongly typed extension interfaces
Browse files Browse the repository at this point in the history
After graphql/graphql-js#2465 , we can now use TypeScript declaration merging to augment the graphql-types nice and cleanly. Woop woop!
  • Loading branch information
airhorns committed Jul 2, 2020
1 parent e8ad2f1 commit 81fcac2
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 100 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
197 changes: 103 additions & 94 deletions src/index.d.ts
Expand Up @@ -3,109 +3,118 @@ export type Maybe<T> = null | undefined | T

// Extend graphql objects and fields

declare module 'graphql/type/definition' {
type SqlJoin<TContext, TArgs> = (
table1: string,
table2: string,
export type SqlJoin<TContext, TArgs> = (
table1: string,
table2: string,
args: TArgs,
context: TContext,
sqlASTNode: any
) => string
export type Where<TContext, TArgs> = (
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<T, TContext, TArgs> =
| ((args: TArgs, context: TContext) => T)
| T

export interface ObjectTypeExtension<TSource, TContext> {
alwaysFetch?: string
sqlTable?: ThunkWithArgsCtx<string, any, TContext>
uniqueKey?: string | string[]
}

export interface FieldConfigExtension<TSource, TContext, TArgs> {
ignoreAll?: boolean
ignoreTable?: boolean
junction?: {
include?: ThunkWithArgsCtx<
{
sqlColumn?: string
sqlExpr?: string
sqlDeps?: string | string[]
},
TContext,
TArgs
>
orderBy?: ThunkWithArgsCtx<OrderBy, TContext, TArgs>
sortKey?: ThunkWithArgsCtx<
{
order: Order
key: string | string[]
},
TContext,
TArgs
>
sqlBatch?: {
thisKey: string
parentKey: string
sqlJoin: SqlJoin<TContext, TArgs>
}
sqlJoins?: [SqlJoin<TContext, TArgs>, SqlJoin<TContext, TArgs>]
sqlTable: ThunkWithArgsCtx<string, TContext, TArgs>
uniqueKey?: string | string[]
where?: Where<any, TArgs>
}
limit?: ThunkWithArgsCtx<number, any, TContext>
orderBy?: ThunkWithArgsCtx<OrderBy, TContext, TArgs>
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<TContext, TArgs> = (
usersTable: string,
args: TArgs,
context: TContext,
sqlASTNode: any
) => string | void
type Order = 'ASC' | 'asc' | 'DESC' | 'desc'
type OrderBy = string | { [key: string]: Order }
type ThunkWithArgsCtx<T, TContext, TArgs> =
| ((args: TArgs, context: TContext) => T)
| T

export interface GraphQLObjectTypeConfig<TSource, TContext> {
extensions?: Maybe<Readonly<Record<string, any>>> & {
alwaysFetch?: string
sqlTable?: ThunkWithArgsCtx<string, any, TContext>
uniqueKey?: string | string[]
}
}
sqlJoin?: SqlJoin<TContext, TArgs>
sqlPaginate?: boolean
where?: Where<TContext, TArgs>
}

export interface GraphQLFieldConfig<TSource, TContext, TArgs> {
extensions?: Maybe<Readonly<Record<string, any>>> & {
ignoreAll?: boolean
ignoreTable?: boolean
junction?: {
include?: ThunkWithArgsCtx<
{
sqlColumn?: string
sqlExpr?: string
sqlDeps?: string | string[]
},
TContext,
TArgs
>
orderBy?: ThunkWithArgsCtx<OrderBy, TContext, TArgs>
sortKey?: ThunkWithArgsCtx<
{
order: Order
key: string | string[]
},
TContext,
TArgs
>
sqlBatch?: {
thisKey: string
parentKey: string
sqlJoin: SqlJoin<TContext, TArgs>
}
sqlJoins?: [SqlJoin<TContext, TArgs>, SqlJoin<TContext, TArgs>]
sqlTable: ThunkWithArgsCtx<string, TContext, TArgs>
uniqueKey?: string | string[]
where?: Where<TContext, TArgs>
}
limit?: ThunkWithArgsCtx<number, any, TContext>
orderBy?: ThunkWithArgsCtx<OrderBy, TContext, TArgs>
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<TContext, TArgs>
sqlPaginate?: boolean
where?: Where<TContext, TArgs>
}
}
export interface UnionTypeExtension {
sqlTable?: string
uniqueKey?: string | string[]
alwaysFetch?: string
}

export interface GraphQLUnionTypeConfig<TSource, TContext> {
extensions?: Maybe<Readonly<Record<string, any>>> & {
sqlTable?: string
uniqueKey?: string | string[]
alwaysFetch?: string
}
export interface InterfaceTypeExtension {
sqlTable?: string
uniqueKey?: string | string[]
alwaysFetch?: string
}

export interface GraphQLInterfaceTypeConfig<TSource, TContext> {
extensions: Maybe<Readonly<Record<string, any>>> & {
sqlTable?: string
uniqueKey?: string | string[]
alwaysFetch?: string
declare module 'graphql' {
interface GraphQLObjectTypeExtensions<TSource = any, TContext = any> {
joinMonster?: ObjectTypeExtension<TSource, TContext>
}
interface GraphQLFieldExtensions<
TSource,
TContext,
TArgs = { [argName: string]: any }
> {
joinMonster?: FieldConfigExtension<TSource, TContext, TArgs>
}
interface GraphQLUnionTypeExtensions {
joinMonster?: UnionTypeExtension
}
interface GraphQLInterfaceTypeExtensions {
joinMonster?: InterfaceTypeExtension
}
}

Expand Down

0 comments on commit 81fcac2

Please sign in to comment.