Hey, I’ve just posted this in the forum but wonder...
# orm-help
j
Hey, I’ve just posted this in the forum but wondering if anybody here could help too: https://www.prisma.io/forum/t/relation-doesnt-exist-on-type/5636 How can I access a relation on a type on my server? I’m trying to run:
Copy code
const recommended: Class[] = await prisma
      .classes({
        // checks
      })
      .$fragment(`
      fragment Example on Class {
        id
        heroImage { // This is the relation
          public_id
        }
      }
`);
However, when I then loop through
recommended
and try to access
heroImage
I get
Property 'heroImage' does not exist on type 'Class'
. When I try to access a single
class
it works fine, e.g:
Copy code
const example = await prisma.class({ id: "123" }).heroImage();

const exampleHeroID = example.public_id
Any help or pointers to the right docs would be greatly appreciated
b
This is pretty basic, but have you console.logging the resulting array from the call to prisma?
It could be initializing recommended as the actual Class type definition, rather than type checking and using the actual class implementation
It may also be the fact that you have defined heroImage as a function in the class, rather than a data field
j
It returns and array of:
Copy code
{ title: 'Class Two Example', heroImage: null, duration: 1900 }
Yeah the interface for
Class
doesn’t include `heroImage`:
Copy code
export interface Class {
  id: ID_Output;
  title: String;
  lowerTitle?: String;
  createdAt: DateTimeOutput;
  updatedAt: DateTimeOutput;
  description?: String;
  active: Boolean;
  focus?: String;
  duration?: Int;
  videoURL?: String;
  featured?: Boolean;
  image?: String;
  difficulty?: DIFFICULTY;
}
In my schema it looks a little like:
Copy code
type Class {
 id: ID!
 heroImage: File
}

type File {
 id: ID
 public_id: String
}
b
Thanks for sending those! So in the Class interface, add the heroImage with corresponding type (in this case:
Copy code
{...
  heroImage: object,
...}
to the interface definition. Also, make sure that you have not defined heroImage as a function. In you example that works, you are calling heroImage as a function, which as far as I know is not support in the graphql spec/primsa, but I could be wrong. Does that help?
j
Hey I think so, I’m just using the the interface that’s generated from prisma-client. I think my problem is that I hadn’t upgraded the backend in quite a while (was running 1.15.x) and everything seems quite different now. I’ve managed to get what I need by running the native graphql query with:
Copy code
const example = await prisma
 .$graphql(`
        query recommendedClasses {
          classes {
            id
            title
            duration
            heroImage {
              public_id
            }
          }
        }`
)
When I look at what’s generated from prisma-client I can see that heroImage is a function, previously this didn’t matter and I could access it. Not sure if I’ve done something wrong as now I need to update a lot of my code.
b
Yeah, I have been working mostly with GraphQL in the context of writing the server queries by hand, and just have a working knowledge of the prisma ecosystem, so I cannot say for sure.
j
I’ve ended up moving back to using prisma-bindings for my core api server, however, using the code output that’s generated from prisma-client and then using the output from prisma-client directly in my worker servers . Seems like the quickest way to get things working again.