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/
Related
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.
I have two queries, one for fetching automatic discounts and other for fetching code discounts of a shopify store.
So is there a way that I can fetch both the discounts on a single query. Also we are letting users search for their discounts by title, so is there any query that returns data based on discount title because I have seen that in products or collections inside the query we can use:
products(first: 10, query: "title:*searched_text*")
These are my queries:
# for code discounts
query_1 = '''
{
codeDiscountNodes (first:10,query:"status:active"){
edges {
node {
id
codeDiscount {
__typename
... on DiscountCodeBxgy {
status
title
}
... on DiscountCodeBasic {
status
title
}
}
}
}
}
}
'''
# for automatic discounts
query_2 = '''
{
automaticDiscountNodes (first: 10) {
edges {
node {
id
automaticDiscount {
__typename
... on DiscountAutomaticBxgy {
status
title
}
... on DiscountAutomaticBasic {
status
title
}
}
}
}
}
}
'''
Welcome to Stack Overflow!
...is there a way that I can fetch both the discounts on a single query?
Yes, and there are two ways to do this:
First, in GraphQL you can bundle queries together into a single string like so:
{
codeDiscountNodes(first:10) {
edges {
node { id }
}
}
automaticDiscountNodes(first:10) {
edges {
node { id }
}
}
}
Results in:
{
"data": {
"codeDiscountNodes": {
"edges": [
{
"node": { "id": "gid://shopify/DiscountCodeNode/10..." }
}
]
},
"automaticDiscountNodes": {
"edges": [
{
"node": { "id": "gid://shopify/DiscountAutomaticNode/10..." }
}
]
}
}
}
Alternatively, the shopify API provides a general discountNodes endpoint which you can use to request all your discounts together.
{
discountNodes(first:10) {
edges {
node {
id
}
}
}
}
This approach is more efficient than combining the two queries (check out the extensions.cost.actualQueryCost for each of these solutions).
...is there any query that returns data based on discount title?
No, it does not appear like this API allows you to search by title. You can find the list of query filter parameters in the documentation..
I have the following GraphQL query; "categories" is a one-to-many relationship, each containing a name and a slug. I want to get all Articles with category slug of "derp". Any thoughts?
{
allContentfulArticle(
sort: { order: DESC, fields: [createdAt] }
) {
edges {
node {
title
slug
datePublished
createdAt
categories {
name
slug
}
}
}
}
}
Contentful DevRel here. 👋
I just prototyped your scenario and this query should do it.
query {
categoryCollection(where: { slug_contains: "schnitzel" }) {
items {
linkedFrom {
entryCollection {
items {
...on Article {
title
}
}
}
}
}
}
}
You can find more info in the docs about links to a specific entry.
Hope that helps. :)
I'm having trouble understanding how to write filters for GraphQL queries in GatsbyJS.
This works:
filter: { contentType: { in: ["post", "page"] }
I basically need the reverse of that, like:
filter: { "post" in: { contentTypes } } // where contentTypes is array
That doesn't work because "NAME is expected" (where "post" is in my example).
After going through GatsbyJS docs I found this:
elemMatch: short for element match, this indicates that the field you are filtering will return an array of elements, on which you can apply a filter using the previous operators
filter:{
packageJson:{
dependencies:{
elemMatch:{
name:{
eq:"chokidar"
}
}
}
}
}
Great! That's what I need! So I try that, and I get:
error GraphQL Error Field "elemMatch" is not defined by type markdownRemarkConnectionFrontmatterTagsQueryList_2.
Keywords defined in markdownRemarkConnectionFrontmatterTagsQueryList_2 are:
eq: string | null;
ne: string | null;
regex: string | null;
glob: string | null;
in: Array | null;
Why am I limited to these keywords when more keywords such as elemMatch are mentioned in docs? Why am I not allowed to use the filter structure "element in: { array }"?
How can I create this filter?
Filter by value in an array
Let's say you have a markdown blog with categories as an array of string, you can filter posts with "historical" in categories like this:
{
allMarkdownRemark(filter:{
frontmatter:{
categories: {
in: ["historical"]
}
}
}) {
edges {
node {
id
frontmatter {
categories
}
}
}
}
}
You can try this query out in any of the graphiq blocks in Gatsby.js docs.
ElemMatch
I think elemMatch is only 'turned on' for fields with array of objects; something like comments: [{ id: "1", content: "" }, { id: "2", content: ""}]. This way, you can apply further filters on the fields of each comment:
comments: { elemMatch: { id: { eq: "1" } } }
Here's an example you can try out in the graphiq blocks in gatsby docs:
// only show plugins which have "#babel/runtime" as a dependency
{
allSitePlugin (filter:{
packageJson:{
dependencies: {
elemMatch: {
name: {
eq: "#babel/runtime"
}
}
}
}
}) {
edges {
node {
name
version
packageJson {
dependencies {
name
}
}
}
}
}
}
Is it possible to get an article(single entity) using the Url Alias (entityUrl.path)?
I am using https://github.com/drupal-graphql/graphql
I can do a bulk query for all the articles, do I then filter those results?
Thanks
query GetArticles {
nodeQuery(filter: {conditions: [{field: "type", value: "article"}]}) {
Articles: entities {
entityId
entityLabel
entityUrl {
path
routed
}
}
}
}
Figured it out
query ($path: String!) {
route:route(path: $path) {
... on EntityCanonicalUrl {
entity {
... on Node {
nid
entityLabel
body{
value
}
}
}
}
}
}