-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate RelationResolver and FieldResolver
BREAKING CHANGE: FieldResolver no more accepts `filter` and `paginate`, use RelationResolver for relations.
- Loading branch information
1 parent
0e353c7
commit 8d74f04
Showing
9 changed files
with
146 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { FieldResolver } from "./resolver/field" | ||
export { ModelResolver } from "./resolver/model" | ||
export { GraphResolver } from "./resolver/graph" | ||
export { ModelResolver } from "./resolver/model" | ||
export { RelationResolver } from "./resolver/relation" | ||
export { CursorPaginator } from "./paginators/cursor" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Model } from "objection" | ||
|
||
import { FiltersDef } from "../filter" | ||
import { run_after } from "../helpers/run_after" | ||
import { PaginatorFn } from "../paginators" | ||
import { FieldResolver, FieldResolverOptions } from "./field" | ||
import { Modifier } from "./model" | ||
|
||
export interface RelationResolverOptions<M extends Model, R extends Model> | ||
extends Exclude<FieldResolverOptions<M>, "select"> { | ||
filter?: FiltersDef | ||
paginate?: PaginatorFn<R> | ||
modifier?: Modifier<R> | ||
} | ||
|
||
export function RelationResolver<M extends Model, R extends Model>( | ||
options?: RelationResolverOptions<M, R>, | ||
) { | ||
const filter = options?.filter | ||
const paginate = options?.paginate | ||
const modifier = options?.modifier | ||
|
||
return FieldResolver<M>({ | ||
select(query, { field, tree, resolve_tree }) { | ||
// withGraphFetched will disregard paginator's runAfter callback (which converts object list into cursor and nodes) | ||
// Save it locally and then re-inject | ||
let paginated_results: any | ||
|
||
query | ||
.withGraphFetched( | ||
`${options?.modelField || field}(${field}) as ${field}`, | ||
paginate | ||
? { | ||
joinOperation: "leftJoin", // Remove after https://github.com/Vincit/objection.js/issues/1954 is fixed | ||
maxBatchSize: 1, | ||
} | ||
: undefined, | ||
) | ||
.modifiers({ | ||
[field]: (subquery) => { | ||
resolve_tree({ | ||
tree, | ||
query: subquery, | ||
filter, | ||
paginate, | ||
}) | ||
if (paginate) { | ||
subquery.runAfter((results) => { | ||
// Save paginated results | ||
paginated_results = results | ||
}) | ||
} | ||
if (modifier) { | ||
modifier(subquery) | ||
} | ||
}, | ||
}) | ||
|
||
if (paginate) { | ||
query.runAfter( | ||
// Re-inject paginated results | ||
// They have been overwritten by objection.js by now | ||
run_after((instance) => { | ||
instance[field] = paginated_results | ||
return instance | ||
}), | ||
) | ||
} | ||
}, | ||
}) | ||
} |
Oops, something went wrong.