paljs / prisma-tools

Prisma tools to help you generate CRUD system for GraphQL servers
https://paljs.com
MIT License
683 stars 54 forks source link

Wrong PrismaSelect value when dealing with JSON inside GraphQL non prisma.schema node #175

Closed backops-threed closed 3 years ago

backops-threed commented 3 years ago

Versions:

"@paljs/plugins": "~2.11.1"
"@prisma/client": "~2.15.0"

File schema.prisma:

model FeedPost {
    uuid    String    @id
    cover   Json
}

File schema.graphql:

type FeedPost {
  uuid: ID!
  cover: Image
}
type Image {
  URL: String!
}

My PrismaSelect code:

const select = new PrismaSelect(info, {dmmf}).value;

What's right: For this graphql query:

query {
  feedPost {
      uuid
      cover {
        url
      }
  }

const select will be:

{
  select: {
    uuid: true,
    cover: true
  }
}

So, cover, since on prisma.schema is Json returns as cover: true, and after apollo-server will deal with the fields inside cover.

The problem: In my specific case, I need to return pagination data with a different query. So my query for that will be this, with the FeedPost fields inside an edge node:

query {
  feedPosts {
    edges {
      uuid
      cover {
        url
      }
    }
    pagination {
      pageCount
    }
  }
}

With this query, my const select will return this:

{
  select: {
    edges: {
       uuid: true,
       cover: {
         select: {
           url: true
         }
       }
    }
  }
}

Since on Prisma cover is a Json and now I'm trying to select fields inside it, Prisma tries to use cover as a relation and that gives me a error:

Invalid value {
  select: {
    url: true
  }
} of type Json for field cover on model FeedPostCountAggregateOutputType. Expected either true or false
backops-threed commented 3 years ago

Thanks to @AhmedElywa for helping me out on Slack.

Solution When the prisma model is inside another node on GraphQL, we can use valueOf instead of value. Ref: https://paljs.com/plugins/select#valueof

So, on my case, the solution was:

const select = new PrismaSelect(info).valueOf('edges', 'FeedPost');