英文:
Spring Boot with GraphQL server not starting
问题
type Person {
id: ID
name: String!
contact: Contact
}
type Query {
countPersons: Long!
findByName(name: String!): [Person]!
findAllPerson: [Person]!
}
type Contact {
id: ID
emailId: String
mobileNumber: String!
}
extend type Query {
findAllContact: [Contact]!
countContacts: Long!
findByMobileNumber(mobileNumber: String!): [Contact]!
findByEmailId(emailId: String!): [Contact]!
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "persons")
public class Person extends BaseAbstractEntity {
@Column(name="person_name", nullable = false)
private String name;
@OneToOne(fetch = javax.persistence.FetchType.LAZY)
@JoinColumn(name = "contact_id", referencedColumnName = "id")
private Contact contact;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "contacts")
public class Contact extends BaseAbstractEntity {
@Column(name="contact_email_id")
private String emailId;
@Column(name="contact_mobile_number", nullable = false)
private String mobileNumber;
@OneToOne(mappedBy = "contact")
private Person person;
}
public class Query implements GraphQLQueryResolver {
private final PersonRepository personRepository;
private final ContactRepository contactRepository;
public Query(PersonRepository personRepository, ContactRepository contactRepository) {
this.personRepository = personRepository;
this.contactRepository = contactRepository;
}
public Iterable<Person> findAllPerson() {
return personRepository.findAll();
}
}
错误提示:
com.coxautodev.graphql.tools.SchemaClassScannerError: Unable to match type definition (NonNullType{type=ListType{type=TypeName{name='Person'}}}) with java type (class com.demo.graphql.entities.Person): Java class is not a List or generic type information was lost: class com.demo.graphql.entities.Person
修改后的可用代码:
type Query {
countPersons: Long!
# findByName(name: String!): [Person]!
# findAllPerson: [Person]!
}
应用链接:https://github.com/shubham-v/spring-boot-graphql-demo/
英文:
type Person {
id: ID
name: String!
contact: Contact
}
type Query {
countPersons: Long!
findByName(name: String!): [Person]!
findAllPerson: [Person]!
}
type Contact {
id: ID
emailId: String
mobileNumber: String!
}
extend type Query {
findAllContact: [Contact]!
countContacts: Long!
findByMobileNumber(mobileNumber: String!): [Contact]!
findByEmailId(emailId: String!): [Contact]!
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "persons")
public class Person extends BaseAbstractEntity {
@Column(name="person_name", nullable = false)
private String name;
@OneToOne(fetch = javax.persistence.FetchType.LAZY)
@JoinColumn(name = "contact_id", referencedColumnName = "id")
private Contact contact;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "contacts")
public class Contact extends BaseAbstractEntity {
@Column(name="contact_email_id")
private String emailId;
@Column(name="contact_mobile_number", nullable = false)
private String mobileNumber;
@OneToOne(mappedBy = "contact")
private Person person;
}
public class Query implements GraphQLQueryResolver {
private final PersonRepository personRepository;
private final ContactRepository contactRepository;
public Query(PersonRepository personRepository, ContactRepository contactRepository) {
this.personRepository = personRepository;
this.contactRepository = contactRepository;
}
public Iterable<Person> findAllPerson() {
return personRepository.findAll();
}
}
Getting error on starting server:
> com.coxautodev.graphql.tools.SchemaClassScannerError: Unable to match type definition (NonNullType{type=ListType{type=TypeName{name='Person'}}}) with java type (class com.demo.graphql.entities.Person): Java class is not a List or generic type information was lost: class com.demo.graphql.entities.Person
The application starts after commenting out findByName
and findAllPersonfindAllPerson
in query:
type Query {
countPersons: Long!
# findByName(name: String!): [Person]!
# findAllPerson: [Person]!
}
I think the issue is with [Person]
in query response which is a list of person.
Link to application: https://github.com/shubham-v/spring-boot-graphql-demo/
答案1
得分: 1
在共享仓库中查找:
public Person findByName(String name) {
return personRepository.findByName(name);
}
解析器返回单个 Person
类对象,但模式需要一个 Person
列表。
尝试将模式更改为:
findByName(name: String!): Person!
英文:
Looking in the shared repo:
public Person findByName(String name) {
return personRepository.findByName(name);
}
The resolver is returning a single Person
class object but the schema expects a list of Person
.
Try changing the schema to:
findByName(name: String!): Person!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论