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

Pass getNode to custom resolver functions #3

Merged
3 commits merged into from
Jan 23, 2019

Conversation

jsanchez034
Copy link

Currently if you have a content model that has nested models (see featuredImage field below) you need to resolve all the nested models and then query the nested model in userland code to build a proper model with all the data you require. This makes the elasticlunr search index JSON potentially very large and the search logic more complicated ...

// gatsby-config.js
{
  resolve : '@gatsby-contrib/gatsby-plugin-elasticlunr-search-local',
  options : {
    // Blog article fields to index
    fields : [
      'title'
    ],
    // How to resolve each field's value for a supported node type
    resolvers : {
      // For any node of type ContentfulBlogPost, list how to resolve the fields' values
      ContentfulBlogPost : {
        title         : node => node.title,
        featuredImage : node => node.featuredImage___NODE
      },
      // For any node of type ContentfulAsset, this is how ContentfulBlogPost featuredImage is resolved
      ContentfulAsset : {
        fileUrl : node => node.file && node.file.url
      }
    }
  }
}
// search.js
....
...
searchIndex.search(query, { expand : true })
    .map(({ ref }) => {
      const { featuredImage, ...rest } = searchIndex.documentStore.getDoc(ref);
      let result = {
        ...rest
      };

      if (featuredImage) {
        const { fileUrl } = searchIndex.documentStore.getDoc(featuredImage) || {};
        result = {
          ...result,
          featuredImage : {
            fixed : {
              src : `${fileUrl}?w=300&h=200&q=50&fit=fill`
            }
          },
          retinaFeaturedImage : {
            fixed : {
              src : `${fileUrl}?w=600&h=400&q=50&fit=fill`
            }
          }
        };
      }

      return result;
    });
...
..

Imagine in the above scenario you have 1,000 ContentfulBlogPost models and 50,0000 ContentfulAsset models. The search index will contain 51,000 items when it really could contain 1,000 items that are a little larger given the nested featuredImage model.

This change passes the getNode function to all resolvers so that a user of this plugin can use the getNode function in there custom resolvers and avoid bloating there elasticlunr search index JSON...

{
  resolve : '@gatsby-contrib/gatsby-plugin-elasticlunr-search-local',
  options : {
    // Blog article fields to index
    fields : [
      'title'
    ],
    // How to resolve each field's value for a supported node type
    resolvers : {
      // For any node of type ContentfulBlogPost, list how to resolve the fields' values
      ContentfulBlogPost : {
        title         : node => node.title,
        featuredImage : (node, getNode) => getNode(node.featuredImage___NODE)
      }
    }
  }
}

@ghost
Copy link

ghost commented Jan 21, 2019

Hi, thanks for this detailed use case. Could you please provide a way to document this on the repo, maybe extending the example on the readme with a link to this PR.

@jsanchez034
Copy link
Author

Hey @benjaminabel, thanks for the quick reply. Yeah sure can do 👍

@ghost ghost merged commit 26adc77 into gatsby-contrib:master Jan 23, 2019
@ghost
Copy link

ghost commented Jan 23, 2019

Thanks for your contribution @jsanchez034.

@jsanchez034
Copy link
Author

jsanchez034 commented Jan 23, 2019

Thank you @benjaminabel ! 😃

@jsanchez034
Copy link
Author

Oh @benjaminabel one question, what is your process for publishing the module to NPM? Is that something I can do?

@ghost
Copy link

ghost commented Jan 23, 2019

Just published it.

@jsanchez034
Copy link
Author

Thank you!

@jsanchez034
Copy link
Author

@benjaminabel looks like new module version did not build latest source code. I'm not seeing the new param being passed into custom resolver

@ghost
Copy link

ghost commented Jan 24, 2019 via email

@dylancristy
Copy link

So how does this work for collections? (Pretty new to Gatsby and GraphQL, so bear with me.)

Your example of

featuredImage : (node, getNode) => getNode(node.featuredImage___NODE)

assumes that there's only one image. What if it's featuredImages, plural, and there can be more than one?

@AleC77
Copy link

AleC77 commented Apr 22, 2021

So how does this work for collections? (Pretty new to Gatsby and GraphQL, so bear with me.)

Your example of

featuredImage : (node, getNode) => getNode(node.featuredImage___NODE)

assumes that there's only one image. What if it's featuredImages, plural, and there can be more than one?

Any reply for this question above?

This pull request was closed.
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

Successfully merging this pull request may close these issues.

3 participants