Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Updated] How can I identify a referenced component's type? #246

Open
SabbeRubbish opened this issue Oct 13, 2022 · 6 comments
Open

[Updated] How can I identify a referenced component's type? #246

SabbeRubbish opened this issue Oct 13, 2022 · 6 comments

Comments

@SabbeRubbish
Copy link

SabbeRubbish commented Oct 13, 2022

[Updated] See this comment for the up to date problem.

Reproduction

You can reproduce the issue with my repo https://github.com/SabbeRubbish/graphcms-bug-repro
Used GraphCMS and latest gatsby default starter.
Added to gatsby-config.js:

exports.onCreateNode = ({ node, getNode, actions }) => {
  const { createNodeField } = actions

  createNodeField({
    node,
    name: `BLAH`,
    value: "BLAH",
  })
}

Expected behaviour

ALL nodes get a fields object with BLAH as key and BLAH as value.

Actual behaviour

ALL nodes except GraphCMS nodes get the extra field:
image
image

But not the GraphCMS nodes:
image
image

Use case

I want to add an extra field "Type" so that I can distinguish on components' types. For example, I have a page with 2 modular components that I can place on them: section with image or testimonials. But in my page template I need to know which one is which, which is why I want to add a _type field to be able to distinguish.

Thanks for any help!

@raae
Copy link

raae commented Oct 13, 2022

This is probably because of a do not infer policy set by GraphCMS on the node's schema.

@raae
Copy link

raae commented Oct 13, 2022

Seems this is done by gatsby-graphql-source-toolkit that is used: https://www.npmjs.com/package/gatsby-graphql-source-toolkit#5-add-explicit-types-to-the-gatsby-schema

@SabbeRubbish
Copy link
Author

Hi raae,

Thank you for your reply and useful insights.
That however does not solve my problem.

Let me elaborate a bit more, I have a Page in GraphCMS that has multiple types of modular components I can add:

image

When I source this into Gatsby, I see this in GraphiQL:
image

There is no way for me to identify which type of page section I'm dealing with:

image

And right now it's just two, but I'll be adding a lot more of them.

So either I find a way to add the internal content type of the referred component myself, or the the source plugin is updated to remediate this issue that others must also face someday or have faced already. I like GraphCMS just a bit too much right now to go find another CMS to do this with.

Many thanks for any help on this!

@SabbeRubbish SabbeRubbish changed the title createNodeField has no effect on GraphCMS nodes [Updated] How can I identify referenced components' type? Oct 14, 2022
@SabbeRubbish SabbeRubbish changed the title [Updated] How can I identify referenced components' type? [Updated] How can I identify a referenced component's type? Oct 14, 2022
@karlwir
Copy link

karlwir commented Oct 24, 2022

Hi @SabbeRubbish

I faced the same issue when trying to map my modular content to react components. I ended up writing a custom resolver, it adds the remoteTypeName as a "component" field on my the contents types I add it to. Its exported from gatsby-node.ts

export const createResolvers: GatsbyNode["createResolvers"] = ({
  createResolvers,
}) => {
  const addComponentResolver = {
    component: {
      type: "String",
      resolve: (source: { remoteTypeName: any }) => {
        return source.remoteTypeName;
      },
    },
  };

  createResolvers({
    HygraphCMS_Block: addComponentResolver,
    HygraphCMS_Grid: addComponentResolver,
    HygraphCMS_GridColumn: addComponentResolver,
    HygraphCMS_Link: addComponentResolver,
    HygraphCMS_ButtonLink: addComponentResolver,
    ...any other type that needs the component field
  });
};

@SabbeRubbish
Copy link
Author

Hi @karlwir,

Thank you for your response!
Works like a charm, but I would expect this type of behaviour to be built into the core of this module.
It's a good workaround for now, but every new component that is added to HyGraph would need a code update, which is not ideal.

@karlwir
Copy link

karlwir commented Oct 24, 2022

@SabbeRubbish

I agree 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants