Spring Boot - GraphQL - MySQL
Getting Started with Spring Boot, GraphQL and MySQL
Data
This demo application communicates with a MySQL database graphql_db
. The sql file is located in 'resources' folder.
There are three tables: language
, user
and project
.
Each project has a language and a user that created that project.
Note that this database and data are just for demonstration purposes.
Usage
GraphQL endpoint is located at http://localhost:8080/graphql
and can be changed in application.properties
file.
To use GraphiQL, locate http://localhost:8080/graphiql
(notice 'i' in graphiql).
The .graphqlconfig
file is located in resources/graphql
folder. The file is used to execute GraphQL from the IntelliJ IDEA.
Queries
allLanguages
- Returns all languages in the database
language(id: ID!)
- Returns one language with passed ID
languageType(languageType: String!)
- Returns languages with passed type (programming, data or markup)
allProjects: [Project]
- Returns all projects in the database
project(id: ID!): Project
- Returns one project with passed ID
userProjects(userId: ID!): [Project]
- Returns all projects created by the user with passed ID
allUsers: [User]
- Returns all users in the database
user(id: ID!): User
- Returns one user with passed ID
Mutations
createLanguage(name: String, code: String, color: String, languageType: String): Language!
- Creates a new language
updateLanguage(id: ID!, name: String, code: String, color: String, languageType: String): Language!
- Updates an existing language
deleteLanguage(id: ID!): Boolean
- Deletes a language by ID
createProject(title: String, description: String, language: ID!, user: ID!): Project!
- Creates a new project
updateProject(id: ID!, title: String, description: String, language: ID, user: ID): Project!
- Updates an existing project
deleteProject(id: ID!): Boolean
- Deletes a project by ID
createUser(username: String, email: String): User!
- Creates a new user
updateUser(id: ID!, username: String, email: String): User!
- Updates an existing user
deleteUser(id: ID!): Boolean
- Deletes a user by ID
Demo Usage
Returns name, color and language type for a language with ID 1
query {
language(id: 1) {
name
color
languageType
}
}
Returns id and name of all languages with type 'programming'
query {
languageType(languageType: "programming") {
id
name
}
}
Creates a new language and returns id and name
mutation {
createLanguage(name: "Kotlin", code: "kotlin", color: "F18E33", languageType: "PROGRAMMING") {
id
name
}
}
Updates email for user with ID 5 and returns id, username and email
mutation {
updateUser(id: 5, email: "[email protected]") {
id
username
email
}
}
Deletes user with ID 4
mutation {
deleteUser(id: 4)
}
Returns all projects created by user with ID 2
query {
userProjects(userId: 2) {
id
title
language {
name
}
}
}