Search code examples
rubygraphqlgraphql-ruby

How can I override the camelCase naming convention in graphql-ruby?


I'm using the excellent graphql-ruby gem (http://graphql-ruby.org/). I have a data structure that stores i18n strings essentially as hashes: {'en': 'Hello', 'es': 'Hola'}. Works great under normal circumstances.

However, we just added simplified and traditional Chinese with the language codes of 'zh-CN' and 'zh-TW'. Due to graphql-ruby translating all input fields in camelCase, these are getting translated as 'zh-cn' and 'zh-tw'.

This is what my input class looks like:

class CoreGql::InputTypes::I18nStringInput < GraphQL::Schema::InputObject
  graphql_name "I18nStringInput"

  argument :en, String, :required=>false
  argument :es, String, :required=>false
  argument :de, String, :required=>false
  argument :fr, String, :required=>false
  argument :is, String, :required=>false
  argument :ja, String, :required=>false
  argument :nl, String, :required=>false
  argument 'zh-CN', String, :required=>false
  argument 'zh-TW', String, :required=>false
end

Is there a way to override the graphql-ruby naming convention for fields and/or arguments? I want it to be 'zh-CN' and 'zh-TW' exactly.


Solution

  • argument :'zh-CN', String, :required=>false, :camelize => false can dump schema you described. However, on GraphiQL, it gives errors:

    Error: Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "zh-CN" does not.
        at assertValidName (http://localhost:3000/assets/graphiql/rails/graphiql-0.11.11.self-8e737fccee2b9e37d473d51a9032299b320e847f04671cb2030e717b0009b2c1.js?body=1:30302:11)
        at http://localhost:3000/assets/graphiql/rails/graphiql-0.11.11.self-8e737fccee2b9e37d473d51a9032299b320e847f04671cb2030e717b0009b2c1.js?body=1:28613:44
        at Array.forEach (<anonymous>)
        at GraphQLInputObjectType._defineFieldMap (http://localhost:3000/assets/graphiql/rails/graphiql-0.11.11.self-8e737fccee2b9e37d473d51a9032299b320e847f04671cb2030e717b0009b2c1.js?body=1:28612:16)
        at GraphQLInputObjectType.getFields (http://localhost:3000/assets/graphiql/rails/graphiql-0.11.11.self-8e737fccee2b9e37d473d51a9032299b320e847f04671cb2030e717b0009b2c1.js?body=1:28601:49)
        at typeMapReducer (http://localhost:3000/assets/graphiql/rails/graphiql-0.11.11.self-8e737fccee2b9e37d473d51a9032299b320e847f04671cb2030e717b0009b2c1.js?body=1:29968:26)
        at Array.reduce (<anonymous>)
        at http://localhost:3000/assets/graphiql/rails/graphiql-0.11.11.self-8e737fccee2b9e37d473d51a9032299b320e847f04671cb2030e717b0009b2c1.js?body=1:29961:36
        at Array.forEach (<anonymous>)
        at typeMapReducer (http://localhost:3000/assets/graphiql/rails/graphiql-0.11.11.self-8e737fccee2b9e37d473d51a9032299b320e847f04671cb2030e717b0009b2c1.js?body=1:29954:27)
    

    I feel it's better to use name like zh_CN with camelize: false