Lighthouse GraphQL - how to pass array as variable?

2.9k Views Asked by At

I'm working to build a simple Facebook clone to build up my knowledge of GraphQL. I'm using lighthouse-php on top of Laravel to serve my data. Essentially, a user has many friends, and I know this works because in the tinker console I can return the correct data with something like $user->friends()->get(). I'm struggling with figuring out how to pass an array of data.

On my frontend, I've got a Vuetify combobox (basically a select dropdown) that builds an array of emails, and this is what I'm trying to pass over to my backend mutation.

How do I structure the type and mutation so that it accepts an array of emails?

user.graphql:

extend type Mutation @guard(with: ["api"]) {
    addFriends(input: [AddFriendsInput]! @spread): [User]! @field(resolver: "UserMutator@addFriends")
}

type User {
    id: ID!
    email: String!
    password: String!
    first_name: String
    last_name: String
    created_at: DateTime!
    updated_at: DateTime!

    friends: [User]! @belongsToMany(relation: "friends")
}

input AddFriendsInput {
    email: String!
}

addFriend.gql:

mutation AddFriends($friends: [AddFriendsInput]!) {
  addFriends(input: { email:[$friends] }){
    id
    email
  }
}
1

There are 1 best solutions below

1
On BEST ANSWER

Ok finally got it figured out.

Modified the input to this:

input AddFriendsInput {
  email: String!
  # friends: [User]
}

and changed my Mutation to this:

mutation AddFriends($friends: [AddFriendsInput]!) {
  addFriends(input: $friends) {
    id
    email
  }
}

This allows me to pass an array of emails to my resolver now.