An open API service indexing awesome lists of open source software.

https://github.com/vladimir-dejanovic/graphql-spring-boot-example

graphql-spring-boot-example
https://github.com/vladimir-dejanovic/graphql-spring-boot-example

graphql graphql-server java spring-boot

Last synced: over 1 year ago
JSON representation

graphql-spring-boot-example

Awesome Lists containing this project

README

          

# GraphQL example implemented with Spring Boot

This is simple example of GraphQL API implemented in Java with Spring Boot, and relaying on it's magic :)

There is example of implementation of **Query** and **Subscription**.

## Dependencies

### GraphQL

For GraphQL dependency added

```

com.graphql-java
graphql-java
8.0


com.graphql-java
graphql-java-tools
5.0.0

```

### GraphQL Spring Boot integration

To connect GraphQL and Spring Boot added this dependency

```

com.graphql-java
graphql-spring-boot-starter
5.0.2

```

### GraphiQL "out of the box"

In order to get GraqhiQL, UI for testing local version of GraphQL API added this dependency

```

com.graphql-java
graphiql-spring-boot-starter
5.0.2

```

For GraphiQL hit http://localhost:8080/graphiql, when you start application

## Query implementation

Query implementation is almost exactly the same like all other my examples of Query. Only difference is annotation **@Component** at top.
For full code just open [Query.java](https://github.com/vladimir-dejanovic/graphql-spring-boot-example/blob/master/src/main/java/xyz/itshark/play/graphqlspringboot/example/resolves/Query.java)

Query example in GraphiQL
```
query {
allData {
message
}
}
```

## Mutation implementation

Mutation implementation is almost exactly the same like all other my examples of Mutation. Only difference is annotation **@Component** at top.
For full code just open [Mutation.java](https://github.com/vladimir-dejanovic/graphql-spring-boot-example/blob/master/src/main/java/xyz/itshark/play/graphqlspringboot/example/resolves/Mutation.java)

Mutation example in GraphiQL
```
mutation {
add(message:"Hello") {
message
}
}
```

after mutation you can run query again and check if all worked as intended.

## Subscription implementation

Subscription implementation is done using RXJava and by implementing **GraphQLSubscriptionResolver**

For full code example check [Subscription.java](https://github.com/vladimir-dejanovic/graphql-spring-boot-example/blob/master/src/main/java/xyz/itshark/play/graphqlspringboot/example/resolves/Subscription.java)

Subscription **can be** tested from GraphiQL.

Subscription example in GraphiQL
```
subscription {
data {
message
}
}
```