2016-01-04 29 views
3

我想更新帶有UpdatePerson突變的person。我不想在inputFields中指定每個屬性 - 而是想要傳遞完整的人員對象。錯誤:字段類型必須爲輸入類型 - 無法將qlType傳遞到突變

當我這樣做,我得到Error: UpdatePersonInput.person field type must be Input Type but got: Person.

有沒有辦法通過完整的對象,而不是所有的屬性來突變?

如果沒有,您是否可以添加一個 - 因爲跨較大應用程序的大對象重複的字段數量可能會變得非常令人沮喪。

這可能是getFatQuerystatic 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; 

回答

6

這是因爲你的qlPerson類型是通過使用GraphQLObjectType類,它是不是一個輸入類型定義的錯誤。您必須改用GraphQLInputObjectType來定義它。基本上,他們都把一個對象當作一個需要相同屬性的論點。所以,您只需要使用GraphQLInputObjectType而不是GraphQLObjectType,如下所示:

export default new GraphQLInputObjectType({ 
    name: 'qlPerson', 
    description: 'Dietary preferences', 
    fields:() => ({ 
    firstName: {type: GraphQLString}, 
    ... 
    }) 
}); 
相關問題