Skip to content

Instantly share code, notes, and snippets.

View pixel13's full-sized avatar

Tiziano Pessa pixel13

  • Register SpA
  • Firenze, Italy
View GitHub Profile
@pixel13
pixel13 / tictactoeSchema.graphqls
Last active April 20, 2021 21:43
GraphQL schema for a server exposing APIs for a tic-tac-toe game
"A cell of the game board"
type Cell {
"The row of this cell (0..2)"
row: Int!
"The column of this cell (0..2)"
column: Int!
"The value assigned to this cell (e.g. 'X', 'O'), or null if the cell is empty"
value: String
}
@pixel13
pixel13 / tictactoeSubscriptionResolverSkeleton.java
Last active April 17, 2021 22:12
Skeleton of the subscription resolver for the tic-tac-toe game
@Component
public class SubscriptionResolver implements GraphQLSubscriptionResolver {
public Publisher<String> opponentArrived() {
// Do something here...
}
public Publisher<Game> opponentMove() {
// Do something here...
}
@pixel13
pixel13 / tictactoeReactiveFlowBeans.java
Created April 17, 2021 22:11
Spring Bean for a tic-tac-toe GraphQL demo application
@Configuration
public class SubscriptionConfig {
@Bean
public Many<Game> gameSink() {
return Sinks.many().multicast().onBackpressureBuffer(Queues.SMALL_BUFFER_SIZE, false);
}
@Bean
public Flux<Game> gameFlux(Many<Game> gameSink) {
@pixel13
pixel13 / tictactoeMutationResolver.java
Last active April 18, 2021 08:10
Mutation resolver for the tic-tac-toe demo application
@Component
public class MutationResolver implements GraphQLMutationResolver {
@Autowired
private GameService gameService;
@Autowired
private AuthManager authManager;
@Autowired
@pixel13
pixel13 / tictactoeSubscriptionResolverFlux.java
Last active April 20, 2021 21:45
A subscription resolver for a simple tic-tac-toe application that uses a Reactor flux
@Component
public class SubscriptionResolver implements GraphQLSubscriptionResolver {
@Autowired
private Flux<Game> gameEvents;
public Publisher<String> opponentArrived() {
return gameEvents
.filter(game -> !game.isStarted())
.map(game -> game.getSecondPlayer().getName());
@pixel13
pixel13 / tictactoeSubscriptionConnectionListener.java
Last active April 18, 2021 09:04
A subscription connection listener for a tic-tac-toe application
@Component
public class SubscriptionConnectionListener implements ApolloSubscriptionConnectionListener {
@Autowired
private Authenticator authenticator;
@Override
public void onConnect(SubscriptionSession session, OperationMessage message) {
Map<String, String> payload = (Map<String, String>) message.getPayload();
@pixel13
pixel13 / tictactoeSubscriptionResolverComplete.java
Last active April 20, 2021 21:44
A complete implementation of the subscription resolver for the tic-tac-toe demo application
@Component
public class SubscriptionResolver implements GraphQLSubscriptionResolver {
@Autowired
private Flux<Game> gameEvents;
@Autowired
private AuthManager authManager;
@PreAuthorize("isAuthenticated()")