This example shows how to implement a GraphQL server with Golang based on Prisma & gqlgen.
Clone the repository:
git clone [email protected]:prisma/prisma-examples.git
Ensure dependencies are available and up-to-date:
cd prisma-examples/go/graphql
dep ensure -update
To run the example, you need the Prisma CLI. Please install it via Homebrew or using another method:
brew install prisma
brew tap
For this example, you'll use a free demo database (AWS Aurora) hosted in Prisma Cloud. To set up your database, run:
prisma deploy
Then, follow these steps in the interactive CLI wizard:
- Select Demo server
- Authenticate with Prisma Cloud in your browser (if necessary)
- Back in your terminal, confirm all suggested values
Alternative: Run Prisma locally via Docker
- Ensure you have Docker installed on your machine. If not, you can get it from here.
- Create
docker-compose.yml
for MySQL (see here for Postgres):version: '3' services: prisma: image: prismagraphql/prisma:1.28 restart: always ports: - "4466:4466" environment: PRISMA_CONFIG: | port: 4466 databases: default: connector: mysql host: mysql port: 3306 user: root password: prisma migrations: true mysql: image: mysql:5.7 restart: always environment: MYSQL_ROOT_PASSWORD: prisma volumes: - mysql:/var/lib/mysql volumes: mysql:
- Run
docker-compose up -d
- Set the
endpoint
inprisma.yml
tohttp://localhost:4466
- Run
prisma deploy
go run ./server
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(
name: "Sarah"
email: "[email protected]"
) {
id
}
}
mutation {
createDraft(
title: "Join the Prisma Slack"
content: "https://slack.prisma.io"
authorEmail: "[email protected]"
) {
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 {
deletePost(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.
After you made changes to schema.graphql
, you need to update the generated types in ./server/generated.go
and potentially also adjust the resolver implementations in ./server/resolver.go
:
go run sripts/gqlgen.go
This updates ./server/generated.go
to incorporate the schema changes in your Go type definitions. It also generates scaffolded resolvers in tmp/resolver.go
that you might need to copy and paste into ./server/resolver.go
.