Fragment “F1” cannot be spread here as objects of

2019-09-19 03:09发布

问题:

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.