graphql-sequelize: Attributes renamed through the 'map' option of attributeFields never get queried

If you use the map option of attributeFields to rename fields, the fields are filtered out and never queried.

The problem appears to be in src\resolver.js in the block processing option.filterAttributes. The raw attribute names from the Sequelize model are always used to determine what attributes to include in the query, instead of the attribute names for the GraphQL type. This happens regardless of whether filterAttributes is set to true or false.

One solution might be to pass a mapAttributeName function in options to resolverFactory that can do the name translation before filtering the attributes. This would be the reverse of the map function passed into attributeFields.

For now I’m going to investigate field renaming at the Sequelize model level, since we’re already abstracted out model and query generation into a library, so switching the level at which renaming occurs should be easy. (We’re using underscores for database field names and camelCase for GraphQL attribute names).

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 17 (8 by maintainers)

Most upvoted comments

@mickhansen The following workaround works for me. This is inside type definition: Before:

fields: attributeFields(Profile, {
    globalId: true,
    map: field => _.camelCase(field)
})

After:

fields: _.each(attributeFields(Profile, {
    globalId: true,
    map: field => _.camelCase(field)
}), (val, key) => {
    val.resolve = (object) => object[_.snakeCase(key)]
})

So this could be done inside of attributeFields or at least documented.