Hi guys, how can I create a custom resolver functi...
# prisma-whats-new
m
Hi guys, how can I create a custom resolver function for a nested parameter? I have a prisma backend with some custom resolvers. Multiple data types point to another data type like so.
Copy code
type Image {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  ...
}
type Story {
id: ID! @unique
createdAt: DateTime!
updatedAt: DateTime!
image: Image!
}
type Post {
id: ID! @unique
createdAt: DateTime!
updatedAt: DateTime!
image: Image!
}
Now, I will only access the
Image
type as a nested parameter through the other (story, post) endpoints. However, I still would like to define a custom resolver on the server side that allways is called when another resolver accesses the image to return a default image in certain cases. Where/how would I define this? Thanks! PS: When doing this only for one resolver it works fine like this:
Copy code
import { Context } from "../utils";

export const Story = {
  image: async (parent, args, ctx: Context, info) => {
  return ...
  }
};
However, I would like to apply this custom resolver to all images, not just the one being called from Story.
n
I suggest to ask questions that need a lot of context in the Forum instead: https://www.graph.cool/forum/c/questions.
r
So in my previous project, we solved this as following :
Copy code
export default {
  Query: {
    getPageOfUsers,
    user
  },
  User: {
    email: resolveOwnerOnly,
    lastname: resolveOwnerOnly,
    avatar: resolveOwnerOnly,
    properties: (...rest) => resolveOwnerOnly(...rest, properties)
  },
  Mutation: {
    updateUser
  }
}
where
properties
is another query resolver and then somewhere else :
Copy code
export async function resolveOwnerOnly (root, args, context, info, next) {
  if (!context.viewer) return new Error('Not authenticated')

  const userId = root.userId || root.id || context.userId

  if (
    (!userId && !isAdmin(context.viewer)) ||
    (userId && !isAdminOrOwner(context.viewer, userId))
  ) return new Error(`Not authorized: only ${info.parentType} owner can have access to ${info.fieldName}`)

  return next ? next(root, args, context, info) : root[info.fieldName]
}