Route Resolver for Inverse Relationship

Is it possible to create a route resolver that utilizes an inverse relationship to get the parent of the current item? In other words:

I have a collection of courses, and each course uses content relationships to get the lessons for the course:

course
- lesson1
- lesson2

However, the URLs for the lessons should include the uid of the course, such as site.com/course-uid/lesson1. Is it possible to achieve this utilizing a route resolver? My lessons don't relate to the courses themselves, only through the relationship of the course documents.

Thanks!

Hello @jwinton

Thanks for reaching out to us.

You haven't mentioned the framework so I describe my answer based on Nuxt.js. You can Create folder structure in your project as /course/_uid/_uid. Whenever you want to know the uid value, try to access all params.
For example:

  1. At /course/_uid/ ,there will be only one params value that will be only uid value, which is course-uid.
  2. At /course/_uid/_uid ,there will be only one params value. It will be 2 uid values, which are course-uid and lesson_uid.

Give this atry and let me know if you have any further questions.

Thanks,
Priyanka

Thank you! I should have mentioned that the application is in Next.js; do you have an example of how to create a route resolver function? I've currently got a linkResolver set up, but it requires passing the courseUid from the query params down manually, and it would be a lot simpler for it to be automatic.

Thanks!

Hello @jwinton,

In the case of Next.js, you can use slugs to check whether the URL is /course-id or /course/lesson-id.

  1. create a dynamic route as /..slug.js and get all the params from URL using router.query.
  2. Create 2 separate functions for course and lesson content operations.
  3. If router.query is returning 1 params, i.e. it is course level and the function of redirection of course should be executed.
  4. If router.query is returning 2 params, i.e. it is course level and the function of redirection of lesson should be executed.

I hope it answers your question. Let me know if you have any further questions.

Thanks,
Piyanka

Hi Priyanka; coming back to this after a long bit!

So, I'm looking back on my workflow for querying data, and it's become complex and the linkResolver isn't cutting it, and I don't want to have to do a lot of work on my end setting up custom URLs as the content keeps growing; would rather use the route resolver, but I'm running into a little bit of a hiccup.

Essentially, like detailed above, I have lesson documents, which don't have a top-level content relationship; courses related to the lessons, but not the other way around, i.e.

course
- lesson1
- lesson2

Along with that, the lessons are in a group field, as there multiple lessons related to each course. Like I said, I want to use the routeResolver instead of linkResolver. I've got it started like so:

routes: [
    {
      type: 'index',
      path: '/',
    },
    {
      type: 'courses',
      path: '/:uid',
    },
    {
      type: 'path',
      path: '/:uid',
    },
    {
      type: 'lesson',
      resolvers: {
        course: 'course',
      },
      path: '/learn/:course/:uid',
    },
  ],

However, when I try and access a lesson now I receive this error:

Error: [Link resolver error] Invalid resolver course in page type lesson
The content relationship course doesnt exist on the top level of the custom type lesson

Is there a way around this so I can query the course relationship from a lesson?

Hi Vero,

The route resolver cannot work with content relationships from a group, unfortunately.