SEE Live preview https://nextjs-hulu-xd.vercel.app
click on Start free trial go to to home page
This example shows how to implement a fullstack app in TypeScript with Next.js using React, Apollo Client (frontend), Nexus Schema and Prisma Client (backend). It uses a mongodb database
Clone this repository:
git clone https://github.com/zax4r0/NEXTJS_HULU_XD.git --depth=1
Install npm dependencies:
cd NEXTJS_HULU_XD
npm install
If you have a look inside, you'll see a .env
file and a schema.prisma
file. The schema.prisma
file will hold the database model and the .env.local
file will hold the database connection string.
Because the database connection string contains sensitive information, it's a good practice to never commit this .env
file with Git, so make sure it's also added to the .gitignore
file.
Run the following command to create your SQLite database file. This also creates the User
and Post
tables that are defined in prisma/schema.prisma
:
Im using mongodb yes with prisma its a preview feature check the prisma docs for more infoooooo
add .env.local make sure u add ur databaseurl in .env
cp .env.sample .env.local
npx prisma genaerate
Now, seed the database with the sample data in prisma/seed.ts
by running the following command:
npx prisma db seed
npm run dev
The app is now running, navigate to http://localhost:3000/
in your browser to explore its UI.
You can also access the GraphQL API of the API server directly. It is running on the same host machine and port and can be accessed via the /api
route (in this case that is localhost:3000/api
).
Below are a number of operations that you can send to the API.
query {
feed {
id
title
content
Image
published
author {
id
name
email
}
}
}
mutation {
signupUser(name: "Sarah", email: "sarah@prisma.io") {
id
}
}
mutation {
createDraft(
title: "Join the Prisma Slack"
content: "https://slack.prisma.io"
authorEmail: "alice@prisma.io"
Image: "https://images.wallpapersden.com/image/download/poster-of-avengers-endgame-movie_a2htaGWUmZqaraWkpJRmbmdlrWZlbWU.jpg"
) {
id
published
}
}
mutation {
publish(postId: "__POST_ID__") {
id
published
}
}
Note: You need to replace the
__POST_ID__
-placeholder with an actualid
from aPost
item. You can find one e.g. using thefilterPosts
-query.
{
filterPosts(searchString: "graphql") {
id
title
content
Image
published
author {
id
name
email
}
}
}
{
post(postId: "__POST_ID__") {
id
title
content
Image
published
author {
id
name
email
}
}
}
Note: You need to replace the
__POST_ID__
-placeholder with an actualid
from aPost
item. You can find one e.g. using thefilterPosts
-query.
mutation {
deletePost(postId: "__POST_ID__") {
id
}
}
Note: You need to replace the
__POST_ID__
-placeholder with an actualid
from aPost
item. You can find one e.g. using thefilterPosts
-query.
Instead of manually defining all the types we expect to receive via GraphQL, we can also use a very cool package GraphQL Code Generator to generate types directly from the Nexus GraphQL endpoint. This way, you essentially only have to define the types once in the schema.prisma
file as the single-source-of-truth, then all types you'll use in the application can be derived from that schema with little manual effort! 🎉
First, copy and refactor the GraphQL queries from the TSX files into the graphql
directory. With the example from Step 8, create a new file at /graphql/queries.graphql.ts
and copy the query from /components/AllUsers.tsx
:
import gql from "graphql-tag"
export const AllUsersQuery = gql`
query AllUsers {
allUsers {
id
name
}
}
`
Separating GraphQL operations from components makes it easier to navigate the codebase.
Next, install the packages needed by graphql-code-generator
as dev dependencies:
npm install -D \
@graphql-codegen/cli \
@graphql-codegen/typescript \
@graphql-codegen/typescript-operations \
@graphql-codegen/typescript-urql
Then, create a codegen.yml
file in the project root with the following content:
overwrite: true
schema: "http://localhost:3000/api/graphql" # GraphQL endpoint via the nexus dev server
documents: "graphql/**/*.graphql.ts" # parse graphql operations in matching files
generates:
generated/graphql.tsx: # location for generated types, hooks and components
plugins:
- "typescript"
- "typescript-operations"
- "typescript-urql"
config:
withComponent: false # we'll use Urql client with hooks instead
withHooks: true
The configs above will tell graphql-code-generator
to pull the GraphQL schema from http://localhost:3000/api/graphql
, then generate types, Urql useQuery
hooks into a file located at /generated/graphql.tsx
.
Cool, let the code generation begin (in watch mode)!
npx graphql-codegen --watch
You will see some nice, written-by-a-robot code in /generated/graphql.tsx
. How neat!
now u can use the usequery hook in react
Once you have added a new query or mutation to the API, you can start building a new UI component in React. It could e.g. be called profile.tsx
and would be located in the pages
directory.
In the application code, you can access the new operations via Apollo Client and populate the UI with the data you receive from the API calls.
- Check out the Prisma docs
- Share your feedback in the
prisma2
channel on the Prisma Slack - Create issues and ask questions on GitHub
- Watch our biweekly "What's new in Prisma" livestreams on Youtube