GraphQL Server Example with NestJS (SDL-first)
This example shows how to implement an GraphQL server (SDL-first) with TypeScript based on Prisma Client, NestJS. It is based on a PostgreSQL database, you can run the database with Docker. The example was bootstrapped using the NestJS CLI command nest new nestjs-graphql-example
.
Getting started
1. Download example and install dependencies
Download this example:
https://github.com/woochanleee/nestjs-graphql-example/archive/main.zip
Install yarn dependencies:
cd nestjs-graphql-example
yarn install
Alternative: Clone the entire repo
Clone this repository:
git clone git@github.com:woochanleee/nestjs-graphql-example.git --depth=1
Install yarn dependencies:
cd nestjs-graphql-example
yarn install
2. Start the PostgreSQL database server
Launch your PostgreSQL database server with this command:
yarn db:start
And setting prisma client and insert dummy data.
yarn db:setting
If you got error like this:
Error: P1001: Can't reach database server at
localhost:
5432``
You just run previous command one more time. Because Docker's execution is not over yet.
3. Start the GraphQL server
Launch your GraphQL server with this command:
yarn dev
Navigate to http://localhost:3005/graphql in your browser to explore the API of your GraphQL server in a GraphQL Playground.
Using the GraphQL API
The schema that specifies the API operations of your GraphQL server is defined in ./src/graphql/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.
Login and header setting
mutation {
login(input: {
email: "test",
password: "test"
}) {
ok
error
accessToken
refreshToken
}
}
Bottom left, setting http headers(Authorization Bearer).
See more API operations
Create a new user
mutation {
createUser(
input: {
email: "email"
password: "password"
name: "optional not required"
}
) {
ok
error
}
}
Edit a user profile
mutation {
editProfile(input: { name: "optional", password: "also optional" }) {
ok
error
}
}
Refresh token
mutation {
refreshToken {
ok
error
accessToken
refreshToken
}
}
Create a post
mutation {
createPost(input: { title: "title", content: "content" }) {
ok
error
}
}
Edit a post
mutation {
editPost(
input: { postId: 1, title: "title optional", content: "content optional" }
) {
ok
error
}
}
Delete a post
mutation {
deletePost(id: 1) {
ok
error
}
}
Retrieve all posts and their authors
query {
getPosts {
ok
error
posts {
author {
email
id
name
registeredAt
role
updatedAt
}
authorId
content
createdAt
id
title
updatedAt
}
}
}
Get a post
query {
getPost(id: 1) {
ok
error
post {
author {
email
id
name
registeredAt
role
updatedAt
}
authorId
content
createdAt
id
title
updatedAt
}
}
}
Hello
query {
hello(name: "woochanleee")
}
Hello, world!
query {
helloWorld
}
Get my profile
query {
me {
ok
error
user {
id
name
registeredAt
role
updatedAt
posts {
authorId
title
content
createdAt
updatedAt
}
}
}
}
Get profile by email
query {
userProfile(email: "test") {
ok
error
user {
id
name
registeredAt
role
updatedAt
posts {
authorId
title
content
createdAt
updatedAt
}
}
}
}
References
https://github.com/prisma/prisma-examples/blob/latest/typescript/graphql-nestjs-sdl-first/README.md