简体   繁体   中英

How can I get mapped type names for a graphQL type using type-graphql

Okay, so I'm starting to dig into graphql a little bit, and I've built an api using koa, type-graphql, and sequelize-typescript. Everything works pretty well.... I managed to get a query working, and even managed to optimize a little bit by using graphql-fields to filter the columns I query in the database... However when I've aliased a field name, I can't seem to get the mapped name.....

For example, given the following ObjectType/Sequelize Model....

export interface IDepartment {
    departmentId: number;
    name: string;
    description: string;
}

@ObjectType()
@Table({ underscored: true })
export class Department extends Model<Department> implements IDepartment {
    @Field({ name: 'id' })
    @PrimaryKey
    @Column({ field: 'department_id'})
    public departmentId: number;

    @Field()
    @Length({ max: 100 })
    @Column
    name: string;

    @Field()
    @Length({ max: 100 })
    @AllowNull
    @Column
    description: string;
}

and sample query....

    query {
    department(name: "Test Dept") {
        id
        name,
        description
    }
}

sample resolver...

async department(@Arg('name') name: string, @Info() info: GraphQLResolveInfo) {
        return Department.findOne({
            where: { name }
        });
    }

This works just fine.... but when I do

async department(@Arg('name') name: string, @Info() info: GraphQLResolveInfo) {
    let fields = Object.keys(getFields(info))
    return Department.findOne({
        attributes: fields,
        where: { name }
    });
}

(getFields is graphql-fields), it fails because the query specified field name id, which is what graphql-fields returns, but the column name is department_id (sequelize model name departmentId).

I've gone through the schema with a fine tooth comb, using the introspectionFromSchema function to see a detailed copy of my schema, but nowhere is there a mention of departmentId or department_id.... However I know it's out there somewhere because when I exclude the attributes field from my sequelize query, even though sequelize returns departmentId as the property name, when I return it from my resolver and it reaches the client, the property name is id.

Any help would be appreciated.... I'm trying to optimize everything by only fetching requested properties and not the entire object. I could always store the maps as separate constants and use those in my @Field definition, but I want to do that as a last resort, however if I can I'm trying to keep the code as lean as possible....

Thank you all in advance.

Unfortunately, the name option was introduced mostly to support resolvers inheritance. Using this for mapping the schema field names is a kinda undocumented feature so it's doesn't provide any mapping or exposing mapping metadata.

Using the name option for input or args types will be even worse - it will result in no access to the fields and the properties being undefined.

For now my recommendation is to just keep it simple and don't map the field names until a proper fix arrives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM