~sircmpwn/sr.ht-discuss

3 2

GraphQL mutation execution

Details
Message ID
<d8def62a-22a9-e0ba-256c-6047b7de1fa8@agragps.com>
DKIM signature
missing
Download raw message
Hello all, I'm very new to SourceHut and GraphQL.  I'm trying to execute 
a 'mutation' to update the ACL of a mercurial repo.

Using the following command:
curl  \
   --oauth2-bearer "$oauth_token" \
   -H 'Content-Type: application/json' \
   -d '{"mutation": "{ updateACL(repoId: 77777, mode: RW, entity: 88888) 
}"}' \
https://meta.sr.ht/query

(Obviously I've obfuscated the actual id numbers, but I can guarantee 
that 77777 is a repo that I own, and 88888 is a valid user).  I get the 
response:
{"errors":[{"message":"no operation 
provided","extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}

If I try it like this:
curl  \
   --oauth2-bearer "$oauth_token" \
   -H 'Content-Type: application/json' \
   -d '{"query": "mutation { updateACL(repoId: 77777, mode: RW, entity: 
88888) }"' \
https://meta.sr.ht/query

Then I get the response:
{"errors":[{"message":"Cannot query field \"updateACL\" on type 
\"Mutation\". Did you mean 
\"updateUser\"?","locations":[{"line":1,"column":12}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}

Not sure how to make this mutation work.  You may wonder why I'm trying 
to do this, when I can just assign access through the web interface, but 
I have a large number of repos and several users to deal with and I want 
to automate this.

Any help is appreciated!

--Dave
Details
Message ID
<20230218222805.l3mm526buotfq7mj@xenrox.net>
In-Reply-To
<d8def62a-22a9-e0ba-256c-6047b7de1fa8@agragps.com> (view parent)
DKIM signature
missing
Download raw message
On Sat, Feb 18, 2023 at 02:05:08PM -0700, Dave Moerman wrote:
> If I try it like this:
> curl  \
>   --oauth2-bearer "$oauth_token" \
>   -H 'Content-Type: application/json' \
>   -d '{"query": "mutation { updateACL(repoId: 77777, mode: RW, entity:
> 88888) }"' \
> https://meta.sr.ht/query

Hello,

There are several problems with your query.
At the moment the GraphQL API is not yet federated. That means that if
you want to execute a "hg" mutation, you have to send it to
"https://hg.sr.ht/query".

Secondly you always have to select a field to return, even when doing a
mutation. In this case you could simple return the "id" of the created
ACL or maybe the timestamp with "created".

The third problem is that the ID field here does not really take an ID,
but a canonical username like "~xenrox".

All in all this should be a working mutation:

curl
    --oauth2-bearer $TOKEN \
    -H 'Content-Type: application/json' \
    -d '{"query": "mutation{ updateACL(repoId: 77777, mode: RW, entity: \"~foobar\"){id} }"}' \
    https://hg.sr.ht/query

Let me know, if you run into more problems.

In the future hut[1] will have support for this to make it a bit easier.

[1]: https://sr.ht/~emersion/hut/
Details
Message ID
<150a604e-9f99-1378-ad3b-ca9f523655c8@agragps.com>
In-Reply-To
<20230218222805.l3mm526buotfq7mj@xenrox.net> (view parent)
DKIM signature
missing
Download raw message
Yes, it worked perfectly, and thank you for the tips!


On 2023-02-18 3:28 p.m., Thorben Günther wrote:
> On Sat, Feb 18, 2023 at 02:05:08PM -0700, Dave Moerman wrote:
>> If I try it like this:
>> curl  \
>>    --oauth2-bearer "$oauth_token" \
>>    -H 'Content-Type: application/json' \
>>    -d '{"query": "mutation { updateACL(repoId: 77777, mode: RW, entity:
>> 88888) }"' \
>> https://meta.sr.ht/query
> Hello,
>
> There are several problems with your query.
> At the moment the GraphQL API is not yet federated. That means that if
> you want to execute a "hg" mutation, you have to send it to
> "https://hg.sr.ht/query".
>
> Secondly you always have to select a field to return, even when doing a
> mutation. In this case you could simple return the "id" of the created
> ACL or maybe the timestamp with "created".
>
> The third problem is that the ID field here does not really take an ID,
> but a canonical username like "~xenrox".
>
> All in all this should be a working mutation:
>
> curl
>      --oauth2-bearer $TOKEN \
>      -H 'Content-Type: application/json' \
>      -d '{"query": "mutation{ updateACL(repoId: 77777, mode: RW, entity: \"~foobar\"){id} }"}' \
>      https://hg.sr.ht/query
>
> Let me know, if you run into more problems.
>
> In the future hut[1] will have support for this to make it a bit easier.
>
> [1]: https://sr.ht/~emersion/hut/
Details
Message ID
<7cc32990-9f49-cc1f-effe-d2cc2acf221c@agragps.com>
In-Reply-To
<150a604e-9f99-1378-ad3b-ca9f523655c8@agragps.com> (view parent)
DKIM signature
missing
Download raw message
I have a follow-up question.  Let's say I have a large number of repos, 
and I want to give a user read/write access to all of them.  I could try 
to execute a series of mutuations, something like this:

   {
     "query" :
       "mutation AddUserToRepo($repoIdent: Int!, $axsMode: AccessMode!, 
$entityIdent: ID!)
       {
         updateACL(repoId: $repoIdent, mode: $axsMode, entity: $entityIdent)
         {
           id,
           created
         }
       }",
     "variables" : [
       {
         "repoIdent" : 66666,
         "axsMode" : "RW",
         "entityIdent" : "~_userA_"
       },
       {
         "repoIdent" : 77777,
         "axsMode" : "RW",
         "entityIdent" : "~_userA_"
       } ]
   }

I get the response:
{"errors":[{"message":"json body could not be decoded: json: cannot 
unmarshal array into Go value of type graphql.RawParams"}

So clearly it doesn't like the array I've specified for the variables.  
I've tried some variations on this theme using the patterns shown in 
https://github.com/jaydenseric/graphql-multipart-request-spec, but 
without luck.

Of course, I could execute consecutive curl commands for each set of 
variables, but I was wondering if there is a more efficient way.

--Dave
Reply to thread Export thread (mbox)