Skip to content

Commit

Permalink
boilerplate for a graphql server
Browse files Browse the repository at this point in the history
  • Loading branch information
setsun committed Jul 12, 2023
1 parent b9b5fc2 commit f22214d
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 24 deletions.
2 changes: 1 addition & 1 deletion apps/portfolio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"classnames": "^2.3.2",
"database": "workspace:*",
"graphql": "^16.7.1",
"graphql-request": "^6.1.0",
"graphql-yoga": "^4.0.3",
"lodash.clamp": "^4.0.3",
"next": "^13.4.9",
"react": "^18.2.0",
Expand Down
65 changes: 65 additions & 0 deletions apps/portfolio/src/app/api/graphql/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { client } from "database";
import { NextResponse } from "next/server";
import { createYoga, createSchema } from "graphql-yoga";

const typeDefs = /* GraphQL */ `
type User {
id: String!
name: String
email: String
createdAt: Int
updatedAt: Int
}
type Post {
id: String!
title: String!
content: String
published: Boolean!
author: User
authorId: String
}
type Query {
listPosts: [Post!]!
getPostById(id: String): Post
}
`;

const resolvers = {
Query: {
listPosts: async () => {
return await client.post.findMany({
where: { published: true },
include: {
author: true,
},
});
},
getPostById: async (_parent, args: { id: string }) => {
return await client.post.findUnique({
where: {
id: args.id,
},
include: {
author: true,
},
});
},
},
};

const { handleRequest } = createYoga({
schema: createSchema({
typeDefs,
resolvers,
}),

// While using Next.js file convention for routing, we need to configure Yoga to use the correct endpoint
graphqlEndpoint: "/api/graphql",

// Yoga needs to know how to create a valid Next response
fetchAPI: { Response: NextResponse },
});

export { handleRequest as GET, handleRequest as POST };
5 changes: 1 addition & 4 deletions packages/scripts/src/generate-previews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ async function generatePreviews() {
// Set screen size
await page.setViewport({ width: 1200, height: 700 });

// Configure the navigation timeout
await page.setDefaultNavigationTimeout(0);

for (let pageNumber of PAGE_NUMBERS) {
await page.goto(`https://setsun.xyz/visualizers/${pageNumber}`, {
waitUntil: "networkidle0",
Expand All @@ -56,4 +53,4 @@ async function generatePreviews() {
// run logic
(async () => {
await generatePreviews();
})();
})();
Loading

0 comments on commit f22214d

Please sign in to comment.