This example shows how to implement a GraphQL server with TypeScript based on Photon.js, graphql-yoga and GraphQL Nexus.
Clone the prisma2
branch of this repository:
git clone --single-branch --branch prisma2 git@github.com:prisma/prisma-examples.git
Install Node dependencies:
cd prisma-examples/typescript/graphql
npm install
To run the example, you need the Prisma 2 CLI:
npm install -g prisma2
For this example, you'll use a simple SQLite database. To set up your database, run:
prisma2 lift save --name 'init'
prisma2 lift up
You can now use the SQLite Browser to view and edit your data in the ./prisma/dev.db
file that was created when you ran prisma2 lift up
.
Alternative: Connect to your own database
Prisma supports MySQL and PostgreSQL at the moment. If you would like to connect to your own database, you can do so by specifying a different data source in the Prisma schema file.
For a MySQL provider:
datasource mysql {
provider = "mysql"
url = "mysql://johndoe:secret42@localhost:3306/mydatabase"
}
OR
For a PostgreSQL provider:
datasource postgresql {
provider = "postgresql"
url = "postgresql://johndoe:secret42@localhost:5432/mydatabase?schema=public"
}
Note: In the above example connection strings,
johndoe
would be the username to your database,secret42
the password,mydatabase
the name of your database, andpublic
the PostgreSQL schema.
Then to migrate your database, run:
prisma2 lift save --name 'init'
prisma2 lift up
Run the following command to generate Photon.js:
prisma2 generate
Now you can seed your database using the seed
script from package.json
:
npm run seed
Launch your GraphQL server with this command:
npm run start
Navigate to http://localhost:4000 in your browser to explore the API of your GraphQL server in a GraphQL Playground.
The schema that specifies the API operations of your GraphQL server is defined in ./src/schema.graphql
. Below are a number of operations that you can send to the API using the GraphQL Playground.
Feel free to adjust any operation by adding or removing fields. The GraphQL Playground helps you with its auto-completion and query validation features.
query {
feed {
id
title
content
published
author {
id
name
email
}
}
}
See more API operations
mutation {
signupUser(
data: {
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(id: "__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(id: "__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 {
deleteOnePost(where: {id: "__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.
To make changes to the GraphQL schema, you need to manipulate the Query
and Mutation
types that are defined in index.ts
.
Note that the start
script also starts a development server that automatically updates your schema every time you save a file. This way, the auto-generated GraphQL schema updates whenever you make changes in to the Query
or Mutation
types inside your TypeScript code.
- Read the Prisma 2 announcement
- Check out the Prisma 2 docs
- Share your feedback in the
prisma2-preview
channel on the Prisma Slack