Skip to content

Commit

Permalink
refactor: rename fetchGraphQL to withGraphQL
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this synchronizes naming with objection
  • Loading branch information
IlyaSemenov committed Nov 12, 2020
1 parent 752d233 commit 697bbd9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Define resolver:
export const resolvers = {
Query: {
posts: (parent, args, ctx, info) => {
return PostModel.query().fetchGraphQL(info)
return PostModel.query().withGraphQL(info)
},
},
}
Expand Down Expand Up @@ -134,15 +134,15 @@ Resolver:

```ts
// for the query above, will pull posts with related author object
PostModel.query().fetchGraphQL(info)
PostModel.query().withGraphQL(info)
```

### Filters

Queries can be filtered like this:

```ts
PostModel.query().fetchGraphQL(info, {
PostModel.query().withGraphQL(info, {
filter: {
date: "2020-10-01",
// Only pull posts where author_id is 123 or 456.
Expand All @@ -169,7 +169,7 @@ and then in resolver:
export const resolvers = {
Query: {
posts: (parent, { filter }, ctx, info) => {
return PostModel.query().fetchGraphQL(info, { filter })
return PostModel.query().withGraphQL(info, { filter })
},
},
}
Expand All @@ -186,7 +186,7 @@ Supported operators:
You can filter nested relations with a nested filter:

```ts
UserModel.query().fetchGraphQL(info, {
UserModel.query().withGraphQL(info, {
filter: {
id: 123,
posts: {
Expand Down Expand Up @@ -216,7 +216,7 @@ export class PostModel extends Model {
then you can filter results with:

```ts
UserModel.query().fetchGraphQL(info, {
UserModel.query().withGraphQL(info, {
filter: {
public: true, // even though the actual value is ignored, sending true is a reasonable convention
search: "hello",
Expand All @@ -231,7 +231,7 @@ Modifier filters take precedence over raw field filters.
All models can be filtered using query-level modifiers:

```ts
PostModel.query().fetchGraphQL(info, {
PostModel.query().withGraphQL(info, {
modifiers: {
User: (query) => query.where("active", true),
},
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface FetchOptions {
modifiers?: Modifiers
}

QueryBuilder.prototype.fetchGraphQL = function <QB extends AnyQueryBuilder>(
QueryBuilder.prototype.withGraphQL = function <QB extends AnyQueryBuilder>(
this: QB,
info: GraphQLResolveInfo,
options?: FetchOptions,
Expand Down
6 changes: 3 additions & 3 deletions src/tests/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type Resolver<A> = (
export const resolvers: Record<string, Record<string, Resolver<any>>> = {
Query: {
user: (parent, { id }, ctx: unknown, info) => {
return UserModel.query().findById(id).fetchGraphQL(info)
return UserModel.query().findById(id).withGraphQL(info)
},
sections: async (parent, { filter }, ctx, info) => {
return SectionModel.query().fetchGraphQL(info, { filter })
return SectionModel.query().withGraphQL(info, { filter })
},
posts: async (parent, { filter }, ctx, info) => {
return PostModel.query().fetchGraphQL(info, { filter })
return PostModel.query().withGraphQL(info, { filter })
},
},
}
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export * from "."
declare module "objection" {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface QueryBuilder<M extends Model, R = M[]> {
fetchGraphQL(info: GraphQLResolveInfo, options?: FetchOptions): this
withGraphQL(info: GraphQLResolveInfo, options?: FetchOptions): this
}
}

0 comments on commit 697bbd9

Please sign in to comment.