- add dependencies into pom.xml
<dependency>
<groupId>cc.voox</groupId>
<artifactId>graphql</artifactId>
<version>0.9.3</version>
</dependency>
- add config into java bean
import cc.voox.graphql.GraphqlProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(value = {"cc.voox.graphql"})
public class GraphQLConfig {
@Bean()
public GraphqlProperties graphqlProperties() {
GraphqlProperties graphqlProperties = new GraphqlProperties();
graphqlProperties.setScanPath("com.xxx");
return graphqlProperties;
}
}
- define schema in classpath or custom schema path value (default value: schema.graphql)
type Query {
test: String
}
type Book {
id: ID
name: String
pageCount: Int
}
- use @Query in entity class or method
- use @QueryMethod in method
- use @QueryField in parameter of method
@Query
@Component
public class TestService {
@QueryMethod("bookById")
Map<String, String> getBookById(@QueryField("id") String id) {
return books
.stream()
.filter(book -> book.get("id").equals(id))
.findFirst()
.orElse(null);
}
}
class Currency implements IScalar {
}
class Lower implements IDirective {
}
class BookDatalodaer implements IDataloader {
}