I am trying to get the schema for GitHub using get-graphql-schema but am getting an error :-
# npm install -g get-graphql-schema
# get-graphql-schema https://api.github.com/graphql
*TypeError: Cannot read property '__schema' of undefined
at Object.buildClientSchema
(C:\Users\Aaron\AppData\Roaming\npm\node_modules\get-graphql-schema\node_modules\graphql\utilities\buildClientSchema.js:48:43)*
I get the same for Facebook I am not sure if this is because they don't give the schemas or whether its an error with get-graphql-schema.
How do I programmatically get the schema using GraphQl normally in the GraphiQL explorer ?
Okay heres a GraphQL script that programmatically queries the __schema entry :-
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
args {
...InputValue
}
onOperation
onFragment
onField
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
https://gist.github.com/AaronNGray/c873c093f7015389cd7358d7270e1e1e
Related
How to write a GraphQL query to retrieve all the workflows/runs from github
I tried below query for to get node id
organization(login: "abc") {
repositories(first: 100) {
nodes {
id
name
}
}
}
and below query to get workflow.
nodes(ids: "sdefrgrt") {
... on Workflow {
id
createdAt
name
updatedAt
}
}
Yes, this should be available via GraphQL (you can try this query out with Github's GraphQL explorer):
{
repository(owner: "$ORG", name: "$REPO") {
pullRequest(number: $NUMBER) {
commits(first: 5) {
edges {
node {
commit {
checkSuites(first: 5) {
nodes {
workflowRun {
url
workflow {
name
}
}
}
}
}
}
}
}
}
}
}
You'd adjust the first parameters, and the client would need to do some grouping on the data that comes back from GraphQL.
You may get the workflow runs via:
{
node(id: "YOUR-WORKFLOW-NODE-ID") {
... on Workflow {
runs(first: 10) {
nodes {
runNumber
createdAt
checkSuite {
commit {
message
associatedPullRequests(first: 1) {
nodes {
number
}
}
history {
nodes {
author {
date
email
name
}
}
}
}
}
}
}
}
}
}
Workflow's node_id could be taken from https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow
I am trying to get from GitHub using graphQL multiple organizations & repositories data. The code i wrote below get only 1 organization & repository.
I thought using variables of two Array [String!] one for Organizations & the second for Repositories.
So
organization(login:"javascript") {...}
should be
organization(login:$organization) {...}
and
repository(owner:"javascript", name:"sorted-array") {...}
should be
repository(owner:$organization, name:$repository) {...}
But i couldn't find how to implement the variables into the below code.
query {
rateLimit{
cost
remaining
resetAt
}
organization(login:"javascript") {
repositories{
totalCount
}
}
repository(owner:"javascript", name:"sorted-array") {
updatedAt
branches: refs(refPrefix:"refs/heads/") {
totalCount
}
tags: refs(refPrefix:"refs/tags/") {
totalCount
}
releases {
totalCount
}
object(expression:"master") {
... on Commit {
committedDate
history {
totalCount
}
}
}
}
}
Will appreciate the help.
Thanks
Here is the request updated to work with variables
query getOrg($owner: String!, $repo: String! ){
organization(login:$owner) {
repositories{
totalCount
}
}
repository(owner:$owner, name:$repo) {
updatedAt
branches: refs(refPrefix:"refs/heads/") {
totalCount
}
tags: refs(refPrefix:"refs/tags/") {
totalCount
}
releases {
totalCount
}
object(expression:"master") {
... on Commit {
committedDate
history {
totalCount
}
}
}
}
}
However, you may be better served by using GraphQL Node IDs as an array of inputs. With the Node ID of the Organization or Repository, something along the lines of...
query inputArray($id: [ID!]!){
nodes(ids: $id){
...on Repository{
id
}
}
}
Note that GitHub V3 of the API supports returning the GraphQL Node IDs to help transition to GraphQL queries.
Here are my solutions i hope it will be helpful to someone:)
First answer is using the Array variables with multiple id's of the organizations "Facebook","JavaScript" and
the repositories "360-Capture-SDK","sorted-array".
If you have 10 ,20 organizations/repositories or more , you will have a great time fetching the id's from the REST API:).
query inputArray($idOrg: [ID!]! $idRepo: [ID!]!){
orgNode:nodes(ids: $idOrg){
...on Organization{
name
}
}
repNode:nodes(ids: $idRepo){
...on Repository{
name
}
}
}
{
"idOrg": ["MDEyOk9yZ2FuaXphdGlvbjY5NjMx","MDEyOk9yZ2FuaXphdGlvbjE3ODIxODA="],
"idRepo":["MDEwOlJlcG9zaXRvcnk4Njg2MDg0Nw==","MDEwOlJlcG9zaXRvcnk5NzkxMTYy"]
}
Second answer is using a more readable approach though cumbersome.
query{
FacebookOrg: organization(login: "facebook") {
...OrgInfo
}
JavaScriptOrg: organization(login: "javaScript") {
...OrgInfo
}
FacebookRep: repository(owner: "facebook" name:"360-Capture-SDK"){
...RepInfo
}
JavaScriptRep: repository(owner: "javaScript" name:"sorted-array"){
...RepInfo
}
}
fragment OrgInfo on Organization {
name
}
fragment RepInfo on Repository {
name
}
Given a user's id, I want to get all pull requests where they are a requested reviewer.
The following won't work as it only allows me to get pull requests opened by that user:
query {
node(id: "$user") {
... on User {
pullRequests(first: 100) {
nodes {
reviewRequests(first: 100) {
nodes {
requestedReviewer {
... on User {
id
}
}
}
}
}
}
}
}
}
Is there a way to do this?
Thanks!
You can get username from the user id and perform a search with review-requested to match a user requested for review :
get username :
{
node(id: "MDQ6VXNlcjk2OTQ3") {
... on User {
login
}
}
}
get open PR where user is review requested :
{
search(query: "type:pr state:open review-requested:refack", type: ISSUE, first: 100) {
issueCount
pageInfo {
endCursor
startCursor
}
edges {
node {
... on PullRequest {
repository {
nameWithOwner
}
number
url
}
}
}
}
}
You can define the query string as a variable, try it in the explorer
I want to use the GraphQL Github API to recursively list all files contained in the directory. Right now my query looks like this:
{
search(first:1, type: REPOSITORY, query: "language:C") {
edges {
node {
... on Repository {
name
descriptionHTML
stargazers {
totalCount
}
forks {
totalCount
}
object(expression: "master:") {
... on Tree {
entries {
name
type
}
}
}
}
}
}
}
}
However, this only gives me only the first level of directory contents, in particular some of the resulting objects are again trees. Is there a way to adjust the query, such that it recursively list the contents of tree again?
There is no way to recursively iterate in GraphQL. However, you can do so programmatically using a query variable:
query TestQuery($branch: GitObjectID) {
search(first: 1, type: REPOSITORY, query: "language:C") {
edges {
node {
... on Repository {
object(expression: "master:", oid: $branch) {
... on Tree {
entries {
oid
name
type
}
}
}
}
}
}
}
}
Start with a value of null and go from there.
working example
More info: https://docs.sourcegraph.com/api/graphql/examples
But probably this will change in the near feature. For example latest github version is v4 https://developer.github.com/v4/explorer/
{
search(first: 1, type: REPOSITORY, query: "language:C") {
edges {
node {
... on Repository {
name
descriptionHTML
stargazers {
totalCount
}
forks {
totalCount
}
object(expression: "master:") {
... on Tree {
entries {
name
object {
... on Tree {
entries {
name
object {
... on Tree {
entries {
name
}
}
}
}
}
}
}
}
}
}
}
}
}
}
There is a parse CloudCode function created as such:
Parse.Cloud.define("getCurrentEvents", function(request, response) {
var TimedEvent = Parse.Object.extend("TimedEvent");
var query = new Parse.Query(TimedEvent);
query.greaterThan("expiresOn", new Date());
query.find({
success: function(results) {
response.success(results);
},
error: function(error) {
response.error("There was an error while looking for TimedEvents");
}
});
});
It returns an array of TimedEvent, as shown in the curl test here:
{"result":[{"expiresOn":{"__type":"Date","iso":"2014-07-31T22:31:00.000Z"},"playMode":"Normal","tableId":"Carnival","objectId":"J1LSO3EnKi","createdAt":"2014-07-28T21:48:22.983Z","updatedAt":"2014-07-28T22:32:14.304Z","__type":"Object","className":"TimedEvent"}]}
When trying to access it from Unity SDK however, I get a "cannot convert to destination type" exception with the following line:
System.Threading.Tasks.Task<Parse.ParseObject[]> task =
Parse.ParseCloud.CallFunctionAsync<Parse.ParseObject[]> ("getCurrentEvents", parameters);
I also tried
System.Threading.Tasks.Task<IEnumerable<Parse.ParseObject>> task =
Parse.ParseCloud.CallFunctionAsync<IEnumerable<Parse.ParseObject[]>> ("getCurrentEvents", parameters);
with the same (lack of) results. What kind of signature is the SDK expecting?
Have you tried something like this (without IEnumerable?):
Threading.Tasks.Task<Parse.ParseObject> task = Parse.ParseCloud.CallFunctionAsync<Parse.ParseObject>("getCurrentEvents", parameters);
But better yet, you could extend ParseObject to create your own TimedEvent class in Unity, like this:
[ParseClassName("TimeEvent")]
public class TimeEvent : ParseObject
{
[ParseFieldName("expiresOn")]
public DateTime expiresOn
{
get { return GetProperty<DateTime>("expiresOn"); }
set { SetProperty(value, "expiresOn"); }
}
[ParseFieldName("playMode")]
public string playMode
{
get { return GetProperty<string>("playMode"); }
set { SetProperty(value, "playMode"); }
}
[ParseFieldName("tableId")]
public string tableId
{
get { return GetProperty<string>("tableId"); }
set { SetProperty(value, "tableId"); }
}
// any other fields you want to access
}
Then you can query your data like this:
IEnumerator getTimedEvents(Dictionary<string, object> parameters)
{
var cloudTask = Parse.ParseCloud.CallFunctionAsync<TimeEvent>("getCurrentEvents", parameters);
while (!cloudTask.IsCompleted)
yield return null;
if (cloudTask.IsCanceled || cloudTask.IsFaulted)
{
// handle error
}
else
{
TimeEvent t = cloudTask.Result;
// do stuff with t
}
}
P.S. Don't forget to register your Parse class somewhere (I usually do it in the Awake() of an early GameObject). In your case, you would do it like this:
Parse.ParseObject.RegisterSubclass<TimedEvent>();