Hello guys! We use `@nexus/schema@0.14.0` with `@p...
# graphql-nexus
i
Hello guys! We use
@nexus/schema@0.14.0
with
@prisma/client@2.1.3
and have order model with related transaction field
Copy code
model Order {
  ...
  transaction Transaction
}
in Nexus API, it is exposed as
Copy code
export const Order = objectType({
  name: 'Order',
  definition(t) {
    ...
    t.model.transaction()
  },
})
Now in one of the queries we want to change some data on the transaction for the returned order. However, Nexus ignores the transaction field on the returned order object and always resolves it from the prisma model. The quick workaround was to duplicate the model and change it for the query, but it's awkward, requires changing schema derived TS types on frontends and breaks fronted Apollo caching for different types.
Copy code
export const CustomOrder = objectType({
  name: 'CustomOrder',
  definition(t) {
    ... // t.model('Order').xxx() 
    t.field('transaction', { type: 'Transaction' })
  },
})

export const order = queryField('order', {
  type: 'CustomOrder',
  args: {
    id: idArg({ required: true }),
  },
  resolve: async (_root, { id }, ctx) => coerceOrderTransaction(await getOrder(ctx, id)),
})
Is there any simple way to tell Nexus that we provide the resolved transaction field on given order query?
r
Hey @iki 👋 Currently the workaround that you have specified is the only way to perform this. I would suggest opening a feature request here for the above.
i
Thanks @Ryan! Used custom resolver and created a helper
Copy code
import { CustomFieldResolver } from 'nexus-plugin-prisma/typegen'

export const allowOverride = <
  TypeName extends string = string,
  FieldName extends string = string
>(
  customResolve?: CustomFieldResolver<TypeName, FieldName>,
): CustomFieldResolver<TypeName, FieldName> => (
  root,
  args,
  ctx,
  info,
  originalResolve,
) => {
  // logger.debug(`${info.parentType || ''}.${info.fieldName}`, root, args)
  return (
    root[info.fieldName] ??
    (customResolve
      ? customResolve(root, args, ctx, info, originalResolve)
      : originalResolve(root, args, ctx, info))
  )
}
to simply allow relation field override on model
Copy code
export const Order = objectType({
  name: 'Order',
  definition(t) {
    ...
    t.model.transaction({ resolve: allowOverride() })
  },
})
🙌 2
💯 2