我想更新帶有UpdatePerson
突變的person
。我不想在inputFields
中指定每個屬性 - 而是想要傳遞完整的人員對象。錯誤:字段類型必須爲輸入類型 - 無法將qlType傳遞到突變
當我這樣做,我得到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;