错误:字段类型必须是输入类型 - 无法将 qlType 传递到变异中

IT技术 reactjs relayjs graphql
2021-05-13 22:00:59

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

当我这样做时,我得到 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;
1个回答

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

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