<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.netflix.graphql.dgs</groupId>
<artifactId>graphql-dgs-platform-dependencies</artifactId>
<!-- The DGS BOM/platform dependency. This is the only place you set version of DGS -->
<version>4.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.graphql.dgs</groupId>
<artifactId>graphql-dgs-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
type Query {
shows(titleFilter: String): [Show]
}
type Show {
title: String
releaseYear: Int
}
这个 Schema 允许查询 shows 的列表,并可以通过 title 进行筛选。
实现一个 Data Fetcher
Java:
@DgsComponent
public class ShowsDatafetcher {
private final List<Show> shows = List.of(
new Show("Stranger Things", 2016),
new Show("Ozark", 2017),
new Show("The Crown", 2016),
new Show("Dead to Me", 2019),
new Show("Orange is the New Black", 2013)
);
@DgsQuery
public List<Show> shows(@InputArgument String titleFilter) {
if(titleFilter == null) {
return shows;
}
return shows.stream().filter(s -> s.getTitle().contains(titleFilter)).collect(Collectors.toList());
}
}
public class Show {
private final String title;
private final Integer releaseYear ;
public Show(String title, Integer releaseYear) {
this.title = title;
this.releaseYear = releaseYear;
}
public String getTitle() {
return title;
}
public Integer getReleaseYear() {
return releaseYear;
}
}
Kotlin:
@DgsComponent
class ShowsDataFetcher {
private val shows = listOf(
Show("Stranger Things", 2016),
Show("Ozark", 2017),
Show("The Crown", 2016),
Show("Dead to Me", 2019),
Show("Orange is the New Black", 2013))
@DgsQuery
fun shows(@InputArgument titleFilter : String?): List<Show> {
return if(titleFilter != null) {
shows.filter { it.title.contains(titleFilter) }
} else {
shows
}
}
data class Show(val title: String, val releaseYear: Int)
}