在Mutation组件中的cache.writeQuery上缺少字段?

时间:2018-04-12 05:53:51

标签: graphql apollo apollo-client apollo-server

我正在研究GraphQL Mutation组件。我正在做一个增加分辨率的突变,即新年的分辨率。这是架构:

type Resolution {
  _id: String!
  name: String!
  goals: [Goal]
  completed: Boolean
}

type Query {
  resolutions: [Resolution]
}

type Mutation {
  createResolution(name: String!): {
        Resolution
        user: String
    }
}

以下是决议解析器:

import Resolutions from "./resolutions";
import Goals from "../goals/goals";
import { PubSub } from 'graphql-subscriptions';

export const pubsub = new PubSub();

export default {
    Query: {
        resolutions(obj, args, { userId }) {
            return Resolutions.find({
                userId
            }).fetch();
        }
    },

    Resolution: {
        goals: resolution =>
            Goals.find({
                resolutionId: resolution._id
            }).fetch(),

        completed: resolution => {
            const goals = Goals.find({
                resolutionId: resolution._id
            }).fetch();
            if (goals.length === 0) return false;
            const completedGoals = goals.filter(goal => goal.completed);
            return goals.length === completedGoals.length;
        }
    },

    Mutation: {
        createResolution(obj, { name }, { userId }) {
            if (userId) {
                const resolutionId = Resolutions.insert({
                    name,
                    userId
                });
                return Resolutions.findOne(resolutionId);
            }
            throw new Error("Unauthortized");
        }
    },
};

这是用户解析器:

export default {
  Query: {
    user(obj, args, { user }) {
      return user || {};
    }
  },
  User: {
    email: user => user.emails[0].address
  }
};

这是突变成分:

const ResolutionForm = () => {
    let input;
    let state = {
        error: null
    };

    return (
        <Mutation
            mutation={CREATE_RESOLUTION}
            update={(cache, {data: {createResolution}}) => {
                const {resolutions} = cache.readQuery({query: GET_RESOLUTIONS});
                cache.writeQuery({
                    query: GET_RESOLUTIONS,
                    data: {resolutions: resolutions.concat([createResolution])}
                });
            }}
        >
            {(createResolution, {data}) => (
                <div>
                    <form
                        onSubmit={e => {
                            e.preventDefault();
                            createResolution({
                                variables: {
                                    name: input.value
                                },
                            });
                            input.value = "";
                        }}
                    >
                        <input
                            ref={node => {
                                input = node;
                            }}
                        />
                        <button type="submit">Submit</button>
                    </form>
                </div>
            )}
        </Mutation>
    );
};

以下是在应用启动时加载所有分辨率的查询:

const GET_RESOLUTIONS = gql`
  query Resolutions {
    resolutions {
      _id
      name
      completed
      goals {
        _id
        name
        completed
      }
    }
    user {
      _id
    }
  }
`;

这很好,但是当我运行突变时:

const CREATE_RESOLUTION = gql`
    mutation createResolution($name: String!) {
      createResolution(name: $name) {
        __typename
        _id
        name
        goals {
          _id
          name
          completed
        }
        completed
      }
    }
`;

...我收到控制台日志错误说:

Missing field user in {
  "resolutions": [
    {
      "_id": "GKTNgbuiDgiZ4wAFZ",
      "name": "testing 123",
      .....

如何将字段user放入我的变异响应中?

1 个答案:

答案 0 :(得分:1)

使用的GET_RESOLUTIONS查询最初来自父组件App.js.它实际上包含两个单独的查询 - 一个用于解析,一个用于用户。 CREATE_RESOLUTION Mutation查询和解析器,不返回用户数据,我还不知道如何让他们这样做。

但是,Mutation组件不需要用户数据。它只会在调用cache.writeQuery时感到沮丧,因为GET_RESOLUTIONS要求user,并且Mutation解析器没有返回user

所以修复似乎是要有一个特殊的GET_RESOLUTIONS_FOR_MUTATION_COMPONENT查询,它首先不要求user

const GET_RESOLUTIONS_FOR_MUTATION_COMPONENT = gql`
  query Resolutions {
    resolutions {
      _id
      name
      completed
      goals {
        _id
        name
        completed
      }
    }
  }
`;

[.....]
     const {resolutions} = cache.readQuery({query: GET_RESOLUTIONS_FOR_MUTATION_COMPONENT});
[.....]

使用它没有要求user的错误消息。