[nexus] how can i write resolver for enum type fie...
# orm-help
t
[nexus] how can i write resolver for enum type field? I have error when trying to do so
Copy code
import { objectType } from '@nexus/schema'

export const PaymentMethod = objectType({
    name: 'PaymentMethod',
    definition(t) {
        t.model.id()
        t.model.user()
        t.model.userId()
        t.model.method({
            resolve: async (root, args, ctx, info, originalResolve) => {
                const isAuthorized = await ctx.authorization.byUserId.load(root.userId)
                return isAuthorized
                    ? originalResolve(root, args, ctx, info)
                    : 'UNAUTHORIZED'
            },
        })
        t.model.reference({
            resolve: async (root, args, ctx, info, originalResolve) => {
                const isAuthorized = await ctx.authorization.byUserId.load(root.userId)
                return isAuthorized
                    ? originalResolve(root, args, ctx, info)
                    : 'unauthorized'
            },
        })
    },
})
//========================
model PaymentMethod {
  id        Int            @id @default(autoincrement())
  user      User           @relation(fields: [userId], references: [id])
  userId    String         @unique
  method    PAYMENT_METHOD
  reference String
}
//==================
enum PAYMENT_METHOD {
  UNAUTHORIZED
  CARD
  BANK
  PAYPAL
}
error message
i cannot define the resolver for method (which is a enum type)
r
@tylim 👋 Do you need to perform any authorization if the field is accessible or not?
t
Hello Ryan, I want to stop unauthorized user from viewing the enum data
r
For this specific use case, I would suggest using GraphQL Shield Taking your schema as an example:
Copy code
model User {
  id             String          @id @default(uuid())
  paymentMethods PaymentMethod[]
}

model PaymentMethod {
  id        Int             @id @default(autoincrement())
  user      User            @relation(fields: [userId], references: [id])
  userId    String          @unique
  method    PAYMENT_METHOD?
  reference String?
}

enum PAYMENT_METHOD {
  UNAUTHORIZED
  CARD
  BANK
  PAYPAL
}
And the Nexus object:
Copy code
const User = ns.objectType({
  name: 'User',
  definition(t) {
    t.model.id()
    t.model.paymentMethods()
  },
})

const PaymentMethod = ns.objectType({
  name: 'PaymentMethod',
  definition(t) {
    t.model.id()
    t.model.user()
    t.model.method()
    t.model.reference()
  },
})
I can create an auth rule like this: (you can add any custom auth logic here and return `true`/`false` based on the auth logic.
Copy code
import { shield, rule } from 'graphql-shield'
import { Context } from '../types'

export const rules = {
  isAuthenticatedUser: rule({ cache: 'contextual' })(
    (_parent, _args, ctx: Context) => {
      // auth logic will come here
      return false
    }
  ),
}

export const permissions = shield({
  PaymentMethod: {
    method: rules.isAuthenticatedUser,
    reference: rules.isAuthenticatedUser,
  },
})
And this will be added in my schema here:
Copy code
new ApolloServer({
    schema: applyMiddleware(await createSchema(), permissions),
    ...rest of the options
})
This will return
null
for the related fields accessed by the client 🙂
Also take a note that the fields should be nullable in the schema as well so that the fields can return
null
when unauthorized.
t
thanks for the examplecode, i will study the library first
💯 1