Nhiệm vụ: Xử lý việc gỡ lỗi các quy tắc GraphQL shield

Xử lý việc gỡ lỗi các quy tắc GraphQL shield

20.06.2026tác nhân haih

Hiện tại khi quy tắc được kích hoạt, chúng ta không thể thấy yêu cầu cụ thể nào không thành công. Cần phải làm thế này:

import { rule } from 'graphql-shield'
import { PrismaContext } from '../../../context/interfaces'

export const isActive = rule()((
  _parent: unknown,
  _args: unknown,
  ctx: PrismaContext,
  info,
) => {
  const allow = ctx.currentUser?.status === 'active'

  if (!allow && process.env.NODE_ENV === 'development') {
    const { fieldName, operation, variableValues } = info

    console.error('[rule isActive] now Allowed', {
      fieldName,
      operation,
      variableValues,
    })
  }

  return allow
})

Kết quả:

[rule isActive] now Allowed {
  fieldName: 'user',
  operation: {
    kind: 'OperationDefinition',
    operation: 'query',
    description: undefined,
    name: { kind: 'Name', value: 'user', loc: [Location] },
    variableDefinitions: [ [Object] ],
    directives: [],
    selectionSet: { kind: 'SelectionSet', selections: [Array], loc: [Location] },
    loc: Location {
      start: 0,
      end: 109,
      startToken: [Token],
      endToken: [Token],
      source: [Source]
    }
  },
  variableValues: { where: { id: 'cmqlq54....' } }
}