React-Native + Apollo-Link-State + AWS Appsync:默认值不存储在缓存中 [英] React-Native + Apollo-Link-State + AWS Appsync : Defaults are not stored in cache

查看:20
本文介绍了React-Native + Apollo-Link-State + AWS Appsync:默认值不存储在缓存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm struggling with this for days.

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

I've successfully configure Apollo-Link-State with AppSync a month ago and start adding defaults and resolvers like this :

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 突变并查询我的应用程序).

So long it worked (I'm calling the @client mutation and query around my app).

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

But now I'm trying to add other data in my Linked State as this (everything else stayed the same):

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

我的缓存没有更新.我的意思是:

And my cache doesn't update. I mean :

currentUser 仍然包含 __typename: 'CurrentUser', username: '', thumbUrl: ' 但不包含 foo: 'bar'.并且缓存不包含hello: 'bar'oruserSettings`.

currentUser still contains __typename: 'CurrentUser', username: '', thumbnailUrl: ' but doesn't contains foo: 'bar'. And the cache doensn't containshello: 'bar'oruserSettings`.

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

More confusing is the fact that if I give a value to username or thumbnailUrl like username: 'joe', the cache actually reflect that change! (while ignoring all my other modifications)

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

I tried all variation of this experiment and cleared all caches (even running it on a fresh colleague computer to be sure they were no dirty cache involved).

我完全无能为力.

上下文:

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

更新:实际上 currentUser 也没有从默认值中存储.它在调用突变时进入缓存.

Update : Actually currentUser is not stored neither from defaults. It get into the cache when the mutation is called.

推荐答案

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

Ok my issue wasn't an issue after all (2 days wasted).

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

The lack of proper debug tools (had to watch the cache evolving thru redux) was the issue. The cache is actually written correctly but doesn't show anywhere.

一旦我开始查询该缓存,一切正常.

As soon as I start querying that cache, everything worked.

迫不及待地等待 react-native-debugger 集成合适的 apollo/graphql 分析器

Can't wait for react-native-debugger to integrate a proper apollo/graphql analyser

这篇关于React-Native + Apollo-Link-State + AWS Appsync:默认值不存储在缓存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆