【问题标题】:Getting With helper for offline capabilities to work获得帮助以使离线功能正常工作
【发布时间】:2019-09-28 18:03:27
【问题描述】:

今天,我开始了解 Appsync 提供的离线功能,这是我第一次感到不知所措。我花了一天时间浏览了几个教程,但它们从来没有提供足够的东西让我完成任务,或者我可能只是愚蠢。好吧,回到我遇到的放大 github 文档


import { buildMutation } from 'aws-appsync';
import { listTodos } from './graphql/queries';
import { createTodo, CreateTodoInput } from './graphql/mutations';

(async () => {
  const result = await client.mutate(buildMutation(client,
    gql(createTodo),
    {
      inputType: gql(CreateTodoInput),
      variables: {
        input: {
          name: 'Use AppSync',
          description: 'Realtime and Offline',
        }
      }
    },
    (_variables) => [ gql(listTodos) ],
    'Todo'));

  console.log(result);
})();

所以我尝试使用它。但我收到错误client is not defined 但我已将 app.js 包装在 apolloprovider 中

const client = new AWSAppSyncClient({
  url: aws_config.aws_appsync_graphqlEndpoint,
  region: aws_config.aws_appsync_region,
  auth: {
    type: aws_config.aws_appsync_authenticationType,
    apiKey: aws_config.aws_appsync_apiKey,
  },
  offlineConfig: {
    callback: (err, succ) => {
      if(err) {
        const { mutation, variables } = err;

        console.warn(`ERROR for ${mutation}`, err);
      } else {
        const { mutation, variables } = succ;

        console.info(`SUCCESS for ${mutation}`, succ);
      }
    },
  },
});

const WithProvider = () => (
  <ApolloProvider client={client}>
    <Rehydrated>
    <ArztRoutes/>
    </Rehydrated>
  </ApolloProvider>

据我了解,它会将客户端传递给所有其他类,所以为什么我不断收到此错误以及如何解决它。非常感谢!

【问题讨论】:

    标签: reactjs react-apollo aws-appsync


    【解决方案1】:

    即使在 ApolloProvider 中包装应用程序时,您仍然需要定义如何访问客户端。例如。名为client 的变量将不会全局可用,因此您需要获取它的句柄。查看 Apollo 文档,您应该能够使用 ApolloConsumer 来访问客户端 (https://www.apollographql.com/docs/react/api/react-apollo#apollo-consumer)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      • 2012-08-31
      • 1970-01-01
      • 2021-11-10
      • 2015-02-14
      相关资源
      最近更新 更多