RelayMutation 期望 prop 是由 Relay 获取的数据,将突变添加到查询不起作用

RelayMutation expects prop to be data fetched by Relay, adding mutation to query not working

我遇到错误:

bundle.js:28169 Warning: RelayMutation: Expected prop `group` supplied to `AddBlock` to be data fetched by Relay. This is likely an error unless you are purposely passing in mock data that conforms to the shape of this mutation's fragment.

它可能看起来与 中描述的问题类似,但答案(确保将突变添加到初始查询)对我来说不是一个解决方案。我已经在原始查询中进行了更改。

这是我的相关代码:

export class AddBlock extends Relay.Mutation {
    getMutation() {
        return Relay.QL`mutation { addBlock }`;
    }

    getVariables() {
        return {
            body: this.props.body
        };
    }

    getFatQuery() {
        return Relay.QL`
            fragment on AddBlock {
                newBlockEdge,
                group {
                    blocks
                }
            }
        `;
    }

    getConfigs() {
        return [{
            type: 'RANGE_ADD',
            parentName: 'group',
            parentID: this.props.group.id,

            connectionName: 'blocks',
            edgeName: 'newBlockEdge',
            rangeBehaviors: {
                '': 'append',
            },
        }];
    }


    getOptimisticResponse() {
        return {
            newBlockEdge: {
                node: {
                    body: this.props.body
                }
            },
            group: {
                id: this.props.group.id
            }
        }
    }


    static get fragments() {
        return {
            group: () => Relay.QL`
                fragment on GroupNode {
                    id
                }
            `,
        }
    }
}


class Designer extends React.Component {
    ...

    addToBlocks(blocks) {
        // Create a mutation to save to the blocks.
        Relay.Store.commitUpdate(
            new AddBlock({
                body: blocks[0].block,
                group: this.props.group
            })
        );
    }
    ...
}


Designer = Relay.createContainer(Designer, {
  fragments: {

    group: (Component) => Relay.QL`
      fragment on GroupNode {
        title
        visibility
        blocks(first: 20) {
            edges {
                node {
                    ${Block.getFragment('block')}
                    ${UpdateBlockBodyMutation.getFragment('block')}
                    position
                    title
                }
            }
        }
        ${AddBlock.getFragment('group')}
      }
    `,
  }
});

我在这里做错了什么?

我怀疑您的突变片段实际上并没有被使用 - 您应该 运行 进行测试,看看如果您向 AddBlock 片段添加其他字段,您会发现它们没有被使用被要求...?我不是 100% 确定原因(可能与 static get fragments 有关),但不太确定。

一旦你的变异片段被实际使用,Relay 就不会再抱怨了,因为它会以正确的方式获取数据:D