Skip to content

Nested mutations #672

Closed
Closed
@stringbeans

Description

@stringbeans

Say I want to do the following:

mutation {
  updateStudent(studentId: 5, input: $input) {
    firstname,
    lastname,
    studentTags {
      value
    }
  }
}

where $input =

{
  firstname: 'joe',
  lastname: 'cool',
  studentTags: [{
    value: 'summer 2016'
  }]
}

My Student input definition is as follows:

new GraphQLInputObjectType({
  name: 'StudentInput',
  fields: {
    firstname: {
      type: GraphQLString
    },
    lastname: {
      type: GraphQLString
    },
    email: {
      type: GraphQLString
    },
    studentTags: {
      type: new GraphQLList(StudentTagInput)
    }
  }
});

... and my mutation definition looks like this:

export default {
  type: StudentObject,
  args: {
    studentId: {
      type: new GraphQLNonNull(GraphQLInt)
    },
    input: {
      type: StudentInput
    }
  },
  resolve(_, {studentId, input}, context) {
    return ACL.Common.enforceLoggedIn(context.user)
      .then(() => Models.Student.update(context, studentId, input))
  }
}

Is there any way to ease the concept of nested mutations in this case? Almost like having a resolve function attached to the StudentTagInputObject such that it runs whenever a student mutation happens, or having non-root mutations?

The only way I can see how to implement this is defining how the nested mutation will work within the resolve function of the root mutation directly but could lead to duplicated code.

Thanks in advance! Hopefully my question makes sense

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions