Reformat sample applications

Fixes gh-54
This commit is contained in:
Brian Clozel
2021-06-02 14:29:19 +02:00
parent 39937679e3
commit 53e2a379de
18 changed files with 80 additions and 129 deletions

View File

@@ -12,13 +12,12 @@ import static org.springframework.web.context.request.RequestAttributes.SCOPE_RE
@Component
public class GreetingDataWiring implements RuntimeWiringCustomizer {
@Override
public void customize(RuntimeWiring.Builder builder) {
builder.type("Query", typeWiring ->
typeWiring.dataFetcher("greeting", env -> {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return "Hello " + attributes.getAttribute(RequestAttributeFilter.NAME_ATTRIBUTE, SCOPE_REQUEST);
}));
builder.type("Query", typeWiring -> typeWiring.dataFetcher("greeting", env -> {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return "Hello " + attributes.getAttribute(RequestAttributeFilter.NAME_ATTRIBUTE, SCOPE_REQUEST);
}));
}
}

View File

@@ -33,9 +33,9 @@ public class RequestAttributeFilter implements Filter {
public static final String NAME_ATTRIBUTE = RequestAttributeFilter.class.getName() + ".name";
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setAttribute(NAME_ATTRIBUTE, "007");
chain.doFilter(request, response);
}

View File

@@ -24,15 +24,13 @@ import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
/**
* {@link ThreadLocalAccessor} to expose a thread-bound RequestAttributes object
* to data fetchers in Spring GraphQL.
* {@link ThreadLocalAccessor} to expose a thread-bound RequestAttributes object to data
* fetchers in Spring GraphQL.
*/
@Component
public class RequestAttributesAccessor implements ThreadLocalAccessor {
private static final String ATTRIBUTES_KEY =
RequestAttributesAccessor.class.getName() + ".requestAttributes";
private static final String ATTRIBUTES_KEY = RequestAttributesAccessor.class.getName() + ".requestAttributes";
@Override
public void extractValues(Map<String, Object> container) {

View File

@@ -53,4 +53,5 @@ public class Project {
public void setReleases(List<Release> releases) {
this.releases = releases;
}
}

View File

@@ -15,17 +15,13 @@ public class ProjectDataWiring implements RuntimeWiringCustomizer {
@Override
public void customize(RuntimeWiring.Builder builder) {
builder
.type("Query", typeWiring ->
typeWiring.dataFetcher("project", env -> {
String slug = env.getArgument("slug");
return client.fetchProject(slug);
}))
.type("Project", typeWiring ->
typeWiring.dataFetcher("releases", env -> {
Project project = env.getSource();
return client.fetchProjectReleases(project.getSlug());
})
);
builder.type("Query", typeWiring -> typeWiring.dataFetcher("project", env -> {
String slug = env.getArgument("slug");
return client.fetchProject(slug);
})).type("Project", typeWiring -> typeWiring.dataFetcher("releases", env -> {
Project project = env.getSource();
return client.fetchProjectReleases(project.getSlug());
}));
}
}

View File

@@ -5,12 +5,13 @@ import java.util.Arrays;
import com.fasterxml.jackson.annotation.JsonCreator;
public enum ProjectStatus {
ACTIVE, COMMUNITY, INCUBATING, ATTIC;
@JsonCreator
public static ProjectStatus fromName(String name) {
return Arrays.stream(ProjectStatus.values())
.filter(type -> type.name().equals(name))
.findFirst().orElse(ProjectStatus.ACTIVE);
return Arrays.stream(ProjectStatus.values()).filter(type -> type.name().equals(name)).findFirst()
.orElse(ProjectStatus.ACTIVE);
}
}

View File

@@ -51,4 +51,5 @@ public class Release {
public void setCurrent(boolean current) {
this.current = current;
}
}

View File

@@ -5,12 +5,13 @@ import java.util.Arrays;
import com.fasterxml.jackson.annotation.JsonCreator;
public enum ReleaseStatus {
GENERAL_AVAILABILITY, MILESTONE, SNAPSHOT;
@JsonCreator
public static ReleaseStatus fromName(String name) {
return Arrays.stream(ReleaseStatus.values())
.filter(type -> type.name().equals(name))
.findFirst().orElse(ReleaseStatus.GENERAL_AVAILABILITY);
return Arrays.stream(ReleaseStatus.values()).filter(type -> type.name().equals(name)).findFirst()
.orElse(ReleaseStatus.GENERAL_AVAILABILITY);
}
}

View File

@@ -19,27 +19,26 @@ import org.springframework.web.client.RestTemplate;
@Component
public class SpringProjectsClient {
private static final TypeReferences.CollectionModelType<Release> releaseCollection =
new TypeReferences.CollectionModelType<Release>() { };
private static final TypeReferences.CollectionModelType<Release> releaseCollection = new TypeReferences.CollectionModelType<Release>() {
};
private final Traverson traverson;
public SpringProjectsClient(RestTemplateBuilder builder) {
RestTemplate restTemplate = builder.messageConverters(Traverson.getDefaultMessageConverters(MediaTypes.HAL_JSON)).build();
RestTemplate restTemplate = builder
.messageConverters(Traverson.getDefaultMessageConverters(MediaTypes.HAL_JSON)).build();
this.traverson = new Traverson(URI.create("https://spring.io/api/"), MediaTypes.HAL_JSON);
this.traverson.setRestOperations(restTemplate);
}
public Project fetchProject(String projectSlug) {
return this.traverson.follow("projects")
.follow(Hop.rel("project").withParameter("id", projectSlug))
return this.traverson.follow("projects").follow(Hop.rel("project").withParameter("id", projectSlug))
.toObject(Project.class);
}
public List<Release> fetchProjectReleases(String projectSlug) {
CollectionModel<Release> releases = this.traverson.follow("projects")
.follow(Hop.rel("project").withParameter("id", projectSlug))
.follow(Hop.rel("releases"))
.follow(Hop.rel("project").withParameter("id", projectSlug)).follow(Hop.rel("releases"))
.toObject(releaseCollection);
return new ArrayList(releases.getContent());
}

View File

@@ -20,9 +20,11 @@ public class ArtifactRepositoriesInitializer implements ApplicationRunner {
public void run(ApplicationArguments args) throws Exception {
List<ArtifactRepository> repositoryList = Arrays.asList(
new ArtifactRepository("spring-releases", "Spring Releases", "https://repo.spring.io/libs-releases"),
new ArtifactRepository("spring-milestones", "Spring Milestones", "https://repo.spring.io/libs-milestones"),
new ArtifactRepository("spring-snapshots", "Spring Snapshots", "https://repo.spring.io/libs-snapshots")
);
new ArtifactRepository("spring-milestones", "Spring Milestones",
"https://repo.spring.io/libs-milestones"),
new ArtifactRepository("spring-snapshots", "Spring Snapshots",
"https://repo.spring.io/libs-snapshots"));
repositories.saveAll(repositoryList);
}
}

View File

@@ -55,4 +55,5 @@ public class ArtifactRepository {
public void setSnapshotsEnabled(boolean snapshotsEnabled) {
this.snapshotsEnabled = snapshotsEnabled;
}
}

View File

@@ -15,8 +15,9 @@ public class ArtifactRepositoryDataWiring implements RuntimeWiringCustomizer {
@Override
public void customize(RuntimeWiring.Builder builder) {
builder.type("Query", typeWiring -> typeWiring
.dataFetcher("artifactRepositories", env -> this.repositories.findAll())
.dataFetcher("artifactRepository", env -> this.repositories.findById(env.getArgument("id"))));
builder.type("Query",
typeWiring -> typeWiring.dataFetcher("artifactRepositories", env -> this.repositories.findAll())
.dataFetcher("artifactRepository", env -> this.repositories.findById(env.getArgument("id"))));
}
}