-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
4 changed files
with
251 additions
and
24 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
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: Number | ||
updatedAt: Number | ||
} | ||
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 }; |
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
Oops, something went wrong.