useMutation 结果返回奇怪的结构/不返回请求的字段

useMutation result returning weird structure/ not returning requested fields

我正在尝试做的事情: 使用 useMutation 将数组的数组提交给 mongoDB。

发生了什么: 它成功地保存在 mongoDb 中并且格式正确 但是 'result.data' of useMutation 不符合预期,太奇怪了,不能用

这是我的数组(当然,它开始时是一个空数组,随着用户在他们的行程中添加吸引力而被推入)。

let itinerary = [{placeIds: ["ChIJafBhcoVPqEcRb7rHEy3G0L8", "ChIJBRk3gUlOqEcR9oEBV-dqK5M"]},
{placeIds: ["ChIJx8Iw5VFOqEcRXUgfWxkmAaA", "ChIJSdWeck5OqEcReSTr3YfoSuE"]}, 
{placeIds: ["ChIJ1WCXcFJOqEcRBR_ICa3TemU"]}]

这是 graphql 突变:

const SUBMIT_ITINERARY =  gql`
        mutation submitItinerary(
            $dayPlans: [DayPlanInput]
        ){
            submitItinerary(
                dayPlans: $dayPlans
            ){
                id
                dayPlans{
                    placeIds
                }
                createdAt
            }
        }
    `

这是 apollo react hook useMutation:

const [submitItinerary] = useMutation(SUBMIT_ITINERARY, {
        update(result){
            console.log(result.data);
        },
        onError(err){
            console.log(err)
        },
        variables: {
            dayPlans: itinerary
        }
    })

在我的 graphql 操场上我得到了这个

所以我想在我的前端我会得到类似的东西但是我得到的是:

为什么会这样? 我想得到一个类似于我在 graphql playground 中得到的结构。


更多可能有用的信息: 这是我的行程安排:

const itinerarySchema = new Schema({
    city: String,
    username: String,
    createdAt: String,
    dayPlans: [],
    user: {
        type: Schema.Types.ObjectId,
        ref: 'users',
    }
});

我的类型定义:

type DayPlan {
    placeIds: [String]!
}
type Itinerary {
    id: ID!
    dayPlans: [DayPlan]!
    username: String!
    createdAt: String!
}
input DayPlanInput {
    placeIds: [String]
}
input RegisterInput {
    username: String!
    password: String!
    confirmPassword: String!
    email: String!
}
type Query {
    getUsers: [User]
}
type Mutation {
    register(registerInput: RegisterInput): User!
    login(username: String!, password: String!): User!
    submitItinerary(dayPlans: [DayPlanInput] ): Itinerary!
}

和我的 submitItinerary 解析器:

Mutation: {
        async submitItinerary(_, {dayPlans}, context) {
            const user = checkAuth(context);
            //console.log(user);

            if (dayPlans.length === 0){
                throw new Error('Itinerary should not be empty');
            }

            const newItinerary = new Itinerary({
                dayPlans,
                user: user.id,
                username: user.username,
                createdAt: new Date().toISOString()
            })

            const submitted = await newItinerary.save()

            return submitted;
        }
    }

useMutationupdate选项的第一个变量是Apollo Cache本身而不是变异结果。变异结果可以在第二个参数中找到。 Here you can find the API docs.

这应该记录正确的数据:

const [submitItinerary] = useMutation(SUBMIT_ITINERARY, {
    update(cache, result){
        console.log(result.data);
    },
    onError(err){
        console.log(err)
    },
    variables: {
        dayPlans: itinerary
    }
})

另一种访问变异结果的方法是使用挂钩中的第二个元素 return 值:

const [submitItinerary, { data, loading, error }] = useMutation(SUBMIT_ITINERARY)