I want to camelize field and argument names, I thought it was simple, just renaming the snake-style fields to camel-style. So I wrote a custom module as the following:
export class SnakeToCamelFieldModule implements PipelineModule {
private snakeToCamelFieldSchema = new SnakeToCamelFieldSchema();
transformSchema(schema: GraphQLSchema): GraphQLSchema {
return transformSchema(schema, this.snakeToCamelFieldSchema);
}
}
function isSpecificCase(s: string, converter: (value: string, locale?: string) => string ) {
return s === converter(s);
}
class SnakeToCamelFieldSchema implements SchemaTransformer {
private convertMap: { [key: string]: string; } = {};
transformField(field: GraphQLNamedFieldConfig<any, any>, context: any) {
// Rename a field in a type
if (isSpecificCase(field.name, snakeCase)) {
const camelName = camelCase(field.name);
this.convertMap[camelName] = field.name;
return {
...field,
name: camelName
};
}
return field;
}
renameField(name: string): string {
return this.convertMap[name] || name;
}
}
The schema was exactly what I want, but I also need to rename the query back. Then I updated FieldNode's name, but it failed and I got Failed to determine type of SelectionSet node. It seems like some validations called before executing the query.
Then I thought maybe graphql aliases could help, so I updated the code as the following:
It only partly succeeded, no errors occured this time, some queries like { getUsername } succeeded, it returned { "data": { "getUsername": "haofuxin" } }. But some queries like
I want to camelize field and argument names, I thought it was simple, just renaming the snake-style fields to camel-style. So I wrote a custom module as the following:
The schema was exactly what I want, but I also need to rename the query back. Then I updated
FieldNode
'sname
, but it failed and I gotFailed to determine type of SelectionSet node
. It seems like some validations called before executing the query. Then I thought maybegraphql aliases
could help, so I updated the code as the following:It only partly succeeded, no errors occured this time, some queries like
{ getUsername }
succeeded, it returned{ "data": { "getUsername": "haofuxin" } }
. But some queries likereturned
isActive
was missing. After diving in for a while, I found thatisActive
was filtered out in https://github.com/graphql/graphql-js/blob/ed74228dc8540e3d2681cb6da473e7ce5edb7850/src/execution/execute.js#L649-L655 .fieldName
isis_active
rather thanisActive
.