Weakky/ra-data-opencrud

Update isn't working

Closed this issue · 4 comments

When I try to update a data from my existing Prisma database (Not e-commerce example), I'm having an error.

I can create a data but I can't update it.

Reason: 'createdAt' Field 'createdAt' is not defined in the input type 'ProductUpdateInput'. (line 1, column 24):
Reason: 'updatedAt' Field 'updatedAt' is not defined in the input type 'ProductUpdateInput'. (line 1, column 24):

From what I understand, ra-data-opencrud is trying to update the updatedAt and/or createdAt fields. Prisma docs states that createdAt and updatedAt fields can't be updated via mutations.

https://www.prisma.io/docs/reference/service-configuration/data-model/data-modelling-(sdl)-eiroozae8u#example

The id, createdAt and updatedAt fields are managed by Prisma and read-only in the exposed GraphQL API (meaning they can not be altered via mutations).

Here is the full error

Warning: Missing translation for key: "GraphQL error: Variable '$data' expected value of type 'ProductUpdateInput!' but got: {"image":null,"title":"Tittt","meta":"Met","description":"Desc","price":13,"code":"ASD","stock":123,"designer":{"connect":{"id":"cjgsgsl141l630b59t3oniy9x","username":"batuhanw"}},"createdAt":"2018-11-10T18:59:31.000Z","updatedAt":"2018-11-10T18:59:31.000Z"}. Reason: 'updatedAt' Field 'updatedAt' is not defined in the input type 'ProductUpdateInput'. (line 1, column 24):
mutation updateProduct($data: ProductUpdateInput!, $where: ProductWhereUniqueInput!) {
                       ^
GraphQL error: Variable '$data' expected value of type 'ProductUpdateInput!' but got: {"image":null,"title":"Tittt","meta":"Met","description":"Desc","price":13,"code":"ASD","stock":123,"designer":{"connect":{"id":"cjgsgsl141l630b59t3oniy9x","username":"batuhanw"}},"createdAt":"2018-11-10T18:59:31.000Z","updatedAt":"2018-11-10T18:59:31.000Z"}. Reason: 'createdAt' Field 'createdAt' is not defined in the input type 'ProductUpdateInput'. (line 1, column 24):
mutation updateProduct($data: ProductUpdateInput!, $where: ProductWhereUniqueInput!) {

@Weakky If you can point me where I can fix this, I'm up for creating a pull request for this one!

I have same issue. @BatuhanW did you find any workaround?

I fixed by using buildQuery

const enhanceBuildQuery = buildQuery => introspectionResults => (
  fetchType,
  resourceName,
  params,
) => {
  const fragment = get(overridenQueries, `${resourceName}.${fetchType}`);

  if ((fetchType === UPDATE || fetchType === CREATE) && params.data) {
    if (params.data.updatedAt) {
      delete params.data.updatedAt;
    }
    if (params.data.createdAt) {
      delete params.data.createdAt;
    }
  }

  return buildQuery(introspectionResults)(
    fetchType,
    resourceName,
    params,
    fragment,
  );
};
....
....
....
buildPrismaProvider({
      client,
      buildQuery: enhanceBuildQuery(buildQuery),
    }).then(dataProvider => this.setState({ dataProvider }));

Thanks for raising the initial issue and for posting the fix folks :) Heres my simplified snippet based on @abdullah-almesbahi 's post.

const enhanceBuildQuery = introspection => (fetchType, resource, params) => {
  if ((fetchType === 'UPDATE' || fetchType === 'CREATE') && params.data) {
    if (params.data.updatedAt) {
      delete params.data.updatedAt;
    }
    if (params.data.createdAt) {
      delete params.data.createdAt;
    }
  }
  return buildQuery(introspection)(fetchType, resource, params);
};
// ...
buildOpenCrudProvider({
  buildQuery: enhanceBuildQuery
}