GitHub GraphQL API Read custom field in Project V2 - graphql

We are using Github Projects V2. I have created a custom field say 'MyCustomField'.
I want to read the value of custom field MyCustomField using Github GraphQL API.
I am following Gtihub GraphQL API Docs
So far I have got till reading a few predefined fields on Gtihub Issues like title, url, assignees and labels. I am using Windows PowerShell:
$project_id="MyProjectIDFetchedUsingDifferentQuery"
gh api graphql -f query='
query($project_id: ID!){
node(id: $project_id) {
... on ProjectV2 {
items(last: 20) {
nodes{
id
content{
...on Issue {
title
url
assignees(first: 10) {
nodes{
login
}
}
labels(first:5) {
edges {
node {
name
}
}
}
}
}
}
}
}
}
}' -f project_id=$project_id
I am not able to find a way to get the custom field MyCustomField.
I am expecting to write some query like below:
$project_id="MyProjectIDFetchedUsingDifferentQuery"
gh api graphql -f query='
query($project_id: ID!){
node(id: $project_id) {
... on ProjectV2 {
items(last: 20) {
nodes{
id
content{
...on Issue {
title
url
assignees(first: 10) {
nodes{
login
}
}
labels(first:5) {
edges {
node {
name
}
}
}
customFields(first:5) {
nodes {
name
}
}
}
}
}
}
}
}
}' -f project_id=$project_id

I came up with this which I think should get the custom fields on your board:
query {
node(id: "(my project id)") {
... on ProjectV2 {
items(last: 20) {
nodes {
id
content {
... on Issue {
title
url
state
assignees(first: 10) {
nodes {
login
}
}
}
}
fieldValues(first: 20) {
nodes {
... on ProjectV2ItemFieldSingleSelectValue {
field {
... on ProjectV2SingleSelectField {
name
}
}
name
id
}
... on ProjectV2ItemFieldLabelValue {
labels(first: 20) {
nodes {
id
name
}
}
}
... on ProjectV2ItemFieldTextValue {
text
id
updatedAt
creator {
url
}
}
... on ProjectV2ItemFieldMilestoneValue {
milestone {
id
}
}
... on ProjectV2ItemFieldRepositoryValue {
repository {
id
url
}
}
}
}
}
}
}
}
}
Credit should go to https://stackoverflow.com/users/2312060/rich-kuzsma for posting https://gist.github.com/richkuz/e8842fce354edbd4e12dcbfa9ca40ff6
This had the basic format for querying ProjectV2 fields, and I added the
ProjectV2SingleSelectField bit to include both the field name and its value in the response.

Related

Github GraphQL API Get all commits of a User

I'm trying to use the GitHub GraphQL API to get all the additions made by a user (additions can be found from their commits). I've been able to get the additions from pull requests, but I haven't found a way to do the same for commits. How can I get all the commits from a user?
This is my query (I am new to GraphQL):
query AllAdditions($username: String!, $from: DateTime, $to: DateTime) {
user(login: $username) {
name
contributionsCollection(from: $from, to: $to) {
commitContributionsByRepository(maxRepositories: 100) {
repository {
nameWithOwner
}
contributions(first: 30) {
totalCount
# I'm trying to get additions like this, but there is no 'commit' field
# nodes {
# commit {
# additions
# }
# }
}
}
pullRequestContributionsByRepository(maxRepositories: 100) {
repository {
nameWithOwner
}
contributions(first: 30) {
nodes {
pullRequest {
additions
}
}
}
}
}
}
}
{
search(query: "org:test", type: REPOSITORY, last: 100) {
nodes {
... on Repository {
name
defaultBranchRef {
name
target {
... on Commit {
history(first: 100, since: "2013-07-11T00:00:00") {
totalCount
nodes {
... on Commit {
committedDate
additions
author {
name
email
}
}
}
}
}
}
}
}
}
}
}
As for getting all orgs, there isn’t currently a way to do that via the GraphQL API because the search connection requires something in the search query. There’s also no top-level connection that allows you to list all users, orgs, or repositories.
I hope that helps!
answer copied from this URL: GraphQL - Get all commits by all users

Nextjs - Contentful - Tags - Dynamic pages

I would like to create dynamic pages when I click a tag in an article or elsewhere on my website.
I'm using Next.js, SSG, and fetching the articles containing the tags from Contentful with the following GraphQL queries:
export async function getArticles() {
const articlesQuery = gql`
{
articleCollection(order: date_DESC) {
items {
title
slug
excerpt
date
contentfulMetadata {
tags {
name
id
}
}
featuredImage {
title
url
width
height
}
author {
name
photo {
fileName
url
width
height
}
title
twitterProfile
linkedInProfile
slug
}
}
}
}
`;
return graphQLClient.request(articlesQuery);
}
export async function getArticle(slug) {
const articleQuery = gql`
query getArticle($slug: String!) {
articleCollection(limit: 1, where: { slug: $slug }) {
items {
title
slug
excerpt
date
contentfulMetadata {
tags {
name
id
}
}
featuredImage {
title
url
width
height
}
author {
name
photo {
fileName
url
width
height
}
title
twitterProfile
linkedInProfile
slug
}
content {
json
links {
entries {
block {
sys {
id
}
__typename
... on VideoEmbed {
title
embedUrl
}
... on CodeBlock {
description
language
code
}
}
}
assets {
block {
sys {
id
}
url
title
width
height
}
}
}
}
}
}
}
`;
return graphQLClient.request(articleQuery, {
slug,
});
}
The contentfulMetadata is where the tags come from:
contentfulMetadata {
tags {
name
id
}
}
This is my [id].jsx file:
import { getArticles, getArticle } from "#utils/contentful";
export async function getStaticPaths() {
const data = await getArticles();
return {
paths: data.articleCollection.items.map((article) => ({
params: { id: article.contentfulMetadata.tags[0].id },
})),
fallback: false,
};
}
export async function getStaticProps(context) {
const data = await getArticle(context.params.id);
return {
props: { article: data.articleCollection.items[0] },
};
}
export default function TagPage({ article }) {
return (
<div>
<h1>{article.contentfulMetadata.tags.id}</h1>
</div>
);
}
I get the following error:
Error: Error serializing `.article` returned from `getStaticProps` in "/tags/[id]". Reason: `undefined` cannot be serialized as JSON. Please use `null` or omit this value.
When console.log(data.articleCollection.items.contentfulMetadata.tags.id); or console.log(data.articleCollection.items.contentfulMetadata.tags[0].id); within getStaticPaths function it provides the following error:
TypeError: Cannot read property 'tags' of undefined
Can anyone show how to create a dynamic page ([id].jsx) file, which shows the tag id as the header <h1> as well as all articles containing the same tag?
Contentful DevRel here 👋🏼.
article.contentfulMetadata.tags is an array, as an entry can have more than one tag. So you'll need to access the tag you want via article.contentfulMetadata.tags[0].id or article.contentfulMetadata.tags[desired_index].id and so on.
Here's an example GraphQL query:
query {
blogPostCollection {
items {
contentfulMetadata {
tags {
id
name
}
}
}
}
}
And here's the response with tags as an array:
"data": {
"blogPostCollection": {
"items": [
{
"contentfulMetadata": {
"tags": [
{
"id": "salmastag",
"name": "Salma s tag"
}
]
}
},
{
"contentfulMetadata": {
"tags": []
}
}
]
}
}
}
Notice how if a blog post doesn't have any PUBLIC tags assigned (the second entry in the response), an empty array is returned — you might want to do some safety checking in your code for this.

Attempting to query with graphql where id is

I need to get a query using graphql in strapi/gatsby where id is {id}.
According to the documentation found here you query all like so:
{
allStrapiArticle {
edges {
node {
id
title
content
}
}
}
}
This works and I'm able to query however I'd like to get only one Article where id is {id};
I have tried:
{
allStrapiArticle(id: "4") {
edges {
node {
id
title
content
}
}
}
}
And also:
{
allStrapiArticle {
edges {
node(id: "4") {
id
title
content
}
}
}
}
Both of the above give me an error. Any idea how I can achieve this?
Use:
{
allStrapiArticle(filter: {id: {eq: "4" }}) {
edges {
node {
id
title
content
}
}
}
}
elemMatch filter might be useful for your use case as well.
Check the localhost:8000/___graphql playground to test your queries and filters.
More references:
https://www.gatsbyjs.com/docs/query-filters/
https://www.gatsbyjs.com/docs/graphql-reference/

GraphQL error: Variable $customerAccessToken of type String! was provided invalid value

I'm attempting to recover a customer using the customerAccessToken that is given when the user logs in to Shopify.
Using Apollo, here's my code:
this.apollo.mutate({
mutation: getCustomerFromToken,
variables: {
input: {
customerAccessToken: '217b9a6952c28eb4db376487a6301294' // Also tried btoa('217b9a6952c28eb4db376487a6301294')
}
},
})
Here's my GraphQL query:
query getCustomerFromToken($customerAccessToken: String!) {
customer(customerAccessToken: $customerAccessToken) {
id
addresses(first: 5) {
edges {
node {
address1
address2
company
city
province
zip
country
phone
}
}
}
orders(first: 200) {
edges {
cursor
node {
id
totalPriceV2 {
amount
currencyCode
}
processedAt
orderNumber
}
}
}
}
}
Here's the login GraphQL code I'm using to retrieve the accessToken from Shopify:
mutation customerAccessTokenCreate($input: CustomerAccessTokenCreateInput!) {
customerAccessTokenCreate(input: $input) {
customerAccessToken {
accessToken
expiresAt
}
customerUserErrors {
code
field
message
}
}
}
My problem was two fold.
I was using a mutation on a query end point
Queries don't use input in the payload
const payload = {
customerAccessToken: "..."
}
// NOT
const payload = {
input: {
customerAccessToken: "..."
}
}

Github GraphQL Repository Query, using two objects

How to use more than one objects when querying Github GraphQL?
The following will break if the 2nd object is uncommented:
query {
repository(owner:"rails", name:"rails") {
object(expression:"master") {
... on Commit {
history {
totalCount
}
}
}
# object(expression: "master:README.md") {... on Blob {byteSize}}
}
}
How to make it working? Thx
Use alias:
query {
repository(owner:"rails", name:"rails") {
object(expression:"master") {
... on Commit {
history {
totalCount
}
}
},
second_object: object(expression: "master:README.md") {... on Blob {byteSize}}
}
}

Resources