javascript – GraphQL: Problem with case convertion in queries/mutations

I am using Postgres for my database, Knex to build the database, Graphql to run queries and mutations and Vue.js in front-end.

The problem is, i need to show the values from the table(s) in the front-end, but i am having problems with case conversion, my DB uses snake_case, but the API uses camelCase.

cotacoesLoad: async (_root, _args, ctx, _info) => {
    return ctx.database('cotacoes')
  },

i’m also using a field resolver to bring data from a related table:

cliente: async (root, _args, ctx, _info) => {
    return ctx.database('erp_clientes').where({ id: root.cliente_id }).first()
  }

this is how I typed the table and the query in graphql:

type Cotacao {
  id: String!
  cliente: ErpCliente!
  situacao: Int
  localEntrega: Int
  amostraInicial: String
  lotePiloto: String
  producao: String
  ferramental: String
  dataEnvio: LocalDate
  nrCotacaoCliente: Int
}

extend type Query {
  cotacoesLoad: [Cotacao!]!
}

the result of the query in playground:

{
  "data": {
    "cotacoesLoad": [
      {
        "cliente": {
          "nome": "mestre"
        },

        "situacao": 1,
        "localEntrega": null,
        "amostraInicial": null,
        "lotePiloto": null,
        "producao": "produ",
        "ferramental": "a",
        "dataEnvio": null,
        "nrCotacaoCliente": null
      }
    ]
  }
}

the issue is: fields that are represented with more than 1 word (example: localEntrega) are returning null, probally because there is no case convertion and the DB will only recognize single-word fields.

I can’t use snake case in my API, so i ask, what would be the best way to convert those camelCased fields to snake_case fields?

i’ve tried to just write the fields in snake_case but I know this isn’t the right way to do.

other thing i’ve done was a file to rename all these camelCased fields to snake_case in a ‘fields’ file like this:

localEntrega: root => {
    return root.local_entrega
  }

but it was not accepted because its also a bad way to do it.

Read more here: Source link