forked from micronaut-projects/micronaut-guides
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migration of GraphQL todo example app to guide (micronaut-projects#822)
* Migration of GraphQL todo example app to guide See: micronaut-projects/micronaut-graphql#242 Relates to: micronaut-projects/micronaut-graphql#241 * remove tags and add languages to metadata.json * Add todo graphql guide and add tests * Switch to postgres and Flyway * Update screenshot to new ID format * More asciidoc work * Polish * Further cleanups... * Address feedback * use common flyway * use common test * use callouts Co-authored-by: Tim Yates <tim.yates@gmail.com>
- Loading branch information
Showing
23 changed files
with
896 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
guides/micronaut-graphql-todo/java/src/main/java/example/micronaut/Author.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package example.micronaut; | ||
|
||
import io.micronaut.data.annotation.GeneratedValue; | ||
import io.micronaut.data.annotation.Id; | ||
import io.micronaut.data.annotation.MappedEntity; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
import static io.micronaut.data.annotation.GeneratedValue.Type.AUTO; | ||
|
||
@MappedEntity // <1> | ||
public class Author { | ||
|
||
@Id // <1> | ||
@GeneratedValue(AUTO) | ||
private Long id; | ||
|
||
@NotNull | ||
private final String username; | ||
|
||
public Author(@NotNull String username) { | ||
this.username = username; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
guides/micronaut-graphql-todo/java/src/main/java/example/micronaut/AuthorDataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package example.micronaut; | ||
|
||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import jakarta.inject.Singleton; | ||
import org.dataloader.DataLoader; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
@Singleton // <1> | ||
public class AuthorDataFetcher implements DataFetcher<CompletionStage<Author>> { | ||
|
||
@Override | ||
public CompletionStage<Author> get(DataFetchingEnvironment environment) { | ||
ToDo toDo = environment.getSource(); | ||
DataLoader<Long, Author> authorDataLoader = environment.getDataLoader("author"); // <2> | ||
return authorDataLoader.load(toDo.getAuthorId()); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
guides/micronaut-graphql-todo/java/src/main/java/example/micronaut/AuthorDataLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package example.micronaut; | ||
|
||
import io.micronaut.scheduling.TaskExecutors; | ||
import jakarta.inject.Named; | ||
import jakarta.inject.Singleton; | ||
import org.dataloader.MappedBatchLoader; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.function.Function; | ||
|
||
import static java.util.stream.Collectors.toMap; | ||
|
||
@Singleton // <1> | ||
public class AuthorDataLoader implements MappedBatchLoader<Long, Author> { | ||
|
||
private final AuthorRepository authorRepository; | ||
private final ExecutorService executor; | ||
|
||
public AuthorDataLoader(AuthorRepository authorRepository, | ||
@Named(TaskExecutors.IO) ExecutorService executor) { | ||
this.authorRepository = authorRepository; | ||
this.executor = executor; | ||
} | ||
|
||
@Override | ||
public CompletionStage<Map<Long, Author>> load(Set<Long> keys) { | ||
return CompletableFuture.supplyAsync(() -> authorRepository | ||
.findByIdIn(keys) | ||
.stream() | ||
.collect(toMap(Author::getId, Function.identity())), | ||
executor | ||
); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
guides/micronaut-graphql-todo/java/src/main/java/example/micronaut/AuthorRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package example.micronaut; | ||
|
||
import io.micronaut.data.jdbc.annotation.JdbcRepository; | ||
import io.micronaut.data.repository.CrudRepository; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
import static io.micronaut.data.model.query.builder.sql.Dialect.POSTGRES; | ||
|
||
@JdbcRepository(dialect = POSTGRES) // <1> | ||
public interface AuthorRepository extends CrudRepository<Author, Long> { // <2> | ||
|
||
Optional<Author> findByUsername(String username); // <3> | ||
|
||
Collection<Author> findByIdIn(Collection<Long> ids); // <4> | ||
|
||
default Author findOrCreate(String username) { // <5> | ||
return findByUsername(username).orElseGet(() -> save(new Author(username))); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../micronaut-graphql-todo/java/src/main/java/example/micronaut/CompleteToDoDataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package example.micronaut; | ||
|
||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import jakarta.inject.Singleton; | ||
|
||
@Singleton // <1> | ||
public class CompleteToDoDataFetcher implements DataFetcher<Boolean> { | ||
|
||
private final ToDoRepository toDoRepository; | ||
|
||
public CompleteToDoDataFetcher(ToDoRepository toDoRepository) { // <2> | ||
this.toDoRepository = toDoRepository; | ||
} | ||
|
||
@Override | ||
public Boolean get(DataFetchingEnvironment env) { | ||
long id = Long.parseLong(env.getArgument("id")); | ||
return toDoRepository | ||
.findById(id) // <3> | ||
.map(this::setCompletedAndUpdate) | ||
.orElse(false); | ||
} | ||
|
||
private boolean setCompletedAndUpdate(ToDo todo) { | ||
todo.setCompleted(true); // <4> | ||
toDoRepository.update(todo); // <5> | ||
return true; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...es/micronaut-graphql-todo/java/src/main/java/example/micronaut/CreateToDoDataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package example.micronaut; | ||
|
||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import jakarta.inject.Singleton; | ||
import javax.transaction.Transactional; | ||
|
||
@Singleton // <1> | ||
public class CreateToDoDataFetcher implements DataFetcher<ToDo> { | ||
|
||
private final ToDoRepository toDoRepository; | ||
private final AuthorRepository authorRepository; | ||
|
||
public CreateToDoDataFetcher(ToDoRepository toDoRepository, // <2> | ||
AuthorRepository authorRepository) { | ||
this.toDoRepository = toDoRepository; | ||
this.authorRepository = authorRepository; | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public ToDo get(DataFetchingEnvironment env) { | ||
String title = env.getArgument("title"); | ||
String username = env.getArgument("author"); | ||
|
||
Author author = authorRepository.findOrCreate(username); // <3> | ||
|
||
ToDo toDo = new ToDo(title, author.getId()); | ||
|
||
return toDoRepository.save(toDo); // <4> | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...icronaut-graphql-todo/java/src/main/java/example/micronaut/DataLoaderRegistryFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package example.micronaut; | ||
|
||
import io.micronaut.context.annotation.Factory; | ||
import io.micronaut.runtime.http.scope.RequestScope; | ||
import org.dataloader.DataLoader; | ||
import org.dataloader.DataLoaderRegistry; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Factory // <1> | ||
public class DataLoaderRegistryFactory { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(DataLoaderRegistryFactory.class); | ||
|
||
@SuppressWarnings("unused") | ||
@RequestScope // <2> | ||
public DataLoaderRegistry dataLoaderRegistry(AuthorDataLoader authorDataLoader) { | ||
DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry(); | ||
dataLoaderRegistry.register( | ||
"author", | ||
DataLoader.newMappedDataLoader(authorDataLoader) | ||
); // <3> | ||
|
||
LOG.trace("Created new data loader registry"); | ||
|
||
return dataLoaderRegistry; | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
guides/micronaut-graphql-todo/java/src/main/java/example/micronaut/GraphQLFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package example.micronaut; | ||
|
||
import graphql.GraphQL; | ||
import graphql.schema.GraphQLSchema; | ||
import graphql.schema.idl.RuntimeWiring; | ||
import graphql.schema.idl.SchemaGenerator; | ||
import graphql.schema.idl.SchemaParser; | ||
import graphql.schema.idl.TypeDefinitionRegistry; | ||
import graphql.schema.idl.errors.SchemaMissingError; | ||
import io.micronaut.context.annotation.Bean; | ||
import io.micronaut.context.annotation.Factory; | ||
import io.micronaut.core.io.ResourceResolver; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
|
||
@Factory // <1> | ||
public class GraphQLFactory { | ||
|
||
@Singleton // <2> | ||
public GraphQL graphQL(ResourceResolver resourceResolver, | ||
ToDosDataFetcher toDosDataFetcher, | ||
CreateToDoDataFetcher createToDoDataFetcher, | ||
CompleteToDoDataFetcher completeToDoDataFetcher, | ||
AuthorDataFetcher authorDataFetcher) { | ||
SchemaParser schemaParser = new SchemaParser(); | ||
SchemaGenerator schemaGenerator = new SchemaGenerator(); | ||
|
||
// Load the schema | ||
InputStream schemaDefinition = resourceResolver | ||
.getResourceAsStream("classpath:schema.graphqls") | ||
.orElseThrow(SchemaMissingError::new); | ||
|
||
// Parse the schema and merge it into a type registry | ||
TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry(); | ||
typeRegistry.merge(schemaParser.parse(new BufferedReader(new InputStreamReader(schemaDefinition)))); | ||
|
||
// Create the runtime wiring. | ||
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring() | ||
.type("Query", typeWiring -> typeWiring // <3> | ||
.dataFetcher("toDos", toDosDataFetcher)) | ||
|
||
.type("Mutation", typeWiring -> typeWiring // <4> | ||
.dataFetcher("createToDo", createToDoDataFetcher) | ||
.dataFetcher("completeToDo", completeToDoDataFetcher)) | ||
|
||
.type("ToDo", typeWiring -> typeWiring // <5> | ||
.dataFetcher("author", authorDataFetcher)) | ||
|
||
.build(); | ||
|
||
// Create the executable schema. | ||
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring); | ||
|
||
// Return the GraphQL bean. | ||
return GraphQL.newGraphQL(graphQLSchema).build(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
guides/micronaut-graphql-todo/java/src/main/java/example/micronaut/ToDo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package example.micronaut; | ||
|
||
import io.micronaut.data.annotation.GeneratedValue; | ||
import io.micronaut.data.annotation.Id; | ||
import io.micronaut.data.annotation.MappedEntity; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
import static io.micronaut.data.annotation.GeneratedValue.Type.AUTO; | ||
|
||
@MappedEntity // <1> | ||
public class ToDo { | ||
|
||
@Id // <2> | ||
@GeneratedValue(AUTO) | ||
private Long id; | ||
|
||
@NotNull | ||
private String title; | ||
|
||
private boolean completed; | ||
|
||
private final long authorId; | ||
|
||
public ToDo(String title, long authorId) { | ||
this.title = title; | ||
this.authorId = authorId; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public boolean isCompleted() { | ||
return completed; | ||
} | ||
|
||
public void setCompleted(boolean completed) { | ||
this.completed = completed; | ||
} | ||
|
||
public long getAuthorId() { | ||
return authorId; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
guides/micronaut-graphql-todo/java/src/main/java/example/micronaut/ToDoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package example.micronaut; | ||
|
||
import io.micronaut.data.jdbc.annotation.JdbcRepository; | ||
import io.micronaut.data.model.query.builder.sql.Dialect; | ||
import io.micronaut.data.repository.PageableRepository; | ||
|
||
import static io.micronaut.data.model.query.builder.sql.Dialect.POSTGRES; | ||
|
||
@JdbcRepository(dialect = POSTGRES) // <1> | ||
public interface ToDoRepository extends PageableRepository<ToDo, Long> { // <2> | ||
} |
20 changes: 20 additions & 0 deletions
20
guides/micronaut-graphql-todo/java/src/main/java/example/micronaut/ToDosDataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package example.micronaut; | ||
|
||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import jakarta.inject.Singleton; | ||
|
||
@Singleton // <1> | ||
public class ToDosDataFetcher implements DataFetcher<Iterable<ToDo>> { | ||
|
||
private final ToDoRepository toDoRepository; | ||
|
||
public ToDosDataFetcher(ToDoRepository toDoRepository) { // <2> | ||
this.toDoRepository = toDoRepository; | ||
} | ||
|
||
@Override | ||
public Iterable<ToDo> get(DataFetchingEnvironment env) { | ||
return toDoRepository.findAll(); | ||
} | ||
} |
Oops, something went wrong.