Skip to content

Commit

Permalink
[drizzle] Fix drizzleFieldWithInput
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Sep 9, 2024
1 parent 2f2de56 commit fa2429f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-ties-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pothos/plugin-drizzle": patch
---

Fix drizzleFieldWithInput
21 changes: 14 additions & 7 deletions packages/plugin-drizzle/src/field-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getSchemaConfig } from './utils/config';
import { resolveDrizzleCursorConnection } from './utils/cursors';
import { queryFromInfo } from './utils/map-query';
import { getRefFromModel } from './utils/refs';
import type { SelectionMap } from './utils/selections';

const fieldBuilderProto = RootFieldBuilder.prototype as PothosSchemaTypes.RootFieldBuilder<
SchemaTypes,
Expand Down Expand Up @@ -69,16 +70,22 @@ fieldBuilderProto.drizzleFieldWithInput = function drizzleFieldWithInput(
...(options as {}),
type: typeParam,
resolve: (parent: unknown, args: unknown, context: {}, info: GraphQLResolveInfo) => {
const query = queryFromInfo({
config: getSchemaConfig(this.builder),
return resolve(
(select: SelectionMap) =>
queryFromInfo({
config: getSchemaConfig(this.builder),
context,
select,
info,
// withUsageCheck: !!this.builder.options.drizzle?.onUnusedQuery,
}),
parent,
args as never,
context,
info,
// withUsageCheck: !!this.builder.options.drizzle?.onUnusedQuery,
});

return resolve(query, parent, args as never, context, info) as never;
) as never;
},
});
}) as never;
} as never;

fieldBuilderProto.drizzleConnection = function drizzleConnection<
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-drizzle/src/global-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,6 @@ declare global {

drizzleFieldWithInput: 'withInput' extends PluginName
? <
Fields extends InputFieldMap,
Args extends InputFieldMap,
Param extends
| keyof Types['DrizzleRelationSchema']
| [keyof Types['DrizzleRelationSchema']]
Expand All @@ -407,6 +405,8 @@ declare global {
ResolveShape,
ResolveReturnShape,
ArgRequired extends boolean,
Fields extends InputFieldMap = {},
Args extends InputFieldMap = {},
InputName extends string = 'input',
Table extends
keyof Types['DrizzleRelationSchema'] = Param extends keyof Types['DrizzleRelationSchema']
Expand Down
13 changes: 1 addition & 12 deletions packages/plugin-drizzle/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,7 @@ export type DrizzleFieldWithInputOptions<
>,
'args'
> &
PothosSchemaTypes.FieldWithInputBaseOptions<
Types,
Args & {
[K in InputName]: ArgumentRef<
Types,
InputShapeFromFields<Fields> | (true extends ArgRequired ? never : null | undefined)
>;
},
Fields,
InputName,
ArgRequired
>;
PothosSchemaTypes.FieldWithInputBaseOptions<Types, Args, Fields, InputName, ArgRequired>;

export type DrizzleObjectFieldOptions<
Types extends SchemaTypes,
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-drizzle/tests/example/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Query {
post(id: ID!): Post
posts(after: String, before: String, category: String, first: Int, last: Int): QueryPostsConnection
user(id: ID!): User
userWithInput(input: QueryUserWithInputInput!): User
}

type QueryPostsConnection {
Expand All @@ -76,6 +77,10 @@ type QueryPostsConnectionEdge {
node: Post
}

input QueryUserWithInputInput {
id: ID!
}

type User implements Node {
bio: String
email: String
Expand Down
13 changes: 13 additions & 0 deletions packages/plugin-drizzle/tests/example/schema/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,18 @@ builder.queryType({
);
},
}),
userWithInput: t.drizzleFieldWithInput({
type: 'users',
input: {
id: t.input.globalID({ required: true, for: User }),
},
resolve: (query, _root, { input: { id } }) => {
return db.query.users.findFirst(
query({
where: (user, { eq }) => eq(user.id, id.id.id),
}),
);
},
}),
}),
});

0 comments on commit fa2429f

Please sign in to comment.