Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can someone help me with Nexus JS when using Relationships? #949

Closed
ConnorMarble opened this issue Jul 17, 2021 · 1 comment
Closed

Can someone help me with Nexus JS when using Relationships? #949

ConnorMarble opened this issue Jul 17, 2021 · 1 comment

Comments

@ConnorMarble
Copy link

So I thought I had Nexus JS figured out, but then I started adding foreign keys and relationships into my postgres database. Nothing complex, or so I thought. All of a sudden, all of my resolvers are throwing errors. I am using prisma but I am staying away from their plugin since it is still in development.

Does anyone have an example project for me to look at? Nexus' examples aren't the best IMO.

Thanks!


The Error:

" The expected type comes from property 'resolve' which is declared here on type 'NexusOutputFieldConfig<"Author", "books">' "


My New Database Prisma Schema With Relationship:

model author {
id BigInt u/id u/default(autoincrement())
name String
age Int
gender String
book book[]
}
model book {
id BigInt id default(autoincrement())
title String
genre String
author_id BigInt
author author relation(fields: [author_id], references: [id])
}

My Nexus Object Types:

export const Author = objectType({
name: "Author",
definition(t) {
t.nonNull.int("id");
t.nonNull.int("age");
t.nonNull.string("name");
t.nonNull.string("gender");
t.list.field("books", {
type: Book,
    });
  },
});

export const Book = objectType({
name: "Book",
definition(t) {
t.string("title");
t.string("genre"),
t.int("id"),
t.int("author_id"),
t.field("author", {
type: Author,
      });
  },
});
@ahmedosama7450
Copy link
Contributor

You need to define custom resolvers for books field on Author type and for author field on Book type

Inside of these custom resolvers, you will always use prisma findUnique to automatically batch all the queries (You can't use findMany because It's not yet batched by prisma data loader, look here).

Look at the code in these examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants