Search code examples
graphqlrelayjsgraphql-mutation

GraphQL/Relay - How to pass a mutation's input variable into a query on the mutation response?


Given a mutation like this (see how nextArticle needs an ID passing):

mutation markArticleCompleteMutation(
    $input: markArticleCompleteInput!
) {
    markArticleComplete(input: $input) {
        user {
            nextArticle(excludeArticleId: $excludeArticleId) {
                id
            }
        }
    }
}

// Input looks like:

{
  "input": {"completedId": "id123"}
}

how can I pass this from one of the input props?

I'd ideally like to write something like nextArticle(excludeArticleId: $input.completedId) but this doesn't seem to work


Solution

  • AFAIK you cannot access it like this. You have to define a separate variable and use it.

    Notice the excludeArticleId is added to the list of variables of your document and it is defined separately in the variables map.

    mutation markArticleCompleteMutation(
        $input: markArticleCompleteInput!,
        $excludeArticleId: String!
    ) {
        markArticleComplete(input: $input) {
            user {
                nextArticle(excludeArticleId: $excludeArticleId) {
                    id
                }
            }
        }
    }
    

    Then the variables should be like this:

    {
        "input": {"completedId": "id123"},
        "excludeArticleId": "id123"
    }