Moritz
04/03/2018, 12:48 PMtype 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: 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.nilan
04/03/2018, 3:24 PMrock-n-node
04/04/2018, 6:48 AMexport 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 :
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]
}