anthonyg-1 / PSGraphQL

This PowerShell module contains functions that facilitate querying and create, update, and delete operations (mutations) for GraphQL, which is an an open-source data language for APIs. For more on GraphQL please see: https://graphql.org/.
MIT License
53 stars 8 forks source link

Passing parameters into a mutation #4

Closed pklotz-jdisc closed 3 years ago

pklotz-jdisc commented 3 years ago

Hi,

I do not understand from the documentation how to pass a parameter to a query or a mutation? In the examples hard-coded values are used instead of the graphql parameter mechanism. Could you make an additional example please?

Thanks, Peter

anthonyg-1 commented 3 years ago

Hi Peter I’m not sure I understand your question. If you take those examples you would substitute my query (defined in the $introspectionQuery variable for the first example) with your query. Variables in PowerShell aren’t necessarily hard-coded and the values could be dynamically derived. Could you give an example in another programming language of what you’re looking for?

anthonyg-1 commented 3 years ago

Hi Peter, the example below demonstrates dynamically constructing queries based on parameter values contained within an array. I hope this helps.

# List of movies we're going to iterate through to generate queries:
$movieList = @("HOPE", "EMPIRE", "JEDI")

# Our GraphQL endpoint
$movieGqlEndpoint = "http://myapi/starwars/v1/graphql"

# Iterate through each episode in the array, construct a query and pass on to Invoke-GraphQLQuery:
foreach ($episode in $movieList) {
    $movieQuery = "query HeroNameAndFriends($episode`: Episode) {
        hero(episode: $episode) {
            name
            friends {
                name
            }
        }
    }"

    Invoke-GraphQLQuery -Query $movieQuery -Uri $movieGqlEndpoint -Raw
}
pklotz-jdisc commented 3 years ago

Ah thanks, that was, what I was looking for Normally when I use a graphql APIs it allows me to specify the parameter separately as parameters to the Invoke-GraphQLQuery function but you simply evaluate them in the string context. Security-wise that is of course string evaluation but that's fine for the moment.

So I would simply use the Powershell variables $user and $password in the string " mutation($user: String, $password: String) { authentication { login(login: $user, password: $password) { status accessToken refreshToken rights } } }"

anthonyg-1 commented 3 years ago

Added the ability to take GraphQL query variables via the -Variables parameter in version 1.1.3.