question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GraphQL unresolved data-model

See original GitHub issue

I am having issues with rendering data from GraphQL into react since the links are unresolved…

When using this plugin with the contentful.js module it works great because everything is resolved in rich-text.

We tried to use the graphql api and everything is unresolved. I wrote a weird thing to manually resolve things myself, but I would love to see if you guys already have something that either does this for us (through this plugin, or another by Contentful).

Is there a way we can either resolve the linked content, or pass in the whole field from graphql to have the renderer resolve these?

Below is the logic route we took and the problem we face.

// get data from Contentful with GraphQL
const entry = await fetchWithGraphQL(`
query {
  articleCollection (limit: 1) {
    body {
      json
      links {
        entries {
          block {
            ... on CaptionBlock {
              __typename
              title
              sys {
                id
              }
              youGetTheIdea
            }
          }
        }
      }
    }
  }
}`)

// Render data. This is where I wonder if we need to resolve the graphql data ourselves first...
const Component = ({ entry }) => (
  <div>
    {documentToReactComponents(entry.body.json, contentfulRichTextOptons)}
  </div>
)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
pjawscommented, Jan 27, 2021

Surprised no one answered this… If anyone else is wondering about this my solution was to wrap the documentToReactComponents call in my own function which passes body.links so we can reference it + find the linked entries through filtering:

const generateBody = (bodyJSON: Document, links: any) => {
	const options: Options = {
		renderNode: {
			[BLOCKS.EMBEDDED_ENTRY]: (node: any, children) => {
				// Have access to links here, filter then render appropriate component
				return <div>embedded</div>;
			},
		},
	};

	return documentToReactComponents(bodyJSON, options);
};

I thought about doing this, but I figured there had to be a better way/something I was missing in the documentation. Apparently not. If the maintainers read this, please give us an easier way to use this library with GraphQL!

1reaction
colton-savage-lmpcommented, Nov 13, 2020

@pybuche Sure thing, basically the idea is to pull the entry ID from the node then you find the entry from the links you pass in to your function separately:

// inside generateBody
[BLOCKS.EMBEDDED_ENTRY]: (node: any) => {
	const linkFieldMap: { [index: string]: any } = {
		'embedded-entry-block': 'block',
	};
	const linkField = linkFieldMap[node.nodeType];
	const targetId = node.data?.target?.sys?.id;
	const entry = links.entries[linkField].filter(
		(cur: any) => cur.sys?.id === targetId
	)[0];

	if (!entry) {
		return null;
	}

	switch (entry.__typename) {
		case 'Quote':
			return <Quote entry={entry} />;
		default:
			return null;
	}
},
// Example generateBody call
generateBody(body?.json, body?.links)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring for GraphQL Documentation
If an exception remains unresolved, by default it is categorized as an INTERNAL_ERROR with a generic message that includes the category name and ......
Read more >
GraphQL Content API - Contentful
The GraphQL Content API provides a GraphQL API interface to the content from Contentful. Each Contentful space comes with a GraphQL schema based...
Read more >
Modeling GraphQL Mutations - commercetools tech
GraphQL mutations provide a flexible way for a client to modify data on ... a quite simple concept: it separates read and write...
Read more >
Data model and resolvers - apischema
None / Optional : Types are non-null (marked with an exclamation mark ! in GraphQL schema) by default; Optional types however results in...
Read more >
GraphQL Federation: A Model-Based approach
Abstract The Graph Query Language (GraphQL) is a framework for de- ... This generally requires a canonical data model [CDT18]. In a Microservice....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found