reactjs – 错误:字段类型必须是输入类型 – 不能将qlType传递给变异

我想用UpdatePerson变异更新一个人.我不想在inputFields中指定每个属性 – 而是想要传递完整的person对象.

当我这样做时,我得到错误:UpdatePersonInput.person字段类型必须是输入类型但得到:人.

有没有办法将完整的对象而不是所有属性传递给变异?

如果没有,你可以添加一个 – 因为较大的应用程序中具有较大对象的字段重复量会变得非常令人沮丧.

同样可能是getFatQuery和静态片段的问题.一遍又一遍地重复所有属性将是一场噩梦.

服务器:

/**
 * 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;
最佳答案
这是错误的,因为您的qlPerson类型是使用GraphQLObjectType类定义的,该类不是Input类型.您必须使用GraphQLInputObjectType来定义它.基本上,它们都将对象作为需要相同属性的参数.因此,您只需要使用GraphQLInputObjectType而不是GraphQLObjectType,如下所示:

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

转载注明原文:reactjs – 错误:字段类型必须是输入类型 – 不能将qlType传递给变异 - 代码日志