3

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
  }
}
J. Jackson
  • 3,326
  • 8
  • 34
  • 74
  • 1
    just `mutation AddFriends($friends: [AddFriendsInput]!) { addFriends(input: $friends){` ... define variable type (matching BE signature) and use it as a whole object – xadm Nov 23 '20 at 23:21
  • This seems closer! I am getting an error in my GraphQL Playground console: `Uncaught Error: Introspection must provide input type for arguments, but received: [User]!`, and when I run the mutation anyways I get the error `"Variable \"$friends\" got invalid value [{\"email\":\"test@gmail.com\"}]; Field value[0].friends of required type [User]! was not provided."` – J. Jackson Nov 23 '20 at 23:26
  • this was about querying/graphql part ... IDK lighthouse - bad args definitions/directives (as passed value matches/is valid for this input type) ?.... return type mixed into the problem (mutation resolver doesn't retunr user array) – xadm Nov 23 '20 at 23:53
  • I mean I think it's still related to the graphql definitions, no? At this point it's not even hitting the resolver yet. – J. Jackson Nov 23 '20 at 23:57
  • I mean @spread doesn't make sense as only one arg and array type ... you can unify arg/vars naming: `mutation AddFriends($input: [AddFriendsInput]!) { addFriends(input: $input){` ... and return only some boolean 'success' to separate problems ... explore graphiql/playground docs, check input type, test mutation using query variables before coding FE – xadm Nov 24 '20 at 00:25

1 Answers1

3

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.

J. Jackson
  • 3,326
  • 8
  • 34
  • 74
  • You're right haha, I've been messing around with so many small changes, I didn't realize that's how it originally was when I posted the question. Thanks for your help! – J. Jackson Nov 24 '20 at 00:35