Apollo-client - 错误:在对象 (ROOT_QUERY) 上找不到字段 allLinks()

Posted

技术标签:

【中文标题】Apollo-client - 错误:在对象 (ROOT_QUERY) 上找不到字段 allLinks()【英文标题】:Apollo-client - Error: Can't find field allLinks() on object (ROOT_QUERY)Apollo-client - 错误:在对象 (ROOT_QUERY) 上找不到字段 allLinks() 【发布时间】:2018-03-03 03:15:19 【问题描述】:

操作系统:Windows 10 专业版 浏览器:Opera 47.0.2631.71 (PGO) 阿波罗客户端:1.9.2 订阅-传输-ws:0.6.0 Vue:2.4.2 vue-apollo: 2.1.0-rc.5

因此,我在尝试使用 graphcool 端点运行查询时遇到了上述错误。仅在尝试对新创建的链接项目进行投票时才会出现此问题。这里有什么问题?

完整的错误是:

"allLinks(\"first\":5,\"skip\":0,\"orderBy\":\"createdAt_DESC\")": [
    
      "type": "id",
      "id": "Link:cj7ui2wn78joh0118s471fvgs",
      "generated": false
    ,
    
      "type": "id",
      "id": "Link:cj7ue9p2pyk9e0118w9dc3eav",
      "generated": false
    ,
    
      "type": "id",
      "id": "Link:cj7uaj5vwsa5b0126inei4jz0",
      "generated": false
    ,
    
      "type": "id",
      "id": "Link:cj7toqpwstxmp0168exm6lihp",
      "generated": false
    ,
    
      "type": "id",
      "id": "Link:cj7too3wgteq20154ax1agats",
      "generated": false
    
  ],
  "_allLinksMeta": 
    "type": "id",
    "id": "$ROOT_QUERY._allLinksMeta",
    "generated": true
  ,
  "allLinks(\"first\":5,\"skip\":5,\"orderBy\":\"createdAt_DESC\")": [
    
      "type": "id",
      "id": "Link:cj7toe2egt9j40115r74dzmzi",
      "generated": false
    ,
    
      "type": "id",
      "id": "Link:cj7to769uslpc0163giiymaeq",
      "generated": false
    ,
    
      "type": "id",
      "id": "Link:cj7tdamdkb2c401074qhjw2ig",
      "generated": false
    ,
    
      "type": "id",
      "id": "Link:cj7qlm4qb8vd501967t9jkvix",
      "generated": false
    ,
    
      "type": "id",
      "id": "Link:cj7qawaic5wgz0133mzzqvmat",
      "generated": false
    
  ]
.
    at readStoreResolver (readFromStore.js?f639:42)
    at executeField (graphql.js?206d:74)
    at eval (graphql.js?206d:31)
    at Array.forEach (<anonymous>)
    at executeSelectionSet (graphql.js?206d:26)
    at graphql (graphql.js?206d:20)
    at diffQueryAgainstStore (readFromStore.js?f639:75)
    at readQueryFromStore (readFromStore.js?f639:18)
    at TransactionDataProxy.readQuery (proxy.js?d9b2:114)
    at VueComponent.updateStoreAfterVote (LinkItem.vue?42d3:51)
tryFunctionOrLogError @ errorHandling.js?e4be:7
data @ store.js?2706:115
apolloReducer @ store.js?8997:42
combination @ combineReducers.js?a3f5:120
computeNextEntry @ VM314:2
recomputeStates @ VM314:2
(anonymous) @ VM314:2
dispatch @ createStore.js?8c3f:165
dispatch @ VM314:2
(anonymous) @ ApolloClient.js?f973:237
(anonymous) @ store.js?8997:16
(anonymous) @ QueryManager.js?2cbe:142
Promise resolved (async)
(anonymous) @ QueryManager.js?2cbe:127
QueryManager.mutate @ QueryManager.js?2cbe:124
ApolloClient.mutate @ ApolloClient.js?f973:197
mutate @ vue-apollo.esm.js?1632:3069
voteForLink @ LinkItem.vue?42d3:38
boundFn @ vue.runtime.esm.js?a427:180
click @ LinkItem.vue?219f:18
invoker @ vue.runtime.esm.js?a427:1817

查询:

export const ALL_LINKS_QUERY = gql`
query AllLinksQuery($first: Int, $skip: Int, $orderBy: LinkOrderBy) 
  allLinks(first: $first, skip: $skip, orderBy: $orderBy) 
    id
    createdAt
    url
    description
    postedBy 
      id
      name
    
    votes 
      id
      user 
        id
      
    
  
  _allLinksMeta 
    count
  

`

代码:

      voteForLink () 
        const userId = localStorage.getItem(GC_USER_ID)
        const voterIds = this.link.votes.map(vote => vote.user.id)
        if (voterIds.includes(userId)) 
          alert(`User ($userId) already voted for this link.`)
          return
        
        const linkId = this.link.id
        this.$apollo.mutate(
          mutation: CREATE_VOTE_MUTATION,
          variables: 
            userId,
            linkId
          ,
          update: (store,  data:  createVote  ) => 
            this.updateStoreAfterVote(store, createVote, linkId)
          
        )
      ,
      updateStoreAfterVote (store, createVote, linkId) 
        // 1
        const data = store.readQuery(
          query: ALL_LINKS_QUERY
        )

        // 2
        const votedLink = data.allLinks.find(link => link.id === linkId)
        votedLink.votes = createVote.link.votes

        // 3
        store.writeQuery( query: ALL_LINKS_QUERY, data )
      

【问题讨论】:

【参考方案1】:

好的,问题已通过将变量与 ALL_LINKS_QUERY 调用关联起来解决:

        const data = store.readQuery(
          query: ALL_LINKS_QUERY,
          variables: 
            first: 5,
            skip: 0,
            orderBy: 'createdAt_DESC'
          
        )

【讨论】:

老兄,你救了我!对于遇到此问题的任何人,请记住您还必须将 variables 传递给 writeQuery

以上是关于Apollo-client - 错误:在对象 (ROOT_QUERY) 上找不到字段 allLinks()的主要内容,如果未能解决你的问题,请参考以下文章

Apollo-client (react) - 更新创建突变 - “在对象 (ROOT_QUERY) 上找不到字段 Fund()”

如何在前端 apollo-client 上处理来自 Node/Koa 的后端错误

使用 React 钩子处理 graphQL 突变错误(useMutation + Apollo-client)

使用带有 NextJS getStaticPaths 和 getStaticProps (SSG) 的 apollo-client - 构建错误 - ECONNREFUSED

是否可以使用 apollo-client 跳过部分查询

使用 GET/HEAD 方法的请求不能有正文 APOLLO-CLIENT