#GraphQL Entity Return Fields

8 messages · Page 1 of 1 (latest)

sharp condor
#

Something that I don't really understand, my DB returned status_id and GraphQL expected for statusId so I used the name option in the entity field to transform the data. But, it still returned as status_id

Is this something that I did wrong?

@ObjectType()
export class FooEntity {
    @Field(() => Int)
    id: number;

    @Field(() => Int, { name: 'statusId' })
    status_id: number;
}
lavish rock
#

I believe the name property is for documentation/ introspection purposes, just like description. There is no extra transforming that I know of.

sharp condor
#

@lavish rock Hmm, but the name property is exactly what is reflected in Graphql

heavy girder
#

I just tested, and it works as you expect:

@ObjectType('Account')
export class AccountModel {
  @Field(() => String) id!: string;

  @Field(() => String, { nullable: true, name: 'accountName' })
  name!: string | null;
}

outputs

type Account {
  accountName: String
  id: String!

and can be queried like so:

query me {
    me {
        ownedAccounts {
            id
            accountName
        }
    }
}

Even though the database returns an object with name attribute instead of accountName. No manual conversion needed.

sharp condor
#

@heavy girder May I ask what your DB method type return is?

heavy girder
#

{name: string | null, ... }

sharp condor
#

@heavy girder So you declared to this.usersService.getOne(user.id).ownedAccounts() method type of AccountModel same as your ownedAccounts?

heavy girder
#

Yes. My AccountModel DTO type is very similar to Account entity, which is returned from ownedAccounts() along others.

Having the return type defined on the resolver method gives me some type-safety, so if I removed createdAt from my Account entity, typescript would tell me that I no-longer satisfy the DTO type.