-
-
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.
orchid-graphql: type check for graph.resolve query
- Loading branch information
1 parent
9de87aa
commit 85f1e5b
Showing
9 changed files
with
157 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"orchid-graphql": minor | ||
--- | ||
|
||
Basic type check for `graph.resolve()` query argument. Use latest orchid-orm types. |
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,10 +1,40 @@ | ||
import { GraphResolver, GraphResolverOptions, TableResolver } from "graphql-orm" | ||
import { | ||
GraphResolveOptions, | ||
GraphResolver, | ||
GraphResolverOptions, | ||
TableResolver, | ||
} from "graphql-orm" | ||
import { Query } from "orchid-orm" | ||
|
||
import { OrchidOrm, orm } from "../orm/orm" | ||
|
||
export function createGraphResolver<Context = unknown>( | ||
types: Record<string, TableResolver<OrchidOrm, Context>>, | ||
options?: GraphResolverOptions<OrchidOrm, Context>, | ||
) { | ||
return new GraphResolver<OrchidOrm, Context>(orm, types, options) | ||
return new OrchidGraphResolver<Context>(types, options) | ||
} | ||
|
||
class OrchidGraphResolver<Context> extends GraphResolver<OrchidOrm, Context> { | ||
constructor( | ||
public readonly types: Record<string, TableResolver<OrchidOrm, Context>>, | ||
public readonly options: GraphResolverOptions<OrchidOrm, Context> = {}, | ||
) { | ||
super(orm, types, options) | ||
} | ||
|
||
resolve<T>( | ||
query: Query & { | ||
returnType: unknown extends T | ||
? any | ||
: T extends unknown[] | ||
? undefined | "all" | ||
: T extends undefined | ||
? "one" | ||
: "oneOrThrow" | ||
}, | ||
options: GraphResolveOptions<Context>, | ||
): Promise<T> { | ||
return super.resolve(query, options) | ||
} | ||
} |
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,11 +1,11 @@ | ||
import { TableResolver, TableResolverOptions } from "graphql-orm" | ||
import { DbTable, Table } from "orchid-orm" | ||
import { Table } from "orchid-orm" | ||
|
||
import { OrchidOrm, orm } from "../orm/orm" | ||
|
||
export function defineTableResolver<TableT extends Table, Context = unknown>( | ||
table: DbTable<TableT>, | ||
export function defineTableResolver<Context = unknown>( | ||
table: Table, | ||
options: TableResolverOptions<OrchidOrm, Context> = {}, | ||
) { | ||
return new TableResolver(orm, table as unknown as DbTable<any>, options) | ||
return new TableResolver(orm, table as any, options) | ||
} |
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 |
---|---|---|
|
@@ -5,5 +5,6 @@ | |
"orchid-graphql": ["./src"], | ||
"graphql-orm": ["../base/src"] | ||
} | ||
} | ||
}, | ||
"exclude": ["./typetests"] | ||
} |
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,66 @@ | ||
import * as r from "orchid-graphql" | ||
import { createBaseTable, orchidORM } from "orchid-orm" | ||
import { expect, test } from "tstyche" | ||
|
||
const BaseTable = createBaseTable() | ||
|
||
class PostTable extends BaseTable { | ||
readonly table = "post" | ||
|
||
columns = this.setColumns((t) => ({ | ||
id: t.serial().primaryKey(), | ||
text: t.text(), | ||
})) | ||
} | ||
|
||
const db = orchidORM( | ||
{}, | ||
{ | ||
post: PostTable, | ||
}, | ||
) | ||
|
||
// Similar to db.post, but not necessary so. | ||
interface Post { | ||
id: number | ||
text: number | ||
excerpt: string | ||
tags: string[] | ||
} | ||
|
||
const graph = r.graph({ | ||
Post: r.table(db.post), | ||
}) | ||
|
||
const ctx = 0 as any | ||
|
||
test("valid query type", () => { | ||
expect(graph.resolve(db.post, ctx)).type.toBe<Promise<unknown>>() | ||
expect(graph.resolve<Post[]>(db.post.all(), ctx)).type.toBe<Promise<Post[]>>() | ||
expect(graph.resolve<Post[]>(db.post.where({ text: "foo" }), ctx)).type.toBe< | ||
Promise<Post[]> | ||
>() | ||
expect(graph.resolve<Post>(db.post.find(1), ctx)).type.toBe<Promise<Post>>() | ||
expect( | ||
graph.resolve<Post | undefined>(db.post.findOptional(1), ctx), | ||
).type.toBe<Promise<Post | undefined>>() | ||
}) | ||
|
||
test("invalid query type", () => { | ||
expect(graph.resolve<Post>(db.post.all(), ctx)).type.toRaiseError() | ||
expect(graph.resolve<Post>(db.post.all(), ctx)).type.toRaiseError() | ||
expect(graph.resolve<Post>(db.post.count(), ctx)).type.toRaiseError() | ||
expect(graph.resolve<Post>(db.post.get("text"), ctx)).type.toRaiseError() | ||
expect(graph.resolve<Post>(db.post.findOptional(1), ctx)).type.toRaiseError() | ||
expect(graph.resolve<Post[]>(db.post.find(1), ctx)).type.toRaiseError() | ||
}) | ||
|
||
test("query type inference", () => { | ||
;((): Promise<Post> => { | ||
return graph.resolve(db.post.find(1), ctx) | ||
})() | ||
;((): Promise<Post[]> => { | ||
// @ts-expect-error find(1) query should not be allowed for Post[] return type | ||
return graph.resolve(db.post.find(1), ctx) | ||
})() | ||
}) |
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,5 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"include": ["./"], | ||
"exclude": [] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.