Fragment “F1” cannot be spread here as objects of type “Node” can never be of type “Store”

梦想的初衷 提交于 2019-12-25 18:36:18

问题


I have written below code :

On Certain User Action below method is called :

handleFolderClicked = (topic, parent) => {
    console.log('Main : handleFolderClicked called : ', parent);
    this.props.relay.setVariables({
      parentFolder: parent,
      email: loginEmail
    });
  }

Main = Relay.createContainer(Main, {
  initialVariables: {
    pageSize: pageSize,
    email: loginEmail,
    parentFolder: parentFolder,
  },
  fragments: {
    store: () => Relay.QL`
      fragment on Store {
        id,
        fileConnection(first:999, email:$email, parentFolder:$parentFolder) {
          ${DirectoryListing.getFragment('files')},
        }
        users {
          email,
        }
      }
    `,
  }
});
export default Main;

On the time of page load its working fine and fetching results but when value of parentFolder is changed using setVariables, it throws above error.

An insight might be useful and appreciated. Thanks


回答1:


I had this error and I solved with something like this:

Store has to implement Relay's Node interface, meaning that it has to provide an id: ID! field, and if you do a node query using the returned ID, your Store has to be returned.

In my case I named my root object viewer of type Viewer; so after implementing the Node interface, this query has to work:

{
  viewer {
    id
  }
}

Which returns:

{
  "data": {
    "viewer": {
      "id": "Vmlld2VyOk5vbmU="
    }
  }
}

Now if I use that ID to run a node Query, the viewer has to be returned. So for the query:

{
  node(id: "Vmlld2VyOk5vbmU=") {
    id,
    __typename
  }
}

This should be the result:

{
  "data": {
    "node": {
      "id": "Vmlld2VyOk5vbmU=",
      "__typename": "Viewer"
    }
  }
}

Replace all occurrences of viewer/Viewer with store/Store and it should work.



来源:https://stackoverflow.com/questions/42552256/fragment-f1-cannot-be-spread-here-as-objects-of-type-node-can-never-be-of-ty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!