-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac2967d
commit 2b5ebfb
Showing
18 changed files
with
363 additions
and
133 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
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,16 @@ | ||
import { defineConfig } from '@eddeee888/gcg-typescript-resolver-files'; | ||
import type { CodegenConfig } from '@graphql-codegen/cli'; | ||
|
||
const config: CodegenConfig = { | ||
schema: 'src/schema/**/schema.graphql', | ||
generates: { | ||
'src/schema': defineConfig({ | ||
resolverGeneration: 'minimal', | ||
typesPluginsConfig: { | ||
contextType: '../context#GraphQLContext', | ||
}, | ||
}), | ||
}, | ||
hooks: { afterAllFileWrite: ['prettier --write'] }, | ||
}; | ||
export default config; |
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,15 @@ | ||
import type { CommentResolvers } from '../../types.generated'; | ||
|
||
export const Comment: CommentResolvers = { | ||
link(parent, _arg, context) { | ||
if (!parent.linkId) { | ||
return null; | ||
} | ||
|
||
return context.prisma.link.findUnique({ | ||
where: { | ||
id: parent.linkId, | ||
}, | ||
}); | ||
}, | ||
}; |
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,11 @@ | ||
import type { LinkResolvers } from '../../types.generated'; | ||
|
||
export const Link: LinkResolvers = { | ||
comments: async (parent, _arg, context) => { | ||
return context.prisma.comment.findMany({ | ||
where: { | ||
linkId: parent.id, | ||
}, | ||
}); | ||
}, | ||
}; |
39 changes: 39 additions & 0 deletions
39
examples/tutorial/src/schema/base/resolvers/Mutation/postCommentOnLink.ts
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,39 @@ | ||
import { GraphQLError } from 'graphql'; | ||
import { Prisma } from '@prisma/client'; | ||
import { parseIntSafe } from '../../../../utils'; | ||
import type { MutationResolvers } from '../../../types.generated'; | ||
|
||
export const postCommentOnLink: NonNullable<MutationResolvers['postCommentOnLink']> = async ( | ||
_parent, | ||
args, | ||
context, | ||
) => { | ||
const linkId = parseIntSafe(args.linkId); | ||
if (linkId === null) { | ||
return Promise.reject( | ||
new GraphQLError(`Cannot post comment on non-existing link with id '${args.linkId}'.`), | ||
); | ||
} | ||
|
||
if (!args.body || args.body.trim().length === 0) { | ||
return Promise.reject(new GraphQLError(`Comment body cannot be empty.`)); | ||
} | ||
|
||
const newComment = await context.prisma.comment | ||
.create({ | ||
data: { | ||
linkId, | ||
body: args.body, | ||
}, | ||
}) | ||
.catch((err: unknown) => { | ||
if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === 'P2003') { | ||
return Promise.reject( | ||
new GraphQLError(`Cannot post comment on non-existing link with id '${args.linkId}'.`), | ||
); | ||
} | ||
return Promise.reject(err); | ||
}); | ||
|
||
return newComment; | ||
}; |
15 changes: 15 additions & 0 deletions
15
examples/tutorial/src/schema/base/resolvers/Mutation/postLink.ts
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,15 @@ | ||
import type { MutationResolvers } from '../../../types.generated'; | ||
|
||
export const postLink: NonNullable<MutationResolvers['postLink']> = async ( | ||
_parent, | ||
args, | ||
context, | ||
) => { | ||
const newLink = await context.prisma.link.create({ | ||
data: { | ||
url: args.url, | ||
description: args.description, | ||
}, | ||
}); | ||
return newLink; | ||
}; |
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,7 @@ | ||
import type { QueryResolvers } from '../../../types.generated'; | ||
|
||
export const comment: NonNullable<QueryResolvers['comment']> = async (_parent, args, context) => { | ||
return context.prisma.comment.findUnique({ | ||
where: { id: parseInt(args.id) }, | ||
}); | ||
}; |
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,27 @@ | ||
import { applySkipConstraints, applyTakeConstraints } from '../../../../utils'; | ||
import type { QueryResolvers } from '../../../types.generated'; | ||
|
||
export const feed: NonNullable<QueryResolvers['feed']> = async (_parent, args, context) => { | ||
const where = args.filterNeedle | ||
? { | ||
OR: [ | ||
{ description: { contains: args.filterNeedle } }, | ||
{ url: { contains: args.filterNeedle } }, | ||
], | ||
} | ||
: {}; | ||
|
||
const take = applyTakeConstraints({ | ||
min: 1, | ||
max: 50, | ||
value: args.take ?? 30, | ||
}); | ||
|
||
const skip = applySkipConstraints(args.skip ?? 0); | ||
|
||
return context.prisma.link.findMany({ | ||
where, | ||
skip, | ||
take, | ||
}); | ||
}; |
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 @@ | ||
import type { QueryResolvers } from '../../../types.generated'; | ||
|
||
export const info: NonNullable<QueryResolvers['info']> = async (_parent, _arg, _ctx) => { | ||
return `This is the API of a Hackernews Clone`; | ||
}; |
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,7 @@ | ||
import type { QueryResolvers } from '../../../types.generated'; | ||
|
||
export const link: NonNullable<QueryResolvers['link']> = async (_parent, args, context) => { | ||
return context.prisma.link.findUnique({ | ||
where: { id: parseInt(args.id) }, | ||
}); | ||
}; |
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,24 @@ | ||
type Query { | ||
info: String! | ||
feed(filterNeedle: String, skip: Int, take: Int): [Link!]! | ||
comment(id: ID!): Comment | ||
link(id: ID!): Link | ||
} | ||
|
||
type Mutation { | ||
postLink(url: String!, description: String!): Link! | ||
postCommentOnLink(linkId: ID!, body: String!): Comment! | ||
} | ||
|
||
type Link { | ||
id: ID! | ||
description: String! | ||
url: String! | ||
comments: [Comment!]! | ||
} | ||
|
||
type Comment { | ||
id: ID! | ||
body: String! | ||
link: Link | ||
} |
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,4 @@ | ||
import type { Comment, Link } from '@prisma/client'; | ||
|
||
export type LinkMapper = Link; | ||
export type CommentMapper = Comment; |
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,24 @@ | ||
import { GraphQLError } from 'graphql'; | ||
|
||
export const parseIntSafe = (value: string): number | null => { | ||
if (/^(\d+)$/.test(value)) { | ||
return parseInt(value, 10); | ||
} | ||
return null; | ||
}; | ||
|
||
export const applySkipConstraints = (value: number) => { | ||
if (value < 0) { | ||
throw new GraphQLError(`'skip' argument value '${value}' is invalid, value must be positive.`); | ||
} | ||
return value; | ||
}; | ||
|
||
export const applyTakeConstraints = (params: { min: number; max: number; value: number }) => { | ||
if (params.value < params.min || params.value > params.max) { | ||
throw new GraphQLError( | ||
`'take' argument value '${params.value}' is outside the valid range of '${params.min}' to '${params.max}'.`, | ||
); | ||
} | ||
return params.value; | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.