[bug] createdAt, updatedAt appear twice

I got the error described on TypeScript code generation and StructuredText value types - #2 by m.finamor but I realized that it is caused by models in my production project having duplicate fields for created and updated at. These fields exist both as createdAt/updatedAt and _createdAt/_updatedAt.

I cannot replicate this behavior in a new DatoCMS project so I suspect it is related to the project itself, which was created many months ago.

Troubleshooting

I created a new empty model in both the old and a new project.

The problem is possible to see by just looking at the generated documentation for the model in the CDA playground.

In the old project I can see that my new model (model id: OPHSix_CQRq3yFuzP8ehjg) has these attributes (there are also other attributes but they are the same in the two projects):

_createdAt: DateTime!
_updatedAt: DateTime!
createdAt: DateTime!
updatedAt: DateTime!

But in the new project the model (model id dyNLgLmkQpSynfPKQzcDew) only the ones prefixed with underscore are present:

_createdAt: DateTime!
_updatedAt: DateTime!

After adding the field name and creating records for each model and fetching these models via the REST-API I can see that the old project shows createdAt/updatedAt under attributes as well as under meta while the new project only shows them under meta:

Old project:

{
  "data": {
    "id": "bETbOd1qQRaQVF8lAJptGQ",
    "type": "item",
    "attributes": {
      "name": "A record",
      "updated_at": "2023-11-24T11:06:08.555+01:00",
      "created_at": "2023-11-24T11:06:08.547+01:00"
    },
    "meta": {
      "created_at": "2023-11-24T11:06:08.547+01:00",
      "updated_at": "2023-11-24T11:06:08.555+01:00",
      …
    }
  }
  …
}

New project:

{
  "data": {
    "id": "Xd2hXXq4TfuknnSzJn9o5Q",
    "type": "item",
    "attributes": {
      "name": "A record"
    },
    "meta": {
      "created_at": "2023-11-24T10:09:36.048+00:00",
      "updated_at": "2023-11-24T10:09:36.057+00:00",
      …
    }
  }
  …
}

I thought the error may depend on unapplied updates or plugins so I forked a new environment in the old project, activated all available updates, removed all plugins and recreated the model but with the same result. The model id for this “clean” environment is DNBxQx5tRYWpzF0kYccuKg and the record id is XqgMroloQZWsuZtCwHhU9A

The only difference left that I can think of is the age of the project itself. Maybe the old project has been grandfathered to use some feature for backwards compatibility reasons? If this is the case, do you plan on making it possible to upgrade this behavior?

Hi @gabriel1,

Good catch, and sorry about that! Could you please share the error message you’re getting, and I’ll report it to the devs?

There was indeed an API change: https://www.datocms.com/product-updates/api-cleanup-for-freshly-created-projects

We want our APIs to be as clean as possible, but we also do not want to disrupt the work of our customers. With that in mind, we have decided to apply some changes to the API only for the websites created from January 1st, 2023 onwards. Therefore, websites created in the new year will have a slightly different API:

  • createdAt and updatedAt fields will be available on the content management API as part of the meta object of the item and item_type entities;
  • the fields createdAt and updatedAt won’t be available via GraphQL in the CDA: the existing _createdAt and _updatedAt will remain as they currently are (quick take-away: in our GraphQL API, meta fields always start with an underscore :wink:);

That was on purpose, but if it’s breaking the TypeScript code generation, I will let the devs know. Is your error also related to StructuredText?

Thanks!

Thanks, that definitely explains why we get the behavior I described.

The situation for old projects doesn’t stop graphql-codegen from running but the generated schema contains the below Duplicate identifier error for the (Created|Updated)At(Asc|Desc) fields of all …OrderBy types such as this BlogpageModelOrderBy:

It would be nice to be able to activate the behavior of new projects in old ones like an update under /configuration/available-updates. But the situation is easy to work around and the git repo linked to from How To Generate TypeScript Types From GraphQL — DatoCMS already contains the work around which is to change how the names of enums are generated.

1 Like

Thanks for the details! I’ll report it and see what they say.

Hi, I also found this problem months ago… and i wrote a simple script to cleanup the file after generation, using the “_ version” of old (created/updated)At.

Here the code of the script.

PS. Note that have to be optimized to read and write only once, instead of doing it every time on each loop as the example.

import { promises as fsPromises } from "fs";

//Replace file contents
async function replaceInFile(sourceFile, destFile, toReplace, replacement) {
  try {
    const contents = await fsPromises.readFile(sourceFile, "utf-8");
    const re = new RegExp(`${toReplace}`, "g");
    let replaced = contents.replace(re, `${replacement}`);
    await fsPromises.writeFile(destFile, replaced);
  } catch (err) {
    console.error(err);
  }
}

(async () => {
  const filePath = "./src/gql/graphql.ts";
  const list = [
    `CreatedAtAsc = '_createdAt_ASC'`,
    `CreatedAtDesc = '_createdAt_DESC'`,
    `UpdatedAtAsc = '_updatedAt_ASC'`,
    `UpdatedAtDesc = '_updatedAt_DESC'`,
  ];
  for (let toReplace of list) {
    let replacement = `_${toReplace}`;
    await replaceInFile(filePath, filePath, toReplace, replacement);
  }
})();

2 Likes

Thank you for sharing this, @l.ponticelli!


@gabriel1 (and anyone else in a similar situation): I talked to the devs about this, and we believe there is a manual database edit we can do to migrate you over. It’s not an automated or common process, unfortunately, so we can only offer it for paying customers (which you already are – thank you!). I’ll reach out via email to discuss the details with you and confirm a time slot for this. Please expect a message shortly.

For anyone else on a paid plan needing a similar migration, please email us at support@datocms.com with your account details :slight_smile:

Gabriel, that email will come soon. Sorry for the delay! Just waiting for the dev to send you over his scheduling link.

1 Like