How to push new item_id to a relation field in graphql mutation

System Information
  • Strapi Version: 4.14.2
  • Operating System: linux
  • Database: mysql
  • Node Version: 16
  • NPM Version:
  • Yarn Version:

mutation code:

mutation updateFollows (
  $updateUsersPermissionsUserId: ID!,
  $data: UsersPermissionsUserInput!,
  ){
    updateUsersPermissionsUser(id: $updateUsersPermissionsUserId, data: $data
    ){
    data{
      id
      attributes{
        follows {
          data {
            id
          }
        }
      }
    }
  }
}

update_parmas is:

{
  "updateUsersPermissionsUserId": 1,
  "data": {
    "follows": [1,2,3,4]
  }
}

if I want push new id 5 to follows,
i can set “follows”: [1,2,3,4,5],
ofcs it worked well,

but my question is:
how to push 5,not renew follows array,

because the old data of follows maybe have follows.length > 10000000000,I can’t get them all and renew a new array and set it

thanks to all

Practically if you have a bidirectionnal relationship and if the other way around is simpler you can update the followed and not the follower. If it’s not the case then… good question.

You can write the src/index.ts file like this:

import {Event} from "@strapi/database/dist/lifecycles";

export default {
  /**
   * An asynchronous register function that runs before
   * your application is initialized.
   *
   * This gives you an opportunity to extend code.
   */
  register(/*{ strapi }*/) {},

  /**
   * An asynchronous bootstrap function that runs before
   * your application gets started.
   *
   * This gives you an opportunity to set up your data model,
   * run jobs, or perform some special logic.
   */
  bootstrap(/*{ strapi }*/) {
    strapi.db.lifecycles.subscribe({
      models: ['plugin::users-permissions.user'],

      // your lifecycle hooks
      async beforeUpdate(event: Event) {

        const user = await strapi.query('plugin::users-permissions.user').findOne({
          where: event.params.where,
          populate: {
            follows: {
              select: ['id']
            }
          }
        })

        if (user) {
          event.params.data.follows = [...user.follows, ...event.params.data.follows]
        }
      }
    })
  },
};

So you just need to use that query variables :
{ "updateUsersPermissionsUserId": 1, "data": { "follows": [4] } }

And you have the previous follows plus the id 4 for user 1

You can also modifying update methods for user