【问题标题】:React-Native + Apollo-Link-State + AWS Appsync : Defaults are not stored in cacheReact-Native + Apollo-Link-State + AWS Appsync:默认值不存储在缓存中
【发布时间】:2019-02-17 00:30:51
【问题描述】:

我为此苦苦挣扎了好几天。

一个月前,我已经成功地使用 AppSync 配置了 Apollo-Link-State,并开始像这样添加默认值和解析器:

const cache = new InMemoryCache() //create the cache to be shared by appsync and apollo-link-state

const appSyncAtrributes = {
  //...
  //setup appSync
}

const stateLink = createLinkWithCache(() => withClientState({
    cache,
    resolvers: {
        Mutation: {
            updateCurrentUser: (_, { username, thumbnailUrl }, { cache }) => {
                // ...
                // working mutation that store the login information when user authenticate.
            }
        }
    },
    defaults: {
        currentUser: {
            __typename: 'CurrentUser',
            username: '',
            thumbnailUrl: ''
        }
    }
}))

const appSyncLink = createAppSyncLink({
    url: appSyncAtrributes.graphqlEndpoint,
    region: appSyncAtrributes.region,
    auth: appSyncAtrributes.auth,
    complexObjectsCredentials: () => Auth.currentCredentials()
})

const link = ApolloLink.from([stateLink, appSyncLink])

const client = new AWSAppSyncClient({}, { link })

它工作了这么久(我正在调用 @client 突变并围绕我的应用进行查询)。

但现在我正尝试在我的链接状态中添加其他数据(其他一切都保持不变):

 
defaults: {
    currentUser: {
        __typename: 'CurrentUser',
        username: '',
        thumbnailUrl: '',
        foo: 'bar',
    },
    hello: 'world',
    userSettings: {
        __typename: 'userSettings',
        isLeftHanded: true
    }
}

而且我的缓存没有更新。我的意思是:

currentUser 仍然包含 __typename: 'CurrentUser', username: '', thumbnailUrl: ' 但不包含 foo: 'bar'. And the cache doensn't containshello: 'bar'oruserSettings`。

更令人困惑的是,如果我给usernamethumbnailUrl 赋值,比如username: 'joe',缓存实际上反映了这种变化! (忽略我所有的其他修改)

我尝试了这个实验的所有变体并清除了所有缓存(甚至在新同事的计算机上运行它以确保它们不涉及脏缓存)。

我一无所知。

上下文:

  • 它发生在 iOS 模拟器中
  • 我正在调试查看 appsync 的 redux 缓存
  • apollo-cache-inmemory:1.2.8
  • apollo 客户端:2.3.5
  • 阿波罗链接:1.2.2
  • 阿波罗链路状态:0.4.1
  • aws-appsync:1.3.2

更新:实际上 currentUser 既不是从默认值存储的。调用突变时它会进入缓存。

【问题讨论】:

    标签: ios react-native apollo apollo-client apollo-link-state


    【解决方案1】:

    好吧,我的问题毕竟不是问题(浪费了 2 天)。

    缺乏适当的调试工具(必须通过 redux 观察缓存的演变)是问题所在。 缓存实际上是正确写入的,但没有显示在任何地方。

    我一开始查询缓存,一切正常。

    等不及 react-native-debugger 集成适当的 apollo/graphql 分析器

    【讨论】:

      猜你喜欢
      • 2019-04-20
      • 2019-03-22
      • 2019-07-06
      • 2019-03-31
      • 2019-05-13
      • 2019-06-30
      • 2018-08-30
      • 1970-01-01
      • 2018-06-08
      相关资源
      最近更新 更多