3

I want to update a person with the UpdatePerson mutation. I don't want to specify each and every property in the inputFields - but rather want to pass the complete person object.

When I do that I get Error: UpdatePersonInput.person field type must be Input Type but got: Person.

Is there no way to pass complete objects rather than all of their properties to a mutation?

If there isn't, could you add one - because the amount of repetition of fields across a larger app with bigger objects can become very frustrating.

Same might be an issue on getFatQuery and static fragments. Repeating all the properties over and over again would be a nightmare.

Server:

/**
 * 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;
      });
    });
  }
});

Client:

/**
 * 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;
0

1 Answer 1

9

It's error because your qlPerson type was defined by using the GraphQLObjectType class, which is not an Input type. You must define it using the GraphQLInputObjectType instead. Basically, both of them take an object as an argument which requires same properties. So, you just need to use GraphQLInputObjectType instead of GraphQLObjectType as following:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.