fetchLinks/GraqhQuery and Routes

I'm using Gatby-source-prismic for a website. I know this isn't supported anymore but hopefully, the question I have is about implementation rather than a bug:

I have a field in my Prismic page document called 'directory'. If the directory field is filled out then the page is generated in that directory using gatsby-node.

However, I'm having a problem with resolving those pages (prismicLink)
My original gatsby-config had the following:

        fetchLinks: ['page.directory'],
        lang: process.env.GATSBY_PRISMIC_LANG,
        linkResolver: require('./src/utils/linkResolver').linkResolver,
        repositoryName: process.env.GATSBY_PRISMIC_MAIN_REPOSITORY_NAME,
        customTypesApiToken: process.env.GATSBY_PRISMIC_CUSTOM_TYPES_API_TOKEN,

In my linkResolver I use doc.data.directory to ensure my links resolve.

Everything was going smoothly until I updated to the latest version of the plugin as it now requires routes: in the configuration, which seems to supersede my linkResolver.

So how can I use directory from my Prismic document in order to resolve my 'routes'?

As a secondary question, I am thinking of updating my fetchLinks to use graphQuery. but everything I try fails. Can you advise/give me an example of how I might use graphQuery in this example?

@Pau anyone in Prismic able to help here?

I have no idea how to fix this, it's been a while since I don't look at the docs and I just noticed we're not documenting the use of routes in the technical ref. Can we get your help @angeloashmore ?

Hi @angeloashmore any help would be greatly appreciated

Hi @thejuniperstudio, I hope things are going well. :slight_smile:

The linkResolver function should still take priority over the routes option. The system will fall back to using the routes option if linkResolver returns null or undefined.

The URL is lazily-computed via the GraphQL field, which means you won't see the resolved URL if you aren't using GraphQL. That could happen if you use something like context.nodeModel.

Your gatsby-node file should use a GraphQL query to gather all of the pages. You should be able to query the document's url field to get the page's URL. The linkResolver should be used.

You could also use Gatsby's File System Route API to remove the need for gatsby-node. For example, you could have a file named {PrismicPage.url}.js, which will generate a page for every PrismicPage document at its resolved URL.

So how can I use directory from my Prismic document in order to resolve my 'routes'?

Route resolver (the routes option) only allows resolving UID fields. If your directory field is a content relationship to a document type with a UID field, then great, you can resolve the value in the URL. The Route Resolver documentation has an explanation and some examples: Route Resolver - Documentation - Prismic

If the directory field is a text-based field, like key text, then it won't be usable in the URL. This behavior protects your website from duplicate URLs; UIDs are unique within a custom type.

As a secondary question, I am thinking of updating my fetchLinks to use graphQuery. but everything I try fails. Can you advise/give me an example of how I might use graphQuery in this example?

GraphQuery can be difficult to get right. The GraphQuery documentation has a bunch of examples that might help: https://prismic.io/docs/graphquery-rest-api

You don't need to migrate to GraphQuery unless you have a specific need to use it. In some cases, GraphQuery is necessary, such as fetching data from content relationships in slices.

Hope that helps!