graphql-nexus / nexus-plugin-prisma

Deprecated
MIT License
828 stars 118 forks source link

Use findOne inside a t.crud definition? Access Context for currentUserID #512

Open ScottAgirs opened 4 years ago

ScottAgirs commented 4 years ago

Hello folks,

Cannot seem to find a fitting example in nexus docs or figure out a way myself to solve this.

The goal is to expose a "currentUser" query to GraphQL API, which would retrieve a single user with .findOne by accessing the ctx.request.currentUserId.

Current workaround I have is this

t.list.field('currentUser', {
      type: 'User',
      resolve: async (_, args, ctx) => {
        const singleUser = await ctx.photon.users.findMany({
          where: { id: '1' },
        })

        return singleUser
      },
    })

which uses t.list.field(), but for obvious reasons this is not ideal, because even though only one user is returned, it is still a list and needs to be destructed on the client.

Bjoernstjerne commented 4 years ago

using this in my project:

t.field('currentUser', {
      type: 'User',
      resolve: async (_, args, ctx) => {
        const singleUser = await ctx.photon.users.findOne({
          where: { id: '1' },
        })

        return singleUser
      },
    })