Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial example #3452

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ packages/graphql-yoga/src/landing-page-html.ts
packages/graphql-yoga/src/graphiql-html.ts
.tool-versions
**/generated/**
**/*.generated.*
examples/apollo-federation-compatibility/src/resolvers-types.ts
**/node_modules
**/dist
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ packages/graphql-yoga/src/landing-page-html.ts
packages/render-graphiql/src/graphiql.ts
.changeset/
examples/apollo-federation-compatibility/src/resolvers-types.ts
examples/hackernews/**/*.generated.*
out/

# Since prettier doesn't support MDX2 he breaks formatting for the following
Expand Down
2 changes: 2 additions & 0 deletions examples/hackernews/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules
.env
prisma/migrations/migration_lock.toml
prisma/*.db*
# Ignore codegen auto-generated files
*.generated.*
16 changes: 16 additions & 0 deletions examples/hackernews/codegen.ts
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;
8 changes: 6 additions & 2 deletions examples/hackernews/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@
"keywords": [],
"scripts": {
"check": "tsc --pretty --noEmit",
"codegen": "graphql-codegen",
"dev": "cross-env NODE_ENV=development ts-node-dev --exit-child --respawn src/main.ts",
"migrate": "prisma migrate dev",
"postinstall": "prisma generate",
"postinstall": "pnpm run prisma:generate && pnpm run codegen",
"precheck": "pnpm run codegen",
"prisma:generate": "prisma generate",
"start": "ts-node src/main.ts"
},
"dependencies": {
"graphql": "16.6.0",
"graphql-yoga": "workspace:*"
},
"devDependencies": {
"@eddeee888/gcg-typescript-resolver-files": "0.10.4",
"@graphql-codegen/cli": "5.0.2",
"@prisma/client": "5.13.0",
"@prisma/internals": "5.13.0",
"@prisma/migrate": "5.13.0",
"@types/node": "22.9.0",
"cross-env": "7.0.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ CREATE TABLE "Comment" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"body" TEXT NOT NULL,
"linkId" INTEGER,
CONSTRAINT "Comment_linkId_fkey" FOREIGN KEY ("linkId") REFERENCES "Link" ("id") ON DELETE SET NULL ON UPDATE CASCADE
"linkId" INTEGER NOT NULL,
CONSTRAINT "Comment_linkId_fkey" FOREIGN KEY ("linkId") REFERENCES "Link" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
4 changes: 2 additions & 2 deletions examples/hackernews/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ model Comment {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
body String
link Link? @relation(fields: [linkId], references: [id])
linkId Int?
link Link @relation(fields: [linkId], references: [id])
linkId Int
}
144 changes: 4 additions & 140 deletions examples/hackernews/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,144 +1,8 @@
import { createGraphQLError, createSchema } from 'graphql-yoga';
import { Prisma, type Link } from '@prisma/client';
import type { GraphQLContext } from './context';

const typeDefinitions = /* GraphQL */ `
type Link {
id: ID!
description: String!
url: String!
comments: [Comment!]!
}

type Comment {
id: ID!
body: String!
}

type Query {
hello: String!
feed(filterNeedle: String, skip: Int, take: Int): [Link!]!
comment(id: ID!): Comment
}

type Mutation {
postLink(url: String!, description: String!): Link!
postCommentOnLink(linkId: ID!, body: String!): Comment!
}
`;

const parseIntSafe = (value: string): number | null => {
if (/^(\d+)$/.test(value)) {
return parseInt(value, 10);
}
return null;
};

const applyTakeConstraints = (params: { min: number; max: number; value: number }) => {
if (params.value < params.min || params.value > params.max) {
throw createGraphQLError(
`'take' argument value '${params.value}' is outside the valid range of '${params.min}' to '${params.max}'.`,
);
}
return params.value;
};

const resolvers = {
Query: {
hello: () => `Hello World!`,
feed: async (
parent: unknown,
args: { filterNeedle?: string; skip?: number; take?: number },
context: GraphQLContext,
) => {
const where = args.filterNeedle
? {
OR: [
{ description: { contains: args.filterNeedle } },
{ url: { contains: args.filterNeedle } },
],
}
: {};

const take = applyTakeConstraints({
min: 1,
max: 50,
value: args.take ?? 30,
});

return context.prisma.link.findMany({
where,
skip: args.skip,
take,
});
},
comment: async (parent: unknown, args: { id: string }, context: GraphQLContext) => {
return context.prisma.comment.findUnique({
where: { id: parseInt(args.id) },
});
},
},
Link: {
id: (parent: Link) => parent.id,
description: (parent: Link) => parent.description,
url: (parent: Link) => parent.url,
comments: (parent: Link, _: unknown, context: GraphQLContext) => {
return context.prisma.comment.findMany({
where: {
linkId: parent.id,
},
});
},
},
Mutation: {
postLink: async (
parent: unknown,
args: { description: string; url: string },
context: GraphQLContext,
) => {
const newLink = await context.prisma.link.create({
data: {
url: args.url,
description: args.description,
},
});
return newLink;
},
postCommentOnLink: async (
parent: unknown,
args: { linkId: string; body: string },
context: GraphQLContext,
) => {
const linkId = parseIntSafe(args.linkId);
if (linkId === null) {
return Promise.reject(
createGraphQLError(`Cannot post common on non-existing link with id '${args.linkId}'.`),
);
}

const comment = await context.prisma.comment
.create({
data: {
body: args.body,
linkId,
},
})
.catch((err: unknown) => {
if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === 'P2003') {
return Promise.reject(
createGraphQLError(
`Cannot post common on non-existing link with id '${args.linkId}'.`,
),
);
}
return Promise.reject(err);
});
return comment;
},
},
};
import { createSchema } from 'graphql-yoga';
import { resolvers } from './schema/resolvers.generated';
import { typeDefs } from './schema/typeDefs.generated';

export const schema = createSchema({
resolvers: [resolvers],
typeDefs: [typeDefinitions],
typeDefs: [typeDefs],
});
15 changes: 15 additions & 0 deletions examples/hackernews/src/schema/base/resolvers/Comment.ts
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,
},
});
},
};
11 changes: 11 additions & 0 deletions examples/hackernews/src/schema/base/resolvers/Link.ts
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,
},
});
},
};
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 examples/hackernews/src/schema/base/resolvers/Mutation/postLink.ts
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;
};
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) },
});
};
27 changes: 27 additions & 0 deletions examples/hackernews/src/schema/base/resolvers/Query/feed.ts
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,
});
};
5 changes: 5 additions & 0 deletions examples/hackernews/src/schema/base/resolvers/Query/info.ts
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`;
};
7 changes: 7 additions & 0 deletions examples/hackernews/src/schema/base/resolvers/Query/link.ts
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) },
});
};
24 changes: 24 additions & 0 deletions examples/hackernews/src/schema/base/schema.graphql
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
}
4 changes: 4 additions & 0 deletions examples/hackernews/src/schema/base/schema.mappers.ts
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;
Loading
Loading