-
When I want to rewrite company.name in a queryField as shown below, objectType's resolve is executed and the rewritten value is not reflected. As a workaround for now, I have solved this problem by defining a separate field that avoids the original name (in this case, company), but this is very inconvenient. I would like to disable resolve so that I can return the state processed by queryField as is. Is there such an option or method? import { queryField, objectType } from "nexus"
import { User, Company } from "nexus-prisma"
export const UserQueryField = queryField((t) => {
t.field("user", {
type: User.$name,
async resolve(_root, args, ctx) {
const user = await prisma.user.findFirst({
include: {
company: true,
}
})
// rename company name
const result = {
...user,
company: {
name: "Rename company",
}
}
return result
},
})
})
export const UserModel = objectType({
name: User.$name,
description: User.$description,
definition(t) {
t.field(User.id)
t.field(User.name)
t.field(User.companyId)
t.field(User.company)
// Error: 'resolve' is declared here.
t.field("company", { type: Company.$name })
// If the field name is set to something other than the original (company),
// the error disappears and the renamed value is reflected.
t.field("customCompany", { type: Company.$name })
},
})
export const CompanyModel = objectType({
name: Company.$name,
description: Company.$description,
definition(t) {
t.field(Company.id)
t.field(Company.name)
},
}) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This sort of defeats the purpose of GraphQL which, by design, is intended to save resources by only fetching what it needs based on the front-end's field selection. Though I understand the unique issue you are trying to solve. You can remove the However, I don't recommend doing that since again it defeats one of the key advantages to GraphQL. Instead, you should only return each level at a time in each resolver rather than returning sub-levels within a single resolver. Doing this though will result in the In a nutshell, you'll want to do something like this: import { queryField, objectType } from "nexus"
import { User, Company } from "nexus-prisma"
export const UserQueryField = queryField((t) => {
t.field("user", {
type: User.$name,
async resolve(_root, args, ctx) {
const user = await ctx.prisma.user.findFirst() // <-- Remove your sub-selection and just query the top-level user
return user
},
})
})
export const UserModel = objectType({
name: User.$name,
description: User.$description,
definition(t) {
t.field(User.id)
t.field(User.name)
t.field(User.companyId)
t.field("company", {
type: Company.$name
resolve: async (source, args, ctx) => {
// Use 'findUnique' to get a reference to your source record
const company = await ctx.prisma.user.findUnique({
where: {
id: source.id
}
}).company() // <-- At the tail-end, prior to resolving the promise, call 'company()' which causes the entire query to resolve as the company record related to your source user
return {
...company,
name: "Rename company" // <-- Now you can rename the company here in your field resolver instead of doing it in the root resolver
}
}
})
},
})
export const CompanyModel = objectType({
name: Company.$name,
description: Company.$description,
definition(t) {
t.field(Company.id)
t.field(Company.name)
},
}) |
Beta Was this translation helpful? Give feedback.
This sort of defeats the purpose of GraphQL which, by design, is intended to save resources by only fetching what it needs based on the front-end's field selection. Though I understand the unique issue you are trying to solve. You can remove the
Error: 'resolve' is declared here.
errors by defining asourceType
forUser
and declarecompany
as a direct property on the source type. Then Nexus will know to expect the property on the source type that gets returned by the root resolver.However, I don't recommend doing that since again it defeats one of the key advantages to GraphQL. Instead, you should only return each level at a time in each resolver rather than returning sub-levels within a…