Can I use grphql query field inside the same query? - graphql

I have query GetPosts:
query GetPosts {
posts {
id
description
owner {
id
}
files {
id
}
isPublished
}
}
I want to not fetch files and description fields if isPublished is false. I know I can use the #skip directive for it but not sure how to pass the field to it instead of the variable.
query GetPosts {
posts {
id
isPublished
description #skip(if: isPublished)
owner {
id
}
files #skip(if: isPublished) {
id
}
}
}

Related

GraphQL query filtering multiple relations

(From Strapi) I am trying to get all "acts" with a certain age (can return multiple) and with a certain place (can return multiple). I can't figure out how to filter that.
This is what I am trying in GraphQL-playground (works without the variables), but it says "Unknown argument "age" on field "Act.ages"." (and "place" respectively).
query GetActs ($age:Int, $place:String) {
acts {
data {
id
attributes {
Title
ages (age: $age) {
data {
id
attributes {
age
}
}
}
places (place: $place) {
data {
id
attributes {
place
}
}
}
}
}
}
}
I just ran into this same issue. I can't make out the error you're reporting, but here is what worked for me.
You can use filter at the collection level to drill down to nested fields for the corresponding attributes. This follows the GraphQL example at the bottom of this Strapi resource on filtering nested fields.
Solution
query GetActs ($age:Int, $place:String) {
acts (filters: {ages: {age: {eq: $age}}, places: {place: {eq: $place}}}) {
data {
id
attributes {
Title
ages {
data {
id
attributes {
age
}
}
}
places {
data {
id
attributes {
place
}
}
}
}
}
}
}

GraphQL: Variable is used by anonymous query but not declared

I am new to GraphQL. I have a query, but it shows error message of "Variable is used by anonymous query but not declared".
{
"query":"{customers(first: 1, query: $input) {edges{node {addresses{ id }}}}}",
"variables":{
"input":{
"id":"gid://shopify/Customer/5044061470926"
}
}
}
Can I get some help what I did wrong?
Thanks!
The error is correct. Your query is
{
customers(first: 1, query: $input) {
edges{
node {
addresses{
id
}
}
}
}
}
and $input is indeed not declared, so GraphQL has no idea what it is supposed to be or how to link it up with your variables values.
You'll need to do
query ($input: <THE_TYPE>!) {
customers(first: 1, query: $input) {
edges{
node {
addresses{
id
}
}
}
}
}
I don't know your API schema, so you'll have to replace <THE_TYPE> with whatever type is defined in your API schema.

Shopify Admin API, graphQL, create draft order - add products

This is my fist time creating an order. From what I understand you need to create a draft order - add products, price, email, notes etc. I am just creating a test query now to see how it works and it tells me "Add at least 1 product". I am trying to add a product, but I dont know how. I have been messing around and reading and cant figure it out.
This is my query:
mutation draftOrderCreate {
draftOrderCreate(input: {email: "123abc#hotmail.com"}) {
draftOrder {
id
order {
id
}
status
}
userErrors {
field
message
}
}
}
If anyone can give me an example on how to add products to this would be great. Thanks.
You can create an draft order like so:
mutation draftOrderCreate($items: DraftOrderInput!) {
draftOrderCreate(input: $items) {
draftOrder {
id
order {
id
}
status
}
userErrors {
field
message
}
}
}
query variables:
{
"items": {
"email": "123abc#hotmail.com",
"lineItems": [
{
"variantId": "gid://shopify/ProductVariant/32231002996788",
"quantity": 1
}
]
}
}
Or if you don't want to use query variables you can pass the whole object as the input:
mutation draftOrderCreate {
draftOrderCreate(input: {email: "123abc#hotmail.com", lineItems: [{variantId: "gid://shopify/ProductVariant/32231002996788", quantity: 1}]}) {
draftOrder {
id
order {
id
}
status
}
userErrors {
field
message
}
}
}

How to get variant by variant Id from product object using by graphql Shopify

Query so far
{
product1: product(id: "gid://shopify/Product/777854222396") {
title
totalVariants
variants(first:99) {
.....
}
hasOutOfStockVariants
}
product2: product(id: "gid://shopify/Product/511571296316") {
title
}
}
What can be done to fetch variant based on id
I have found ProductVariant on their GraphQL Admin API reference docs, you can take reference from their else I didn't find any ProductVariant Schema open at QueryRoot as defined here in the open graphQL admin panel although they have mentioned an example.
query {
productVariant(id: "gid://shopify/ProductVariant/10782354800682") {
inventoryItem {
inventoryLevels (first:10) {
edges {
node {
location {
name
}
available
}
}
}
}
}
}

Use #include directive for more than one field

I have a relay container that conditionally fetches some fields using #include(if: $variable), but I have to write the directive for each individual field:
const relayOptions = {
initialVariables: {
expandDetails: false,
},
fragments: {
company: Relay.QL`
fragment on Company {
id
name
employees #include(if: $expandDetails) {
fullName
}
admins #include(if: $expandDetails) {
fullName
}
departments #include(if: $expandDetails) {
name
}
}
`,
},
}
Is there any way that I could write the directive only once, for example something like this (and I know this code won't work):
const relayOptions = {
initialVariables: {
expandDetails: false,
},
fragments: {
company: Relay.QL`
fragment on Company {
id
name
#include(if: $expandDetails) {
employees {
fullName
}
admins {
fullName
}
departments {
name
}
}
}
`,
},
}
I tried some other crazy ways but none of them worked, I got this error:
'#include' can't be applied to fragment definitions (allowed: fields, fragment spreads, inline fragments)
Edit: Note: I need to do that to avoid fetching data unnecessarily. I'd like to only fetch the data if the collapsible component displaying the data is expanded (planning to do that using setVariables after component is mounted).
In this example there are only 3 conditional fields, but in the actual case I'm trying to solve for there is a lot more than that.
You can put the directive on an inline fragment, and just use the same type as the outer field:
fragment on Company {
... on Company #include(if: $expandDetails) {
employees { fullName }
admins { fullName }
departments { fullName }
}
}

Resources