Apollo客户端无法通过SchemaLink在Next.js的getServerSideProps中进行查询。
创始人
2024-09-09 11:30:31
0

在getServerSideProps函数中访问Apollo客户端的查询功能,需要使用另一种方法而不是在浏览器中使用的常规方法。这是因为getServerSideProps运行在服务器上,而不是在浏览器中。解决这个问题的一种方法是使用GraphQL的fetch功能来访问Apollo客户端的查询功能。以下是一个使用这种方法的示例代码:

import { ApolloClient, ApolloProvider, InMemoryCache, gql } from '@apollo/client';
import { SchemaLink } from '@apollo/client/link/schema';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { useRouter } from 'next/router';

// Defining the executable schema
const typeDefs = `
  type Query {
    hello: String
  }
`;

// Resolving the schema
const resolvers = {
  Query: {
    hello: () => 'world'
  }
};

// Creating the schema
const schema = makeExecutableSchema({ typeDefs, resolvers });

async function getServerSideProps() {
    const client = new ApolloClient({
      ssrMode: true,
      cache: new InMemoryCache(),
      link: new SchemaLink({ schema })
    });

    const { data } = await client.query({
      query: gql`
        query {
          hello
        }
      `
    });

    return {
      props: {
        hello: data.hello
      }
    }
}

function Hello({ hello }) {
  const router = useRouter();

  return (
    

{hello}

); } export default function App({ hello }) { return ( ); } export { getServerSideProps };

相关内容

热门资讯

盘点十款“wpk辅助kaq66... >>您好:wpk辅助kaq665确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,总...
最新通报“wepoker有透视... 您好,wepoker有透视软件挂吗这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302...
必备教程“wepoker真的能... >>>您好:,软件加微信【添加136704302】确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
专业讨论“wpk德州最新版”开... 无需打开直接搜索加微信客服(136704302)咨询了解wpk德州最新版辅助器目前(微信:13670...
交流学习经验“wepoker有... >>您好:wepoker有没有透视挂软件加扣扣群确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
技巧知识分享“wepoker是... wepoker是不是有辅助是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户...
一分钟揭秘“wepoker辅助... 无需打开直接搜索;操作使用教程:一分钟揭秘“wepoker辅助开挂方法”开挂辅助器(透视)线上教程1...
技术分享“wepoker是不是... >>您好:wepoker是不是有透视软件软件加扣扣群确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
玩家必看攻略“德州wepoke... >>您好:德州wepoker透视挂确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,...
玩家必用“WPK免费挂机辅助器... >>您好:WPK免费挂机辅助器软件加扣扣群确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...