Changing details of this because I don’t want to ...
# graphql-nexus
p
Changing details of this because I don’t want to use real company stuff but my PostGresSQLDB has a model like this:
Copy code
model 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:
Copy code
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.
Copy code
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?
r
Hey @Paul Hendrickson 👋 You need to have those defined in your model to access them from the parent:
Copy code
const 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 complain
p
Awesome! I knew I was missing like one little thing! Thank you so much!
🙌 1