3

我想personUpdatePerson突变更新 a 。我不想在inputFields- 中指定每个属性,而是想传递完整的 person 对象。

当我这样做时,我得到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;
4

1 回答 1

9

这是错误的,因为您的qlPerson类型是使用GraphQLObjectType类定义的,而该类不是输入类型。您必须使用GraphQLInputObjectType来定义它。基本上,它们都将对象作为需要相同属性的参数。所以,你只需要使用GraphQLInputObjectType而不是GraphQLObjectType如下:

export default new GraphQLInputObjectType({
   name: 'qlPerson',
   description: 'Dietary preferences',
   fields: () => ({
     firstName: {type: GraphQLString},
     ...
   })
 });
于 2016-03-09T04:47:57.980 回答