birkir / gatsby-source-prismic-graphql

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

update from 3.5.0 to 3.6.2 breaks multi-language page query #207

Closed tekhaus closed 4 years ago

tekhaus commented 4 years ago

Hello, first off, thank you for creating this plugin for connecting Gatsby to Prismic.

Just wanted to let you know that somewhere between 3.5.0 and the 3.6.2 release, something has changed in what's being passed to graphqll for localized content which causes a fatal build error.

More specifically, it looks like the doc lang is not being properly passed, resulting in graphql trying to filter on an undefined value.

I can confirm that all of the pages I have in my Prismic repo have published versions for both languages. The build works smoothly with 3.5.0, updating this plugin to 3.6.2 causes this issue, and rolling back fixes it.

Thanks for looking into this bug.

There was an error in your GraphQL query:

Variable "$lang" got invalid value undefined; Expected non-nullable type String! not to be null.

>  1 | query PRISMIC_PAGE($uid: String!, $lang: String!) {
     |                                   ^
   2 |   prismic {
   3 |     page(uid: $uid, lang: $lang) {
   4 |       _meta {
   5 |         id
   6 |         uid
   7 |         type
   8 |         tags
   9 |         lang
  10 |       }
  11 |       name

File path: /[redacted]/src/templates/page.jsx
Url path: /preview/page
Plugin: none

in gatsby-config.js
===================
{
  resolve: 'gatsby-source-prismic-graphql',
  options: {
    repositoryName: process.env.PRISMIC_REPO,
    accessToken: process.env.PRISMIC_API_KEY,
    langs: ['fr-ch', 'en-us'],
    shortenUrlLangs: true,
    path: '/preview',
    previews: true,
    pages: [
      {
        type: 'Page',
        match: '/:lang/:uid',
        path: '/page-preview',
        component: require.resolve('./src/templates/page.jsx'),
      },
    ],
  },
},
MadsMadsDk commented 4 years ago

Could this be due to no defaultLang being set on your plugin options, albeit it being optional?

veloce commented 4 years ago

It could be the defaultLang indeed. I'm gonna look at it.

veloce commented 4 years ago

Sorry I couldn't reproduce a build error with a similar conf, even if I remove the defaultLang.

tekhaus commented 4 years ago

Thank you both for investigating. I was able to reproduce it again right now just by upgrading to 3.6.2. However, adding the defaultLang option does in fact resolve the issue.

I mistakenly read that setting the defaultLang would remove the lang prefix from the paths of the default language, but as long as I don't use the /:lang?/:uid' convention, I'm actually fine.