-
-
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.
Prevent naming clash on fields in joins. Automatically select pagination key fields. Fix #7
- Loading branch information
1 parent
55192c1
commit 46a2d29
Showing
8 changed files
with
180 additions
and
15 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import "objection" | ||
|
||
declare module "objection" { | ||
interface QueryBuilder<M extends Model, R = M[]> { | ||
// Not sure why it's missing in official typings | ||
tableRef(): string | ||
} | ||
} |
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,83 @@ | ||
import gql from "graphql-tag" | ||
import { Model } from "objection" | ||
import { | ||
CursorPaginator, | ||
GraphResolver, | ||
ModelResolver, | ||
} from "objection-graphql-resolver" | ||
import tap from "tap" | ||
|
||
import { Resolvers, setup } from "./setup" | ||
|
||
class BookModel extends Model { | ||
static tableName = "book" | ||
|
||
id?: number | ||
title?: string | ||
author?: string | ||
} | ||
|
||
const schema = gql` | ||
type Book { | ||
id: Int! | ||
title: String! | ||
author: String! | ||
} | ||
type BookPage { | ||
nodes: [Book!]! | ||
cursor: String | ||
} | ||
type Query { | ||
library: BookPage! | ||
} | ||
` | ||
|
||
const resolve_graph = GraphResolver({ | ||
Book: ModelResolver(BookModel), | ||
}) | ||
|
||
const resolvers: Resolvers = { | ||
Query: { | ||
library: (_parent, _args, ctx, info) => | ||
resolve_graph(ctx, info, BookModel.query(), { | ||
paginate: CursorPaginator({ take: 1, fields: ["author", "id"] }), | ||
}), | ||
}, | ||
} | ||
|
||
tap.test("auto select pagination key", async (tap) => { | ||
const { client, knex } = await setup(tap, { typeDefs: schema, resolvers }) | ||
|
||
await knex.schema.createTable("book", (book) => { | ||
book.increments("id").notNullable().primary() | ||
book.string("title").notNullable() | ||
book.string("author").notNullable() | ||
}) | ||
|
||
await BookModel.query().insertGraph([ | ||
{ title: "1984", author: "George Orwell" }, | ||
{ title: "Tom Sawyer", author: "Mark Twain" }, | ||
]) | ||
|
||
tap.strictSame( | ||
await client.request( | ||
gql` | ||
{ | ||
library { | ||
nodes { | ||
title | ||
} | ||
} | ||
} | ||
` | ||
), | ||
{ | ||
library: { | ||
nodes: [{ title: "1984" }], | ||
}, | ||
}, | ||
"pagination works without selecting author explicitly" | ||
) | ||
}) |
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,63 @@ | ||
import gql from "graphql-tag" | ||
import { Model } from "objection" | ||
import { GraphResolver, ModelResolver } from "objection-graphql-resolver" | ||
import tap from "tap" | ||
|
||
import { Resolvers, setup } from "./setup" | ||
|
||
class BookModel extends Model { | ||
static tableName = "book" | ||
|
||
id?: number | ||
|
||
get foo() { | ||
return "whatever" | ||
} | ||
} | ||
|
||
const schema = gql` | ||
type Book { | ||
id: Int! | ||
foo: String! | ||
} | ||
type Query { | ||
library: [Book!]! | ||
} | ||
` | ||
|
||
const resolve_graph = GraphResolver({ | ||
Book: ModelResolver(BookModel), | ||
}) | ||
|
||
const resolvers: Resolvers = { | ||
Query: { | ||
library: (_parent, _args, ctx, info) => | ||
resolve_graph(ctx, info, BookModel.query()), | ||
}, | ||
} | ||
|
||
tap.test("select virtual attribute only", async (tap) => { | ||
const { client, knex } = await setup(tap, { typeDefs: schema, resolvers }) | ||
|
||
await knex.schema.createTable("book", (book) => { | ||
book.integer("id").notNullable().primary() | ||
}) | ||
|
||
await BookModel.query().insert({ id: 1 }) | ||
|
||
tap.strictSame( | ||
await client.request( | ||
gql` | ||
{ | ||
library { | ||
foo | ||
} | ||
} | ||
` | ||
), | ||
{ | ||
library: [{ foo: "whatever" }], | ||
} | ||
) | ||
}) |