question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to create Field with OneToMany

See original GitHub issue

I try

 @OneToMany(type => Family, family => family.childhood)
    @Field({type: () => Family})
    family: Family[];

Error: @ObjectType Family.childhood: Validation of type failed. Resolved type for @Field must be GraphQLOutputType.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

5reactions
pie6kcommented, Apr 12, 2018

Seems you have circular relationship (Childhood > Family > Childhood > Family) etc. In such case, you have to define type as function. (http://siawyoung.com/coding/javascript/circular-references-graphql-type-definitions.html)

Try to modify:

    @ManyToOne(type => Childhood, childhood => childhood.family)
    @Field()
    childhood: Childhood;

to

    @ManyToOne(type => Childhood, childhood => childhood.family)
    @Field({ type: () => Childhood })
    childhood: Childhood;

Also change

    @OneToMany(type => Family, family => family.childhood)
    @Field()
    family: Family[];

to

    @OneToMany(type => Family, family => family.childhood)
    @Field({ type: () => [Family] })
    family: Family[];

And let me know if that helps.

Note that if you use array types (eg. Family[]) you’d have to set type manually anyway as typescript is not able to guess type of array item.

3reactions
pie6kcommented, Apr 12, 2018

@korolariya you don’t need to define function that returns your Childhoods. Typeorm is handling it by itself.

So basically you could simply

@ManyToOne(type => Childhood, childhood => childhood.family)
@Field({type: () => Childhood})
childhood: Childhood;

If you’d need to add some custom logic you could do this like:

    @Field({type: () => Childhood})
    async childhood() {
        return Childhood.createQueryBuilder().relation("family").of(this).loadOne();
        // or some custom logic
    };

But it might not be needed propably in your case. In case you’d like to learn more about relational queries: http://typeorm.io/#/relational-query-builder

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hibernate One to Many Annotation Tutorial - Baeldung
This quick Hibernate tutorial will take us through an example of a one-to-many mapping using JPA annotations, an alternative to XML.
Read more >
The best way to map a @OneToMany relationship with JPA ...
There are many ways to map the @OneToMany association. We can use a List or a Set. We can also define the @JoinColumn...
Read more >
OneToMany not creating a column in table - Stack Overflow
I have no idea how this can happen. Maybe you need to add mapped_by in your OneToMany Annotation: @OneToMany(mappedBy = "season", fetch = ......
Read more >
Best Practices for Many-To-One and One ... - Thorben Janssen
Best Practices for Many-To-One and One-To-Many Association Mappings · 1 Don't use unidirectional one-to-many associations · 2 Avoid the mapping of huge to-many ......
Read more >
Many-to-one / one-to-many relations - typeorm - GitBook
Many-to-one / one-to-many is a relation where A contains multiple instances of B, but B contains only one instance of A. Let's take...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found