Alan
Alan

Reputation: 10125

Querying NOT NULL GraphQL with Prisma

Schema:

type TrackUser {
  id: ID! @unique
  createdAt: DateTime!
  user: User #note there is no `!`
}
type User {
  id: ID! @unique
  name: String! @unique
}

I want to get Alls TrackUser where User is not null. What would be the query?

Upvotes: 14

Views: 31926

Answers (2)

Alan
Alan

Reputation: 10125

this works, but I guess it is just a hack..

query TrackUsersQuery($orderBy: TrackUserOrderByInput!, $where: TrackUserWhereInput, $first: Int, $skip: Int) {
  trackUsers(where: $where, orderBy: $orderBy, first: $first, skip: $skip) {
    id
    createdAt
    user {
      id
      name
    }
  }
}


variables = {
  where: {
    user: {
      name_contains: ''
    }
  }
}

UPDATE:

For Prisma2, here you have the possibilities:

For products that have no invoice, you can use the following:

const data = await prisma.product.findMany({
    where: {
      invoices: {
        none: {
          id: undefined,
        },
      },
    },
})

And for Invoices that do not have a product associated:

const data = await prisma.invoice.findMany({
    where: {
      productId: null,
    },
})

more details here: https://github.com/prisma/prisma/discussions/3461

Upvotes: 1

Matthias Oertel
Matthias Oertel

Reputation: 889

This would be a possible query:

query c {
  trackUsers(where: { NOT: [{ user: null }] }) {
    name
  }
}

Here you can see how it looks in the Playground. I added a name to Trackuser in the datamodel in order to be able to create it from that side without a user.

enter image description here

Upvotes: 22

Related Questions