birkir / gatsby-source-prismic-graphql

Gatsby source plugin for Prismic GraphQL
MIT License
137 stars 75 forks source link

extraPageFields throws an error when I add a page with no such field #183

Open kb1995 opened 4 years ago

kb1995 commented 4 years ago

I have an Article type, and I use extraPageFields to filter by category to generate different sub-categories. But I also decided to add another type Newsletter which doesn't have a category but still want to generate the said page.

        extraPageFields: "category",
        pages: [
          {
            type: "Newsletter",
            match: "/newsletter",
            path: "/newsletter",
            component: require.resolve("./src/templates/newsletter.js"),
          },
          {
            type: "Article",
            match: "/rails/:uid",
            path: "/blog",
            filter: data => data.node.category === "rails",
            component: require.resolve("./src/templates/article.js"),
          },
          {
            type: "Article",
            match: "/ruby/:uid",
            path: "/blog",
            filter: data => data.node.category === "ruby",
            component: require.resolve("./src/templates/article.js"),
          },
        ],

I get

GraphQLError: Cannot query field "category" on type "PRISMIC_Newsletter".

I guess Prismic tries to search for the field in this page but it fails and it throws an error. But this means that if I want to use extraPageFields, I need to add it to every page that I want to use.