Gatsby: Field with `resolve` (using custom Node interface) doesn't appear in filtering

Created on 21 Aug 2019  路  14Comments  路  Source: gatsbyjs/gatsby

Description

When adding a resolve on a field that extends a custom Interface, the field isn't accessible for filtering:

https://github.com/LekoArts/gatsby-themes/blob/362ca8c0df0645bf5509c65f9151d50f069d1f99/themes/gatsby-theme-emma-core/gatsby-node.js#L79

image

With the title - which comes from the frontmatter - it works:

image

Steps to reproduce

Clone https://github.com/LekoArts/gatsby-themes/tree/362ca8c0df0645bf5509c65f9151d50f069d1f99 and run yarn workspace emma develop and see the individual project pages not working.

Expected result

Filterable by slug.


As per Slack discussion with @freiksenet

GraphQL bug

Most helpful comment

@LekoArts What you can do in the meantime is put the resolver on the interface.
You could either use buildInterfaceType for Project, similar to buildObjectType for MdxProject. Or preferably just wrap the slugify helper function in a custom fieldextension and use it on the Project.slug field:

actions.createFieldExtension({
  name: `slugify`,
  extend() {
    return {
      resolve(source) {
        return slugify(source);
      },
    };
  },
});

and then:

actions.createTypes(`
  interface Project @nodeInterface {
    id: ID!
    slug: String! @slugify
  }
`)

All 14 comments

@stefanprobst At first I thought it should be solvable by your PR, but the slug doesn't materialize on mdxProject either. Not sure what's wrong.

yes this is the sort of issue that #16679 was supposed to fix. it doesn't?

ok so this is what i get with #16679 on the theme-core/emma branch:
interfaces

馃憤馃徎

But I shouldn鈥檛 expect this to be merged soon, right? I'm fine with using something else as filter in the meantime

@LekoArts What you can do in the meantime is put the resolver on the interface.
You could either use buildInterfaceType for Project, similar to buildObjectType for MdxProject. Or preferably just wrap the slugify helper function in a custom fieldextension and use it on the Project.slug field:

actions.createFieldExtension({
  name: `slugify`,
  extend() {
    return {
      resolve(source) {
        return slugify(source);
      },
    };
  },
});

and then:

actions.createTypes(`
  interface Project @nodeInterface {
    id: ID!
    slug: String! @slugify
  }
`)

Ah, good to know, thanks! Will try that tomorrow

Following your instructions throws this error:

There was an error loading your projects or pages Cannot return null for non-nullable field MdxProject.slug.



  Error: Cannot return null for non-nullable field MdxProject.slug.

  - Array.forEach

  - index.js:83 forEach
    [gatsby-themes]/[iterall]/index.js:83:25
return {
  resolve: slugify,
}

And

return {
  resolve(source) {
    return slugify(source);
  },
}

Are different though 馃槅 Is this the reason?

that shouldn't make a difference. do you have the stuff that errors committed somewhere? I could take a look later today.

btw you could also wrap the mdx passthrough stuff in an extension so you can write all typedefs in plain sdl: https://github.com/stefanprobst/gatsby-themes/commit/696f165c55552162d2a12160ea4f5628b51ceb88

Ahh, I know what you didn't change but I changed:

https://github.com/stefanprobst/gatsby-themes/blob/696f165c55552162d2a12160ea4f5628b51ceb88/themes/gatsby-theme-emma-core/src/templates/project-query.tsx#L7-L8

Change the id here to slug. As the initial problem was that I can't use that for filtering, and in your example you placed the slugify somewhere else but don't use the slug for filtering.

filtering by slug should work fine now: https://github.com/stefanprobst/gatsby-themes/commit/89aad2e6ee3f6cd139f64923a9f13a32e82e319f just to clarify: this does or still doesn't work for you?

Reviewing your file in more detail, I also didn't place the @slugify on the MdxProject type as your snippet only showed the Project nodeInterface. So if it works for you, it should do so for me, too. I'll check.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jimfilippou picture jimfilippou  路  3Comments

ghost picture ghost  路  3Comments

hobochild picture hobochild  路  3Comments

Oppenheimer1 picture Oppenheimer1  路  3Comments

timbrandin picture timbrandin  路  3Comments