Paul Hendrickson
10/29/2020, 7:42 PMmodel customer{
id Int
addressLine1 String
addressLine2 String
...
# more fields here
...
}
and I’m trying to return address as one line in my GraphQL Type. So basically trying to turn two columns in a DB to a single scalar type in GraphQL. This is what I have right now:
export const customer = objectType({
name: "customer",
definition(t) {
t.field("address", {
type: "String",
resolve: (root) => {
return root.addressLine1 + " " + root.addressLine2;
},
})
...
//more fields here
...
})
}
When I run the query I get back the response I was expecting, but typescript is telling me Property 'addressLine1' does not exist on type '{ (lists fields of other type) }'
so I thought of type typecasting the root argument in my resolve
with an interface that matched my prisma model.
interface customerSchema{
id: number
addressLine1: String
addressLine2: String
...
// more fields here
...
}
export const customer = objectType({
name: "customer",
definition(t) {
t.field("address", {
type: "String",
resolve: (root: customerSchema) => {
return root.addressLine1 + " " + root.addressLine2;
},
})
...
//more fields here
...
})
}
Which made that error go away but I’m instead getting a typescript error that says Type '(root: customerSchema => string' is not assignable to type 'FieldResolver<"Customer", "address">'.
I’m getting my expected answer when I query for address, but I’m worried I’ll run into issues in the future if I continue to use this design pattern. How should I combine two columns in a DB to output as one field in GraphQL?Ryan
10/30/2020, 6:31 AMconst Customer = objectType({
name: 'customer',
definition(t) {
t.model.id()
t.model.addressLine1()
t.model.addressLine2()
t.field('address', {
type: 'String',
resolve(parent) {
return `${parent.addressLine1} ${parent.addressLine2}`
},
})
},
})
If you add those then TypeScript will not complainPaul Hendrickson
10/30/2020, 1:09 PM