hasura / ra-data-hasura

react-admin data provider for Hasura GraphQL Engine
MIT License
337 stars 70 forks source link

Edit Page sends id as string, hasura expects Int #50

Open webdeb opened 3 years ago

webdeb commented 3 years ago

I've a basic entity (labels) with an id of type integer. When I go to the edit page #/labels/1 This query is then produced:

query labels($limit: Int, $where: labels_bool_exp) {
  returning: labels(limit: $limit, where: $where) {
  id
  label
}}

variables: {where: {id: {_eq: "1"}}, limit: 1}

As you can see id is treated as a string.

And Hasura responds with:

extensions: {path: "$.selectionSet.labels.args.where.id._eq", code: "validation-failed"}
message: "expected a 32-bit integer for type "Int", but found a string"
Rigel772 commented 3 years ago

Same here! Do you know how to fix this?

webdeb commented 3 years ago

hmm, well I copied src and fixed some files locally. To fix it you basically have to cast the id to the correct type, so basically parseInt. My code is here:

const castIdType = (params, resource) => {
  const id = params.id
  const target = params.target

  if (!id) return

  const key = target ?? "id"
  const typedParam = resource.type.fields.find(f => key === f.name)
  if (!typedParam) return

  if (getFinalType(typedParam.type).name === "Int") {
    params[key] = parseInt(params[key])
  }
}

const buildVariables = introspectionResults => (
  resource,
  aorFetchType,
  params,
  queryType
) => {
  // cast the id before using in the switch..
  castIdType(params, resource)

Its not best code at all, but it works for me, so if it helps someone else, I am glad to help ;)

Rigel772 commented 3 years ago

Any working code is better than "best" but buggy ;) Thanks for your help but it is still beyond my JS skills... could you tell me some more details where to paste it and where to execute? What do you mean by "fixed some files"? I can see only ra-data-hasura.js

webdeb commented 3 years ago

@Rigel772 I've copied the whole src directory of this package into my code base, and imported it instead of "ra-data-hasura", I had to edit the buildVariables.js file in the main exported function which I called buildVariables

Rigel772 commented 3 years ago

Makes sense, Thank you

bhaskar20 commented 3 years ago

Can we have a solution for this

fentas commented 3 years ago

https://github.com/hasura/ra-data-hasura/issues/50#issuecomment-794620383

This works. Can we add this to the default buildVariables function or into buildQueryFactory?

arjunyel commented 2 years ago

@webdeb hi friend can you open a PR with your changes? Would this affect people whose id type is a string? Thank you!