この記事は最終更新日から1年以上が経過しています。

nuxt.config.jsでApollo Clientを使う方法

2021-02-12
2021-03-04

暫定対応

bash
yarn add node-fetch apollo-boost
js
export default { modules: [ "@nuxtjs/sitemap", "@nuxtjs/apollo", ], sitemap: { path: "/sitemap.xml", hostname: process.env.BASE_URL || "https://example.com/", generate: true, cacheTime: 1000 * 60 * 60 * 24, exclude: [ "/login", "/404", ], routes: async () => { const routes = [] const fetch = require("node-fetch") const { gql } = require("apollo-boost") const ApolloBoost = require("apollo-boost") const ApolloClient = ApolloBoost.default const apolloClient = new ApolloClient({ fetch: fetch, uri: YOUR_API_ENDPOINT, }) const fetchUsers = gql` query { users { id } } ` const users = await apolloClient .query({ query: fetchUsers, }) .then((res) => res.data.users) users.forEach((user) => { routes.push({ route: `/${user.id}`, }) }) return routes }, }