thompsonsj / gatbsy-source-payload-cms

MIT License
5 stars 0 forks source link

[question] missing `nodeTransform` object in `formatEntity` function #69

Closed jekel1818 closed 11 months ago

jekel1818 commented 1 year ago

Hello, I am a total noob in Gatsby, so sorry if this is a dumb question..

After adding the plugin, I was always getting the following error: TypeError: Cannot read properties of undefined (reading 'id')

Looking at the source code it seems like nodeTransform is expected to be defined ( which was not the case for me )

    Object.keys(res).forEach((value) => {
        if ((0, lodash_1.isFunction)(context.pluginOptions.nodeTransform[value])) {
            transformedRes[value] = context.pluginOptions.nodeTransform[value](res[value]);
        }
        else {
            transformedRes[value] = res[value];
        }
    });

Solving the issue was very straightforward, just needed to add a dummy nodeTransform object in the plugin config, but it felt a bit weird

  plugins: [
    {
      resolve: `gatsby-source-payload-cms`,
      options: {
        endpoint: `https://payload.<some-company>.ca/api/`,
        nodeTransform: {}, // <-
        collectionTypes: [
          {
            slug: `projects`,
            params: { [`where[published][equals]`]: true, limit: 50 },
          },
        ],
      },
    },
  ],

Is there a better way to handle this situation, or maybe I am just missing some global Gatsby configuration?

Thanks

thompsonsj commented 11 months ago

@jekel1818 thank you so much! Fixed and released in 0.12.2. This was a mistake I made in the logic. I wanted to add nodeTransforms to modify any field before it is added to Gatsby, but neglected to consider the behaviour if this option is not used.

How are you finding querying the data in GraphQL? The plugin will likely work out of the box initially, but as data structure/queries get more complex, GraphQL schema inference starts to break down.

I am working on creating a Payload plugin that exposes the GraphQL schema to this plugin. This unlocks the power of the whole Gatsby GraphQL setup!