Skip to content

Commit

Permalink
Make query resolver arg callable
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Aug 2, 2024
1 parent 782146c commit c09b314
Show file tree
Hide file tree
Showing 18 changed files with 468 additions and 345 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-hotels-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pothos/plugin-prisma": minor
---

Make query argument of resolvers a callable function that merges provided include/select
41 changes: 24 additions & 17 deletions examples/complex-app/src/schema/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,25 @@ builder.queryFields((t) => ({
games: t.prismaConnection({
type: 'Game',
cursor: 'id',
resolve: (query) => db.game.findMany({ ...query, orderBy: { createdAt: 'desc' } }),
resolve: (query) =>
db.game.findMany(
query({
orderBy: { createdAt: 'desc' },
}),
),
}),
game: t.prismaField({
type: 'Game',
args: { id: t.arg.id({ required: true }) },
nullable: true,
resolve: (query, _, { id }) =>
db.game.findUnique({
...query,
where: {
id: parseID(id),
},
}),
db.game.findUnique(
query({
where: {
id: parseID(id),
},
}),
),
}),
}));

Expand All @@ -86,18 +92,19 @@ builder.mutationField('createGame', (t) =>
input: t.arg({ type: CreateGame, required: true }),
},
resolve: async (query, _, { input }) => {
const game = await db.game.create({
...query,
data: {
complete: input.complete ?? false,
opponentName: input.opponentName,
team: {
connect: {
id: parseID(input.teamId),
const game = await db.game.create(
query({
data: {
complete: input.complete ?? false,
opponentName: input.opponentName,
team: {
connect: {
id: parseID(input.teamId),
},
},
},
},
});
}),
);

return game;
},
Expand Down
81 changes: 43 additions & 38 deletions examples/complex-app/src/schema/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,25 @@ builder.prismaNode('Player', {
type: 'Game',
cursor: 'id',
resolve: (query, player) =>
db.game.findMany({
...query,
orderBy: {
createdAt: 'desc',
},
where: {
team: { id: player.teamId },
points: {
some: {
players: {
some: {
id: player.id,
db.game.findMany(
query({
orderBy: {
createdAt: 'desc',
},
where: {
team: { id: player.teamId },
points: {
some: {
players: {
some: {
id: player.id,
},
},
},
},
},
},
}),
}),
),
}),
}),
});
Expand Down Expand Up @@ -75,18 +76,19 @@ builder.mutationField('addPlayerToTeam', (t) =>
input: t.arg({ type: AddPlayerToTeamInput, required: true }),
},
resolve: (query, _, { input }) =>
db.team.update({
...query,
where: { id: parseID(input.teamId) },
data: {
players: {
create: {
name: input.player.name,
number: input.player.number,
db.team.update(
query({
where: { id: parseID(input.teamId) },
data: {
players: {
create: {
name: input.player.name,
number: input.player.number,
},
},
},
},
}),
}),
),
}),
);

Expand All @@ -105,19 +107,22 @@ builder.mutationField('addPlayersToTeam', (t) =>
input: t.arg({ type: AddPlayersToTeamInput, required: true }),
},
resolve: (query, _, { input }) =>
db.team.update({
...query,
where: {
id: parseID(input.teamId),
},
data: {
players: {
create: input.players.map((player) => ({
name: player.name,
number: player.number,
})),
},
},
}),
db.team.update(
query(
query({
where: {
id: parseID(input.teamId),
},
data: {
players: {
create: input.players.map((player) => ({
name: player.name,
number: player.number,
})),
},
},
}),
),
),
}),
);
31 changes: 16 additions & 15 deletions examples/complex-app/src/schema/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,23 @@ builder.mutationField('addPoint', (t) =>
resolve: async (query, _, { input }) => {
const game = await db.game.findUniqueOrThrow({ where: { id: parseID(input.gameId) } });

return db.point.create({
...query,
data: {
scored: input.scored,
startedOnOffense: input.startedOnOffense,
players: {
connect: input.playerIds.map((id) => ({ id: parseID(id) })),
return db.point.create(
query({
data: {
scored: input.scored,
startedOnOffense: input.startedOnOffense,
players: {
connect: input.playerIds.map((id) => ({ id: parseID(id) })),
},
game: {
connect: { id: game.id },
},
team: {
connect: { id: game.teamId },
},
},
game: {
connect: { id: game.id },
},
team: {
connect: { id: game.teamId },
},
},
});
}),
);
},
}),
);
42 changes: 22 additions & 20 deletions examples/complex-app/src/schema/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ builder.queryFields((t) => ({
teams: t.prismaConnection({
type: 'Team',
cursor: 'id',
resolve: (query) => db.team.findMany({ ...query, orderBy: { createdAt: 'desc' } }),
resolve: (query) => db.team.findMany(query({ orderBy: { createdAt: 'desc' } })),
}),
team: t.prismaField({
type: 'Team',
args: { id: t.arg.id({ required: true }) },
nullable: true,
resolve: (query, _, { id }) =>
db.team.findUnique({
...query,
where: {
id: parseID(id),
},
}),
db.team.findUnique(
query({
where: {
id: parseID(id),
},
}),
),
}),
}));

Expand All @@ -71,19 +72,20 @@ builder.mutationField('createTeam', (t) =>
input: t.arg({ type: CreateTeamInput, required: true }),
},
resolve: (query, _, { input }) =>
db.team.create({
...query,
data: {
name: input.name,
players: {
create: input.players
? input.players.map((player) => ({
name: player.name,
number: player.number,
}))
: [],
db.team.create(
query({
data: {
name: input.name,
players: {
create: input.players
? input.players.map((player) => ({
name: player.name,
number: player.number,
}))
: [],
},
},
},
}),
}),
),
}),
);
13 changes: 7 additions & 6 deletions examples/complex-app/src/schema/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ builder.queryField('viewer', (t) =>
return null;
}

return db.user.findUnique({
...query,
where: {
id: ctx.user.id,
},
});
return db.user.findUnique(
query({
where: {
id: ctx.user.id,
},
}),
);
},
}),
);
13 changes: 7 additions & 6 deletions examples/prisma-federation/src/comments/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const User = builder.externalRef('User', builder.selection<{ id: string }>('id')
resolve: (query, user) =>
db.user
.findUniqueOrThrow({ where: { id: Number.parseInt(user.id, 10) } })
.comments({ orderBy: { updatedAt: 'desc' }, ...query }),
.comments(query({ orderBy: { updatedAt: 'desc' } })),
}),
}),
});
Expand All @@ -43,7 +43,7 @@ const Post = builder.externalRef('Post', builder.selection<{ id: string }>('id')
resolve: (query, post) =>
db.post
.findUniqueOrThrow({ where: { id: Number.parseInt(post.id, 10) } })
.comments({ orderBy: { updatedAt: 'desc' }, ...query }),
.comments(query({ orderBy: { updatedAt: 'desc' } })),
}),
}),
});
Expand Down Expand Up @@ -76,10 +76,11 @@ builder.queryType({
id: t.arg.id({ required: true }),
},
resolve: (query, _root, { id }) =>
db.comment.findUniqueOrThrow({
...query,
where: { id: Number.parseInt(id, 10) },
}),
db.comment.findUniqueOrThrow(
query({
where: { id: Number.parseInt(id, 10) },
}),
),
}),
}),
});
Expand Down
27 changes: 13 additions & 14 deletions examples/prisma-federation/src/posts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const User = builder.externalRef('User', builder.selection<{ id: string }>('id')
resolve: (query, user) =>
db.user
.findUniqueOrThrow({ where: { id: Number.parseInt(user.id, 10) } })
.posts({ orderBy: { updatedAt: 'desc' }, ...query }),
.posts(query({ orderBy: { updatedAt: 'desc' } })),
}),
}),
});
Expand Down Expand Up @@ -68,10 +68,11 @@ builder.queryType({
id: t.arg.id({ required: true }),
},
resolve: (query, _root, args) =>
db.post.findUnique({
...query,
where: { id: Number.parseInt(args.id, 10) },
}),
db.post.findUnique(
query({
where: { id: Number.parseInt(args.id, 10) },
}),
),
}),
posts: t.prismaField({
type: ['Post'],
Expand All @@ -80,19 +81,17 @@ builder.queryType({
skip: t.arg.int(),
},
resolve: (query, _root, args) =>
db.post.findMany({
...query,
take: args.take ?? DEFAULT_PAGE_SIZE,
skip: args.skip ?? 0,
}),
db.post.findMany(
query({
take: args.take ?? DEFAULT_PAGE_SIZE,
skip: args.skip ?? 0,
}),
),
}),
postsConnection: t.prismaConnection({
type: 'Post',
cursor: 'id',
resolve: (query, _root, _args) =>
db.post.findMany({
...query,
}),
resolve: (query, _root, _args) => db.post.findMany(query({})),
}),
}),
});
Expand Down
Loading

0 comments on commit c09b314

Please sign in to comment.