Spring Boot与GraphQL服务器无法启动。

huangapple go评论90阅读模式
英文:

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 = &quot;persons&quot;)
public class Person extends BaseAbstractEntity {

    @Column(name=&quot;person_name&quot;, nullable = false)
    private String name;

    @OneToOne(fetch = javax.persistence.FetchType.LAZY)
    @JoinColumn(name = &quot;contact_id&quot;, referencedColumnName = &quot;id&quot;)
    private Contact contact;

}

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = &quot;contacts&quot;)
public class Contact extends BaseAbstractEntity {

    @Column(name=&quot;contact_email_id&quot;)
    private String emailId;

    @Column(name=&quot;contact_mobile_number&quot;, nullable = false)
    private String mobileNumber;

    @OneToOne(mappedBy = &quot;contact&quot;)
    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&lt;Person&gt; 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!

huangapple
  • 本文由 发表于 2020年9月7日 18:31:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63775832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定