question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using graphql request inside `generate` to request data for routes?

See original GitHub issue

This is more of a question than an issue. We’re currently using nuxt-graphql-request in our project and it’s working great.

However, we’re looking to make a graphQL request in the generate property of the Nuxt config in order to generate routes based on the query response, something similar is outlined in this post which uses Apollo: https://rosso.codes/blog/nuxt-generate-dynamic-routes-with-apollo-and-graphql/

Wondering if there’s a way to reference/access the graphql property created in the Nuxt config INSIDE another property in the config? Obviously we can’t use this.$graphql inside the generate property.

Or perhaps there’s a way to do this using middleware? Any thoughts welcome. Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Gomahcommented, Apr 29, 2021

Hey @alexmahan, indeed, you can’t use this.$graphql inside the generate property as, you have to use graphql-request like the following example:

import { request, gql } from 'graphql-request';

export default {
  generate: {
    routes() {
      const query = gql`
        {
          Movie(title: "Inception") {
            releaseDate
            actors {
              name
            }
          }
        }
      `;
      return request('https://api.graph.cool/simple/v1/movies', query).then(
        res => {
          return res.data.map(movie => {
            return {
              route: '/movies/' + movie.id,
              payload: movie,
            };
          });
        },
      );
    },
  },
};
0reactions
andreasvirkuscommented, Oct 15, 2021

Thanks for the reply! I have the queries working inside Vue files, but I’d need to include it in nuxt.config.js so I could use it in the generate.routes option

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serving over HTTP - GraphQL
Your GraphQL HTTP server should handle the HTTP GET and POST methods. GET request#. When receiving an HTTP GET request, the GraphQL query...
Read more >
How To Build A GraphQL Server Using Next.js API Routes
In this guide, we will be first learning what are API Routes, and then create a GraphQL server that retrieves the data from...
Read more >
Custom Routes for External Data with GraphQL - Strapi
The first thing is to create a reusable Query component that we can use anywhere in our code to fetch the \graphql endpoint....
Read more >
How to Fetch GraphQL Data in Next.js with Apollo GraphQL
When fetching data, instead of making a GET request to a URL to grab that data, GraphQL endpoints take a “query”. That query...
Read more >
4 Simple Ways to Call a GraphQL API
Passing the GraphQL query to the useQuery hook gives us an object that can be de-structured into loading , error , and data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found