Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define GraphQL input type in vue-apollo client

Situation:

I am using laravel-graphql as a backend GraphQL service and vue-apollo as a frontend client.

In my Vue component I have a gql query as follows: (relation: A Song has more SongParts)

const getSong = gql`
    query FetchSong ($id:Int!){
        song(id: $id) {
            id,
            name,
            authors_lyrics,
            authors_music
            song_parts{
                type
                lyrics
            }
        }
    }`;

Now there's a mutation that i want to use to update given song:

const updateSong = gql`
    mutation updateSongMutation ($id:Int!,$name:String,$authors_lyrics:String,$authors_music:String, $song_parts: [SongPart]) {
        updateSongMutation(id: $id, name: $name, authors_lyrics: $authors_lyrics, authors_music: $authors_music, song_parts: $song_parts) {
            id
        }
    }`;

.. and in the Vue component used this way:

methods: {
            save: function(){
                this.$apollo.mutate({
                    mutation: updateSong,
                    variables: this.songData,
                }).then((result) => {
                    console.log(result);
                }).catch((error) => {
                    console.log(error);
                })
            }
        },

However, to update the song_parts array I need to define a custom input type:

input SongPart{
    id: Int!
    lyrics: String
}

Problem

I can't figure out, where to define this input type. When I'm using GraphiQL in Chrome WebTools, there's no problem defining it in front of the mutation. I also found some info for apollo server, but I need it in the client.

So basically the problem is about mutating a list of data.

like image 743
thes01 Avatar asked Dec 07 '25 13:12

thes01


1 Answers

Your input types should still be defined inside your schema on the server. I'm not that familiar with laravel, but looking at the documentation for laravel-graphql, it looks like at the moment inputs are defined just like regular types, you just set the $inputObject property to true. See the example here. I would imagine you would then add them to your types inside config/graphql.php along with the regular types.

I would also suggest renaming the input to something like SongPartInput so you don't get confused if you also have a type for SongPart.

like image 164
Daniel Rearden Avatar answered Dec 09 '25 03:12

Daniel Rearden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!