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

Chr*_*ine 3 reactjs graphql relayjs

我想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;
      });
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

客户:

/**
 * 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;
Run Code Online (Sandbox Code Playgroud)

lva*_*yut 9

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

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