为什么Apollo客户端查询组件在突变本地状态(Apollo-llink-state)之后返回不同格式的数据?

时间:2018-07-20 02:04:23

标签: graph apollo react-apollo apollo-client

我正在使用具有Apollo-link-state的Apollo客户端,下面是我的代码

这是我的默认状态

const defaultState: any = {
    info: {
        currentSelectItem: {
            id: -1,
            name: '',
            key: 'all',
            __typename: 'CurrentlyItemInfo',
        },
        currentSelectStore: '',
        __typename: 'ItemInfo',
    },
};

下面是我的解析器

resolvers: {
    Mutation: {
        changeItem: (_: any, { id, name, key }: any, { cache }: any): any => {
            console.log('==== changeItem ====');
            console.log(id);
            console.log(name);
            console.log(key);
            const data: any = {
                info: {
                    currentSelectItem: {
                        id,
                        name,
                        key,
                        __typename: 'CurrentlyItemInfo',
                    },
                    __typename: 'ItemInfo',
                },
            };
            console.log(data);
            cache.writeData({ data });
            return null;
        },
    },
},

下面是突变gql

changeCategory({ variables: { id: 888, name: 'cookie', key: 'food' } });

const CHANGE_ITEM: any = gql`
  mutation changeItem($id: Number, $name: String, $key: String ) {
    changeItem(id: $id, name: $name, key: $key) @client
  }
`;

我将状态更改为数据后,查询组件将再次查询,为什么它会像下面那样为我提供新的数据格式

gameInfo:
    currentSelectItem:
        generated:false
        id:"CurrentlyItemInfo:888"
        previousResult:{type: "id", generated: false, id: "CurrentlyItemInfo:3", typename: "CurrentlyItemInfo", previousResult: {…}}
        type:"id"
        typename:"CurrentlyItemInfo"
        __proto__:Object
        currentSelectStore:""
        Symbol(id):"$ROOT_QUERY.info"
        __typename:"ItemInfo"
        __proto__:Object
        Symbol(id):"ROOT_QUERY"

我希望数据会像

一样返回
info: {
        currentSelectItem: {
            id: 888,
            name: 'cookie',
            key: 'food',
            __typename: 'CurrentlyItemInfo',
        },
        currentSelectStore: '',
        __typename: 'ItemInfo',
    },

为什么会这样?

0 个答案:

没有答案