首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >意外*丢失字段*在解决graphql查询的联合结果时出错( Vue阿波罗客户端)

意外*丢失字段*在解决graphql查询的联合结果时出错( Vue阿波罗客户端)
EN

Stack Overflow用户
提问于 2021-02-01 14:50:55
回答 2查看 2.5K关注 0票数 0

我正在使用VueApollo进行GraphQL查询。

我确实有使用联合的查询,它在独立的GraphQL编辑器中工作。当我试图使用与每个类型不同的字段查询联合类型时,会出现问题。

我正在使用VueApollo智能查询接口。

下面是一个查询:

代码语言:javascript
复制
{
    notifications{
    id,
    message,
    recipient {
      id
      username
    }
    notificable {
      __typename
      ... on Comment {
        id
        commentable {
          ... on Post {
            __typename
            id
            title
          }
          ... on Comment { 
            __typename
            id
            message     
          }
        }
      }
    }   
    }
}

在解决查询结果时,我在控制台中接收到的错误:

代码语言:javascript
复制
instrument.js:109 Missing field message in {
  "__typename": "Post",
  "id": "999bf735-057b-4a69-a164-e2b09a0be2f1",
  "title": "Post"
}

(notifications.notificable.commentable)当我添加要注释的消息字段时会出现错误,但它与Post无关。我猜邮件字段是在写到本地商店时被迫使用Post类型的。

堆栈跟踪:

代码语言:javascript
复制
(anonymous) @   instrument.js:109
(anonymous) @   invariant.esm.js:29
(anonymous) @   bundle.esm.js:575
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.StoreWriter.writeSelectionSetToStore    @   bundle.esm.js:551
(anonymous) @   bundle.esm.js:603
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.StoreWriter.writeSelectionSetToStore    @   bundle.esm.js:551
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.StoreWriter.writeFieldToStore   @   bundle.esm.js:649
(anonymous) @   bundle.esm.js:560
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.StoreWriter.writeSelectionSetToStore    @   bundle.esm.js:551
(anonymous) @   bundle.esm.js:603
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.StoreWriter.writeSelectionSetToStore    @   bundle.esm.js:551
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.StoreWriter.writeFieldToStore   @   bundle.esm.js:649
(anonymous) @   bundle.esm.js:560
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.StoreWriter.writeSelectionSetToStore    @   bundle.esm.js:551
(anonymous) @   bundle.esm.js:702
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.StoreWriter.processArrayValue   @   bundle.esm.js:685
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.StoreWriter.writeFieldToStore   @   bundle.esm.js:631
(anonymous) @   bundle.esm.js:560
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.StoreWriter.writeSelectionSetToStore    @   bundle.esm.js:551
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.StoreWriter.writeResultToStore  @   bundle.esm.js:529
push../node_modules/apollo-cache-inmemory/lib/bundle.esm.js.InMemoryCache.write @   bundle.esm.js:875
push../node_modules/apollo-client/bundle.esm.js.DataStore.markQueryResult   @   bundle.esm.js:1781
push../node_modules/apollo-client/bundle.esm.js.QueryManager.markQueryResult    @   bundle.esm.js:1201
(anonymous) @   bundle.esm.js:1642
next    @   Observable.js:322
notifySubscription  @   Observable.js:135
onNotify    @   Observable.js:179
next    @   Observable.js:235
(anonymous) @   bundle.esm.js:866
next

我做错什么了?

我确实向阿波罗缓存添加了可能的类型,如docs:https://www.apollographql.com/docs/react/data/fragments/#defining-possibletypes-manually中所描述的。

possibleTypes.json

代码语言:javascript
复制
{"Commentable":["Comment","Post"],"Node":["Post","User"],"Notificable":["Comment","Post"]}

这没什么用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-01 15:27:17

当我将IntrospectionFragmentMatcher用于阿波罗InMemoryCache时,它起了很大的作用:https://www.apollographql.com/docs/react/v2/data/fragments/#fragments-on-unions-and-interfaces

票数 0
EN

Stack Overflow用户

发布于 2022-01-24 20:00:59

此错误是由于阿波罗InMemoryCache引发的。Refreshing the browser window solved the error

如果失败,请重新启动开发服务器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65994792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档