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
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/
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/
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