-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
graphql #50
Merged
graphql #50
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c33783e
graphql setup; graphql exception handling; graphql LocalDateTime convert
adfd385
graphql validation
a555821
separate controller for graphql
84f9f7e
dto removed in graphql; pageable validation fixes
4ebd670
dto in rest controller
679c337
fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eyakauleva as far I understand this ValidationSchemaWiring also should be represented as Spring Bean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@h1alexbel I call this method in another method, then why it should be a bean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eyakauleva for code reuse?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@h1alexbel but I call this method only once
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eyakauleva I know, but what about future usages?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@h1alexbel according to YAGNI principle features should only be added when required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eyakauleva YAGNI is about features or FR, not about maintainability, which is NFR
try to design all your classes, methods, functions with maintainability in mind
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done 679c337