Skip to content
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

Add search feature to webmvc-http sample #211

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class SpringProjectsClient {
private static final TypeReferences.CollectionModelType<Release> releaseCollection =
new TypeReferences.CollectionModelType<Release>() {};

private static final TypeReferences.CollectionModelType<Project> projectCollection =
new TypeReferences.CollectionModelType<Project>() {};

private final Traverson traverson;

public SpringProjectsClient(RestTemplateBuilder builder) {
Expand All @@ -39,7 +42,11 @@ public List<Release> fetchProjectReleases(String projectSlug) {
CollectionModel<Release> releases = this.traverson.follow("projects")
.follow(Hop.rel("project").withParameter("id", projectSlug)).follow(Hop.rel("releases"))
.toObject(releaseCollection);
return new ArrayList(releases.getContent());
return new ArrayList<>(releases.getContent());
}

public List<Project> fetchAllProjects() {
CollectionModel<Project> projects = this.traverson.follow("projects").toObject(projectCollection);
return new ArrayList<>(projects.getContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql.search;

import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;

import io.spring.sample.graphql.project.Project;
import io.spring.sample.graphql.project.SpringProjectsClient;
import io.spring.sample.graphql.repository.ArtifactRepositories;
import io.spring.sample.graphql.repository.ArtifactRepository;

@Controller
public class SearchController {

private final SpringProjectsClient client;
private final ArtifactRepositories repositories;

public SearchController(SpringProjectsClient client, ArtifactRepositories repositories) {
this.client = client;
this.repositories = repositories;
}

@QueryMapping
public Stream<Object> search(@Argument String text, @Argument SearchType type) {
Stream<ArtifactRepository> repos =
(type == null || type == SearchType.ARTIFACTREPOSITORY) ?
StreamSupport.stream(repositories.findAll().spliterator(), false)
.filter(r -> r.getName().toLowerCase().contains(text.toLowerCase())) :
Stream.empty();
Stream<Project> projects =
(type == null || type == SearchType.PROJECT) ?
client.fetchAllProjects().stream()
.filter(p -> p.getName().toLowerCase().contains(text.toLowerCase())) :
Stream.empty();
return Stream.concat(repos, projects);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql.search;

public enum SearchType {
ARTIFACTREPOSITORY,
PROJECT
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ type Query {
artifactRepositories : [ArtifactRepository]
artifactRepository(id : ID!) : ArtifactRepository
project(slug: ID!): Project
search(text: String!, type: SearchType): [SearchResult]
}

type ArtifactRepository {
Expand All @@ -26,6 +27,13 @@ type Release {
current: Boolean
}

union SearchResult = ArtifactRepository | Project

enum SearchType {
ARTIFACTREPOSITORY
PROJECT
}

enum ProjectStatus {
ACTIVE
COMMUNITY
Expand Down
10 changes: 10 additions & 0 deletions samples/webmvc-http/src/main/resources/graphql/search.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
query search($text: String!, $type: SearchType) {
search(text: $text, type: $type) {
... on ArtifactRepository {
name
}
... on Project {
name
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql.search;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.graphql.boot.test.tester.AutoConfigureWebGraphQlTester;
import org.springframework.graphql.test.tester.WebGraphQlTester;

import org.junit.jupiter.api.Test;

@SpringBootTest
@AutoConfigureWebGraphQlTester
class SearchControllerTests {

@Autowired
private WebGraphQlTester graphQlTester;

@Test
void fullResults() {
this.graphQlTester.queryName("search")
.variable("text", "spring")
.execute()
.path("search[*].name")
.entityList(String.class).hasSizeGreaterThan(3);
}

@Test
void artifactRepositoriesResults() {
this.graphQlTester.queryName("search")
.variable("text", "spring")
.variable("type", SearchType.ARTIFACTREPOSITORY)
.execute()
.path("search[*].name")
.entityList(String.class).hasSize(3);
}

@Test
void projectsResults() {
this.graphQlTester.queryName("search")
.variable("text", "spring")
.variable("type", SearchType.PROJECT)
.execute()
.path("search[*].name")
.entityList(String.class).hasSizeGreaterThan(3);
}
}