我想用person
突变更新UpdatePerson
。我不想在inputFields
中指定每个属性 - 而是想要传递完整的人物对象。
当我这样做时,我得到Error: UpdatePersonInput.person field type must be Input Type but got: Person.
有没有办法将完整的对象而不是所有属性传递给变异?
如果没有,可以添加一个 - 因为较大的应用程序中具有较大对象的字段重复量会变得非常令人沮丧。
同样可能是getFatQuery
和static fragments
上的问题。一遍又一遍地重复所有属性将是一场噩梦。
服务器:
/**
* Create the GraphQL Mutation.
*/
export default mutationWithClientMutationId({
// Mutation name.
name: 'UpdatePerson',
// Fields supplied by the client.
inputFields: {
person: {type: qlPerson} // <========================================
},
// Mutated fields returned from the server.
outputFields: {
person: {
type: qlPerson,
// Parameters are payload from mutateAndGetPayload followed by outputFields.
resolve: (dbPerson, id, email) => {
return dbPerson;
}
}
},
// Take the input fields, process the mutation and return the output fields.
mutateAndGetPayload: ({qlPerson}, {rootValue}) => {
// TODO: Process Authentication {"session":{"userId":1}}
console.log(JSON.stringify(rootValue));
// Convert the client id back to a database id.
var localPersonId = fromGlobalId(qlPerson.id).id;
// Find the person with the given id in the database.
return db.person.findOne({where: {id: localPersonId}}).then((dbPerson)=> {
// Mutate the person.
dbPerson.email = qlPerson.email;
// Save it back to the database.
return dbPerson.save().then(()=> {
// Return the mutated person as an output field.
return dbPerson;
});
});
}
});
客户端:
/**
* Create the GraphQL Mutation.
*/
class UpdatePersonMutation extends Relay.Mutation {
getMutation() {
return Relay.QL`mutation {updatePerson}`;
}
getVariables() {
return {person: this.props.person}; // <========================================
}
getFatQuery() {
return Relay.QL`
fragment on UpdatePersonPayload {
person {
email, // ??????????????????????????
}
}
`;
}
getConfigs() {
return [{
type: 'FIELDS_CHANGE',
fieldIDs: {
person: this.props.person.id
}
}];
}
static fragments = {
person: () => Relay.QL`
fragment on Person {
id,
email // ???????????????????????????
}
`
};
getOptimisticResponse() {
return {
person: this.props.person
};
}
}
/**
* Exports.
*/
export default UpdatePersonMutation;
答案 0 :(得分:7)
这是错误的,因为您的qlPerson
类型是使用GraphQLObjectType
类定义的,该类不是输入类型。您必须使用GraphQLInputObjectType
来定义它。基本上,它们都将对象作为需要相同属性的参数。因此,您只需使用GraphQLInputObjectType
代替GraphQLObjectType
,如下所示:
export default new GraphQLInputObjectType({
name: 'qlPerson',
description: 'Dietary preferences',
fields: () => ({
firstName: {type: GraphQLString},
...
})
});