我得到以下内容,似乎无法找到答案.
Error [ValidationError{validationErrorType=FieldUndefined, queryPath=[find_by_id], message=Validation error of type FieldUndefined: Field 'find_by_id' in type 'Query' is undefined @ 'find_by_id', locations=[SourceLocation{line=1, column=2}], description='Field 'find_by_id' in type 'Query' is undefined'}]
Run Code Online (Sandbox Code Playgroud)
我的守则.
询问
@GraphQLQuery(name = "find_by_id")
public Event findById(@GraphQLArgument(name = "id") Long id) {
Run Code Online (Sandbox Code Playgroud)
架构
@EJB
private EventFacade eventFacade; // Normal stateless bean
GraphQLSchema guestSchema = new GraphQLSchemaGenerator()
.withOperationsFromSingleton(eventFacade)
.withValueMapperFactory(new JacksonValueMapperFactory())
.withDefaults()
.generate();
GraphQL graphQL = GraphQL.newGraphQL(guestSchema).build();
Run Code Online (Sandbox Code Playgroud)
要执行的代码
String query = "{find_by_id (id: 1){eventName}}";
ExecutionResult result = graphQL.execute(query);
Run Code Online (Sandbox Code Playgroud)
使用SPQR lib
事件POJO是基本的,eventName为String,抽象(Parent)类为id.实体类位于不同的jar(Entity Jar)中.执行查询和构建模式的代码位于EJB Jar中. …
给定一个 graphql 查询
query @my (arg:1) {
foo @my (arg:2) {
bar @my (arg:3)
foobar @my (arg:4)
}
}
Run Code Online (Sandbox Code Playgroud)
和graphql服务器实现使用 graphql-spqr
query @my (arg:1) {
foo @my (arg:2) {
bar @my (arg:3)
foobar @my (arg:4)
}
}
Run Code Online (Sandbox Code Playgroud)
@GraphQLType(name="foo")
public class Foo{
@GraphQLQuery(name="bar")
public String bar;
@GraphQLQuery(name="foobar")
public String foobar;
}
Run Code Online (Sandbox Code Playgroud)
@GraphQLDirective(name="my", locations = {QUERY, FIELD} )
public class MyGraphqlDirective{
@GraphqlInputField("arg")
public int arg;
}
Run Code Online (Sandbox Code Playgroud)
我可以访问(arg:2)查询 foo(如果存在),(arg:1)如果不存在则回退。
但是,如果我想在 foo 和 foobar 字段上阅读它,我会遇到问题。
我想让它不可读,滥用@GraphqlContext每个领域。有没有更简单的方法可以在字段级别存档一些简单的逻辑(例如大写、语言环境、舍入、数字格式)。
还尝试过dataFetchingEnvironment.getSelectionSet().get().getSubField("edge/node/foo/bar").getSingleField().getDirective() …
如何使用 Graphql SPQR 库实现 GraphQL 的订阅功能?
有没有办法检索客户端在 GraphQL 查询中请求的字段列表?
假设我有以下类型:
type Book {
isbn: String
title: String
genre: String
author: Author
}
type Author {
name: String
surname: String
age: Int
}Run Code Online (Sandbox Code Playgroud)
Java 端有没有办法在用 @GraphQLQuery 注释的方法中知道客户端请求的字段?
例如,有以下查询:
query {
book ( isbn = "12345" ) {
title
genre
}
}
query {
book ( isbn = "12345" ) {
title
author {
name
surname
}
}
}Run Code Online (Sandbox Code Playgroud)
我可以知道第一个查询请求 Book 的标题和类型字段以及 Book 中的第二个必需标题以及作者的姓名和姓氏?
谢谢马西莫
我正在使用 spring boot 和 graphql 开发一个新项目。我对如何继续感到困惑,因为有两种方法可以开发它,一种是通过 graphqls 文件和基于注释的方法。我更喜欢基于注释的方法,但它们稳定吗?例如: https: //github.com/leangen/graphql-spqr。
我有一个需要defaultValue编译时常量的注释。我defaultValue从enum下面:
enum class RaceType {
MARATHON,
SPRINT;
companion object {
fun apply(type: RaceType): RaceDto {
return when (type) {
MARATHON -> MarathonDto()
SPRINT -> SprintDto()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的dtos如下:
interface RaceDto {
}
data class MarathonDto: RaceDto
data class SprintDto: RaceDto
Run Code Online (Sandbox Code Playgroud)
当我使用注释时,@QraphQLArgument(defaultValue = RaceType.SPRINT.name)Kotlin 要求RaceType.SPRINT.name是编译时常量。
注释实现本身:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface GraphQLArgument {
String NONE = "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
String NULL = "\n\t\t\n\t\t\n\ue000\ue001\ue002\ue003\n\t\t\t\t\n";
String name();
String description() default "";
String …Run Code Online (Sandbox Code Playgroud) 我真的很喜欢SPQR轻松地将graphql与现有系统集成的方式,我唯一想看的是.graphqls文件,因此我可以在GraphQL语法上学习更多。
有没有一种方法可以从集成SPQR注释的现有代码中生成方案文件?
为了提供一些代码,让我们使用GitHub站点上的相同代码
实体:
public class User {
private String name;
private Integer id;
private Date registrationDate;
@GraphQLQuery(name = "name", description = "A person's name")
public String getName() {
return name;
}
@GraphQLQuery
public Integer getId() {
return id;
}
@GraphQLQuery(name = "regDate", description = "Date of registration")
public Date getRegistrationDate() {
return registrationDate;
}
}
Run Code Online (Sandbox Code Playgroud)
服务等级:
class UserService {
@GraphQLQuery(name = "user")
public User getById(@GraphQLArgument(name = "id") Integer id) {
...
}
} …Run Code Online (Sandbox Code Playgroud) 目前,我正在抛出RuntimeException's 以返回 GraphQL 验证错误。它工作得非常好,除了它会在我的日志中抛出带有大量堆栈跟踪的可怕错误。
在这里您可以看到我正在检查提交的新用户注册变更,以确保密码彼此匹配并且电子邮件地址尚未使用。
在 GraphQL SPQR Spring Boot Starter 中执行此操作的正确方法是什么。
@GraphQLMutation (name="register")
public User register(@GraphQLArgument(name="firstname") String firstname, @GraphQLArgument(name="lastname") String lastname, @GraphQLArgument(name="email") String email, @GraphQLArgument(name="msisdn") String msisdn, @GraphQLArgument(name="password") String password, @GraphQLArgument (name="confirmPassword") String confirmPassword) {
if (userRepo.findByEmail(email) != null) {
throw new RuntimeException("User already exists");
}
if (!password.equals(confirmPassword)) {
throw new RuntimeException("Passwords do not match");
}
User newUser = new User();
//...
return userRepo.save(newUser);
}
Run Code Online (Sandbox Code Playgroud) graphql-spqr ×8
graphql ×5
graphql-java ×5
java ×4
annotations ×2
kotlin ×1
spring-boot ×1
validation ×1