简体   繁体   English

中继突变不会更新我的UI

[英]Relay mutation does not update my UI

I have created this gist that represents my current case. 我已经创建了代表我当前案例的要点。 Basically I have an object with details, in this details I have an item selected from a list. 基本上,我有一个带有详细信息的对象,在这个详细信息中,我有一个从列表中选择的项目。 I have different types of list. 我有不同类型的列表。 And the selection has relation with the user too. 并且选择也与用户有关。

So I am calling a mutation in order to change the selected item, The mutations works as expected, but my UI remains outdate. 因此,我正在调用一个突变以更改所选项目。该突变按预期工作,但我的UI仍然过时。

https://gist.github.com/dobleuber/c74625704c37d7245b747d31b3c433cc https://gist.github.com/dobleuber/c74625704c37d7245b747d31b3c433cc

types.graphql types.graphql

type User @model {
  id
  userObjectSelection: [UserObjectSelection!]!
}

type Object @model {
  id
  name
  userObjectSelection: [UserObjectSelection!]!
  selectionType: SelectionType!
}

type SelectionType @model {
  id
  items: [SelectItem!]!
  objects: [Object!]!
}

type SelectItem @model {
  id
  name
  selectionType: SelectionType!
  userObjectSelection: [UserObjectSelection!]!
}

type UserObjectSelection @model {
  id
  user: User!
  object: Object!
  selectedItem: selectItem
}

ObjectPage.js ObjectPage.js

const query = graphql`
  query ObjectPageQuery($objectId: ID!, $userObjectSelectionId: ID!) {
    object: node(id: $objectId) {
      ...Object_object
    }
    selection: node(id: $userObjectSelectionId) {
      ...Object_selection
    }
  }
`;
changeSelectedItem (selectionId, itemId) {
  mutateSelectedItem(selectionId, itemId, () => console.log('updated'));
}

///
render={({ error, props }) => {
    if (error) {
      return <div>{error.message}</div>;
    } else if (props) {
      return (
        <div className="object-page">
          <Item
            object={props.object}
            selection={props.selection}
            onSelectItem={this.changeSelectedItem}
          />
        </div>
      );
    }

    return <div>Loading</div>;
  }}
///

Object.js Object.js

const Object = ({ object, selection, onSelectItem }) => {
  const {id} = object;
  return (
    <div>
      <span>{id}</span>
      <div className="item-list">
         object.selectionType.items.edges.map(({ node }) => (
              <Item
                key={node.__id}
                item={node}
                userObjectSelectionId={selection.id}
                onSelectCard={onSelectItem}
                selectedItem={selection.item}
              />
            ))
      </div>      
    </div>);
};

createFragmentContainer(Object, graphql`
  fragment Object_story on Object {
    details
    selectionType {
      id
      items: {
        edges: {
          node {
            ...Item_item
          }
        }
      }
    }
  }
  fragment Object_selection on UserObjectSelection {
    id
    selectedItem: item {
      ...Item_selectedCard
    }
  }
`);

mutation.js mutation.js

const mutation = graphql`
  mutation UpdateUserObjectSelectionMutation($input: UpdateUserObjectSelectionInput!) {
    updateUserObjectSelection(input: $input) {
      UserObjectSelection {
        id
      }
      SelectItem {
        id
      }
    }
  }
`;

After to check the code and debug the mutation and update code in relay, I found what the error was in my mutation result. 在检查代码并调试中继中的突变并更新代码后,我发现了突变结果中的错误。 I was returning the incorrect 'selectItem'. 我返回了不正确的“ selectItem”。

I updated my mutation.js and it's working properly right now. 我更新了mutation.js,它现在可以正常工作。

const mutation = graphql`
  mutation UpdateUserObjectSelectionMutation($input: UpdateUserObjectSelectionInput!) {
    updateUserObjectSelection(input: $input) {
      UserObjectSelection {
        id
        SelectItem {
          id
        }
      }
    }
  }
`;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM