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?

4

2 Answers

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.

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:

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest