Git Product home page Git Product logo

Comments (4)

kedarguy avatar kedarguy commented on August 18, 2024 2

Hey,

Ran in to this when looking for a solution, this is how I did it:

In nuxt .config.js

const { createApolloFetch } = require('apollo-fetch')

module.exports = {
 ...
  generate: {
    routes: function () {
      const uri = 'yourendpoint'
      const apolloFetch = createApolloFetch({ uri })
      const query = `
      query allBlogPosts{
        allBlogPosts{
              slug
        }
      }
      `
      const staticRoutes = [
        '/policies/disclaimer',
        '/policies/privacy',
        '/policies/terms'
      ]
      return apolloFetch({ query }) // all apolloFetch arguments are optional 
        .then(result => {
          const { data } = result
          const dynamicRoutes = data.allBlogPosts.map(post => `/post/${post.slug}`)
          return staticRoutes.concat(dynamicRoutes)
        })
        .catch(error => {
          console.log('got error')
          console.log(error)
        })
    }
  },

from apollo.

stromi avatar stromi commented on August 18, 2024

@kedarguy thanks for the response, but actually it gets stuck at the first line – it throws an error:
Error: Cannot find module 'apollo-fetch'

I'm using the nuxt apollo module (this repo), so my nuxt.config.js is the following (more or less, just the corresponding parts):

module.exports = {
    modules: [
        '@nuxtjs/apollo',
    ],
    apollo: {
        networkInterfaces: {
            default: '~/apollo/network-interfaces/default.js'
        }
    },
    ...
    generate: {        
        routes: function() {            
            const uri = 'my_api_url'
            const apolloFetch = createNetworkInterface({ uri })
            const query = `
            query Shops($language: language, $category: ShopCategoryParam) {
              site(language: $language) {    
                shops { 
                  by_category(category: $category, pagination: {sort: {field: name}, limit: 100}) {
                    total
                    edges {
                      node {                        
                        name                        
                      }
                    }
                  }
                }
              }
            }

            `
            return apolloFetch({ query })
              .then(result => {
                const { data } = result
                const dynamicRoutes = data.Shops.by_category.edges.map(node => `/shops/${shop.slug}`)                
              })
              .catch(error => {
                console.log('got error')
                console.log(error)
              })
        }
    }
}

Do you have any idea how to solve this one? :) Thanks in advance!

from apollo.

kedarguy avatar kedarguy commented on August 18, 2024

@stromi

looks like you missed the require line

const { createApolloFetch } = require('apollo-fetch')

module.exports = {
 ...

make sure you install apollo-fetch package from npm

Check out the npm instructions for how to pass variables to the query, I think sorting is not relevant for generate but it shouldnt cause problems.


        .then(result => {
                const { data } = result
                const dynamicRoutes = data.Shops.by_category.edges.map(node => `/shops/${shop.slug}`) 
                console.log('dynamicRoutes: ')
                console.log(dynamicRoutes)
                return dynamicRoutes               
              })

here you need to return an array containing the routes you were expecting, you can add a console.log for debugging

from apollo.

dohomi avatar dohomi commented on August 18, 2024

will close this issue for now as it seems its been solved

from apollo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.