This is based on an example, with the directory structure having changed.
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, which can easily change.
Download this example:
git clone https://github.com/themetalfleece/nextjs-prisma-apollo-example.git
Or, create a repo by pressing Use this template
at GitHub.
Install yarn:
npm i -g yarn
Install npm dependencies:
cd nextjs-prisma-apollo-example
yarn
Run the following to generate the types, schema and model.
You need to run this every time you make changes to .prisma
file or to graphql
files, as it generates the schema.prisma
, nexus-typegen.ts
and schema.graphql
files which are used by the app.
yarn generate
Copy the .env.example
file to a .env
file next to it.
Edit it to use your own mongodb connection string. If you need a free-to-start managed database, you can also use MongoDB Atlas.
You can replace mongo with any prisma supported database.
yarn dev
The app is now running, navigate to http://localhost:3000/
in your browser to explore its UI.
- Remove the
.github/workflows/upgrade-dependencies.yml files
, since it contains the workflow to upgrade all dependencies on a daily basis.- n case you would like to keep it, remove lines 7, 30, 32 from it. Also, change the git user name in line 28.
- Edit the name & version in the
package.json
file. - Edit the code in
src
, apart from thesrc/prisma
directory.
- Create the corresponding
.model.prisma
file and the.objectType.api.ts
file - Run
yarn generate
- Create the
.sourceType.ts
file which re-exports the prisma type - Add the
objectType
tographql/types/types.sourceType.ts
- Modify the corresponding
.model.prisma
file and the.objectType.api.ts
file - Run
yarn generate
- Create
.query.api.ts
or.mutation.api.ts
file - Add the definition to
graphql/queries/queries.sourceType.ts
orgraphql/mutations/mutations.sourceType.ts
- Run
yarn generate
- Create
.query.ts
or.mutation.ts
file
- Modify the
.query.api.ts
and.query.ts
files, or.mutation.api.ts
and.mutation.ts
files - Run
yarn generate
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
published
author {
id
name
email
}
}
}
See more API operations
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"
) {
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
published
author {
id
name
email
}
}
}
{
post(postId: "__POST_ID__") {
id
title
content
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.
- 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