首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nuxt.config.js中的阿波罗查询

nuxt.config.js中的阿波罗查询
EN

Stack Overflow用户
提问于 2020-11-25 16:17:12
回答 1查看 1.3K关注 0票数 4

我正在尝试用nuxt-apollo-modulenuxt中提出一个聪明的请求,以便为nuxt-sitemaps-module获取我的路由(这样我就可以用它们创建我的网站地图)。

我需要在nuxt.config.js文件中发出此请求。我尝试过这种方法,但没有成功(因为在这个上下文中不存在app )。这样做的正确方式是什么?提前感谢!

我的nuxt.config.js的相关部分

代码语言:javascript
复制
import gql from 'graphql-tag'

module.exports = {

  modules: [
    '@nuxtjs/apollo',
    '@nuxtjs/sitemap'
  ],

  apollo: {
    clientConfigs: {
      default: {
        httpEndpoint: 'https://example.com/graphql'
      }
    }
  },


  sitemap: {
    path: '/sitemap.xml',
    hostname: 'https://example.com/',
    generate: true,
    cacheTime: 86400,
    trailingSlash: true,
    routes: async ({ app }) => {
      const myRoutes = ['/one-random-path/']
      let client = app.apolloProvider.defaultClient
      let myProductsQuery = gql`query {
          products {
              slug
          }
      }`
      let myBrandsQuery = gql`query {
          brands {
              slug
          }
      }`
      const myProducts = await client.query({ query: myProductsQuery })
      const myBrands = await client.query({ query: myBrandsQuery })

      return [myRoutes, ...myProducts, ...myBrands]
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-02-12 22:38:02

我能够以这种方式生成它,但我相信有更好的方法。

代码语言:javascript
复制
yarn add node-fetch apollo-boost
代码语言:javascript
复制
sitemap: {
  routes: async () => {
    const routes = []
    const fetch = require("node-fetch")
    const { gql } = require("apollo-boost")
    const ApolloBoost = require("apollo-boost")
    const ApolloClient = ApolloBoost.default

    const client = new ApolloClient({
      fetch: fetch,
      uri: YOUR_API_ENDPOINT,
    })

    const fetchUsers = gql`
      query {
        users {
          id
        }
      }
    `

    const users = await client
      .query({
        query: fetchUsers,
      })
      .then((res) => res.data.users)

    users.forEach((user) => {
      routes.push({
        route: `/${user.id}`,
      })
    })

    return routes
  },
},
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65000800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档