-
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.
Merge pull request #50 from eyakauleva/49
graphql
- Loading branch information
Showing
9 changed files
with
315 additions
and
7 deletions.
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
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
65 changes: 65 additions & 0 deletions
65
src/main/java/com/solvd/micro9/users/web/controller/GraphqlUserController.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,65 @@ | ||
package com.solvd.micro9.users.web.controller; | ||
|
||
import com.solvd.micro9.users.domain.aggregate.User; | ||
import com.solvd.micro9.users.domain.command.CreateUserCommand; | ||
import com.solvd.micro9.users.domain.command.DeleteUserCommand; | ||
import com.solvd.micro9.users.domain.criteria.UserCriteria; | ||
import com.solvd.micro9.users.domain.es.EsUser; | ||
import com.solvd.micro9.users.domain.query.EsUserQuery; | ||
import com.solvd.micro9.users.service.EsUserCommandHandler; | ||
import com.solvd.micro9.users.service.UserQueryHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.graphql.data.method.annotation.Argument; | ||
import org.springframework.graphql.data.method.annotation.MutationMapping; | ||
import org.springframework.graphql.data.method.annotation.QueryMapping; | ||
import org.springframework.stereotype.Controller; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
public class GraphqlUserController { | ||
|
||
private final EsUserCommandHandler commandHandler; | ||
private final UserQueryHandler queryHandler; | ||
|
||
@QueryMapping("getAllUsers") | ||
public Flux<User> getAll() { | ||
return queryHandler.getAll(); | ||
} | ||
|
||
@QueryMapping("findByCriteria") | ||
public Flux<User> findByCriteria(@Argument final UserCriteria criteria, | ||
@Argument final int size, | ||
@Argument final int page) { | ||
Pageable pageable = PageRequest.of(page, size); | ||
return queryHandler.findByCriteria(criteria, pageable); | ||
} | ||
|
||
@QueryMapping("findUserById") | ||
public Mono<User> findByUserId(@Argument final String userId) { | ||
EsUserQuery query = new EsUserQuery(userId); | ||
return queryHandler.findById(query); | ||
} | ||
|
||
@MutationMapping("createUser") | ||
public Mono<EsUser> create( | ||
@Argument final User user, | ||
@Argument final String commandBy | ||
) { | ||
CreateUserCommand command = new CreateUserCommand(user, commandBy); | ||
return commandHandler.apply(command); | ||
} | ||
|
||
@MutationMapping("deleteUser") | ||
public Mono<EsUser> delete( | ||
@Argument final String id, | ||
@Argument final String commandBy | ||
) { | ||
DeleteUserCommand command = new DeleteUserCommand(id, commandBy); | ||
return commandHandler.apply(command); | ||
} | ||
|
||
} |
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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/solvd/micro9/users/web/controller/exception/GraphqlExceptionResolver.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,25 @@ | ||
package com.solvd.micro9.users.web.controller.exception; | ||
|
||
import graphql.GraphQLError; | ||
import graphql.GraphqlErrorBuilder; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter; | ||
import org.springframework.graphql.execution.ErrorType; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class GraphqlExceptionResolver extends DataFetcherExceptionResolverAdapter { | ||
|
||
@Override | ||
protected GraphQLError resolveToSingleError( | ||
final Throwable ex, final DataFetchingEnvironment env | ||
) { | ||
return GraphqlErrorBuilder.newError() | ||
.errorType(ErrorType.BAD_REQUEST) | ||
.message(ex.getMessage()) | ||
.path(env.getExecutionStepInfo().getPath()) | ||
.location(env.getField().getSourceLocation()) | ||
.build(); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
...ain/java/com/solvd/micro9/users/web/controller/graphql/LocalDateTimeToStringCoercing.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,58 @@ | ||
package com.solvd.micro9.users.web.controller.graphql; | ||
|
||
import graphql.language.StringValue; | ||
import graphql.schema.Coercing; | ||
import graphql.schema.CoercingParseLiteralException; | ||
import graphql.schema.CoercingParseValueException; | ||
import graphql.schema.CoercingSerializeException; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
|
||
@Component | ||
public class LocalDateTimeToStringCoercing implements Coercing<LocalDateTime, String> { | ||
|
||
@Override | ||
public String serialize(final Object input) { | ||
if (input instanceof LocalDateTime) { | ||
DateTimeFormatter formatter = | ||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | ||
return ((LocalDateTime) input).format(formatter); | ||
} else { | ||
throw new CoercingSerializeException( | ||
"Expected a LocalDateTime object" | ||
); | ||
} | ||
} | ||
|
||
@Override | ||
public LocalDateTime parseValue(final Object input) { | ||
return this.parse(input); | ||
} | ||
|
||
@Override | ||
public LocalDateTime parseLiteral(final Object input) { | ||
return this.parse(input); | ||
} | ||
|
||
private LocalDateTime parse(final Object input) { | ||
try { | ||
if (input instanceof StringValue) { | ||
return LocalDateTime.parse(((StringValue) input).getValue()); | ||
} else if (input instanceof String) { | ||
return LocalDateTime.parse((String) input); | ||
} else { | ||
throw new CoercingParseLiteralException( | ||
"Expected String or StringValue" | ||
); | ||
} | ||
} catch (DateTimeParseException e) { | ||
throw new CoercingParseValueException( | ||
String.format("Not a valid date: '%s'.", input), e | ||
); | ||
} | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/solvd/micro9/users/web/mapper/EsMapper.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,17 @@ | ||
package com.solvd.micro9.users.web.mapper; | ||
|
||
import com.solvd.micro9.users.domain.es.EsUser; | ||
import com.solvd.micro9.users.web.dto.EsDto; | ||
import org.mapstruct.Mapper; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface EsMapper { | ||
|
||
EsDto domainToDto(EsUser esUser); | ||
|
||
default Mono<EsDto> domainToDto(Mono<EsUser> esUserMono) { | ||
return esUserMono.map(this::domainToDto); | ||
} | ||
|
||
} |
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
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,83 @@ | ||
scalar LocalDateTime | ||
|
||
directive @NotBlank(message : String = "graphql.validation.NotBlank.message") | ||
on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ||
directive @Min(value : Int = 0, message : String = "graphql.validation.Min.message") | ||
on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ||
|
||
type Query { | ||
getAllUsers: [User] | ||
findUserById(userId: ID!): User | ||
findByCriteria(criteria: CriteriaInput!, size: Int! @Min(value: 1), page: Int! @Min): [User] | ||
} | ||
|
||
type Mutation { | ||
createUser(user: UserInput!, commandBy: String!): Es | ||
deleteUser(id: ID!, commandBy: String!): Es | ||
} | ||
|
||
type User { | ||
id: ID | ||
firstName: String! | ||
lastName: String! | ||
email: String! | ||
phone: String! | ||
age: Int | ||
gender: Gender | ||
height: Float | ||
weight: Float | ||
eyesColor: EyesColor | ||
startStudyYear: Int | ||
endStudyYear: Int | ||
} | ||
|
||
enum Gender { | ||
MALE | ||
FEMALE | ||
UNSET | ||
} | ||
|
||
enum EyesColor { | ||
BLUE | ||
GREEN | ||
BROWN | ||
GREY | ||
UNSET | ||
} | ||
|
||
input UserInput { | ||
firstName: String @NotBlank | ||
lastName: String @NotBlank | ||
email: String @NotBlank | ||
phone: String @NotBlank | ||
age: Int | ||
gender: Gender | ||
height: Float | ||
weight: Float | ||
eyesColor: EyesColor | ||
startStudyYear: Int | ||
endStudyYear: Int | ||
} | ||
|
||
input CriteriaInput { | ||
name: String | ||
phone: String | ||
age: Int | ||
heightFrom: Float | ||
heightTo: Float | ||
weightFrom: Float | ||
weightTo: Float | ||
genders: [Gender] | ||
eyesColors: [EyesColor] | ||
studyYear: Int | ||
} | ||
|
||
type Es { | ||
id: ID | ||
type: String | ||
time: LocalDateTime | ||
createdBy: String | ||
entityId: String | ||
payload: String | ||
status: String | ||
} |