Shopify Functions Query Input - graphql

Im having trouble with my Shopify Function where I want to give a discount on the products if the client chooses pickup. Is there a way to get if the client picked PICK_UP as the delivery option ? I tried looking into this option HERE, but the response from shopify comes back empty does not matter if I choose shipping option or a pickup option. Here is my input query:
query Input {
cart {
lines {
quantity
merchandise {
__typename
...on ProductVariant {
id
}
}
}
deliveryGroups {
selectedDeliveryOption {
deliveryMethodType
}
}
}
discountNode {
metafield(namespace: "this", key: "that") {
value
}
}
}
Am I doing anything wrong? Any ideas what I could try?

Related

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 use Shopify Graphql ProductVariantsBulkInput

I am trying to bulk update the price on multiple product variants at once. I'm just copying the "interactive" example from Shopify's page
All I did was copy and paste the code and put in my own id's. And of course it does not work.
It looks like this:
mutation productVariantsBulkUpdate($variants: [ProductVariantsBulkInput!]!, $productId: ID!) {
productVariantsBulkUpdate(variants: $variants, productId: $productId) {
product {
cursor
}
productVariants {
cursor
}
userErrors {
code
field
message
}
}
}
With Variables like this:
{
"variants": [
{
id: "gid://shopify/ProductVariant/39369514385591",
price: "50.00"
}
],
"productId": "gid://shopify/Product/6591908577463"
}
I'm getting this error:
Variables are invalid JSON: Unexpected token i in JSON at position 30.
It's OK for me. (with some quick tweaks)
I tweaked the request a little since the cursor is not present in the product/variant object, don't know why Shopify has not updated the example in their docs.
mutation productVariantsBulkUpdate($variants: [ProductVariantsBulkInput!]!, $productId: ID!) {
productVariantsBulkUpdate(variants: $variants, productId: $productId) {
product {
id
}
productVariants {
id
price
}
userErrors {
code
field
message
}
}
}
So try to fix the query and remove the cursor object and check if you are using the proper end-point since the bulk operation is available in the unstable version only if I'm not mistaken.
See the image below showing that the response is OK for me.

How To Query The Price Of A Product Using Shopify Storefront API and GraphQL

I'm totally new to both Shopify Storefront API AND GraphQL. I've been pulling my hair out the last 3-4 hours reading through docs and trying to find and write a query that, in my mind anyways, should be simple.
In a nutsell, I have a storefront that has certain products included on it with custom buy buttons. Each buy button's ID corresponds with the product ID on my actual shopify store, meaning ../products/5854987878567
When the page loads, I need to query my store to pull the updated prices, and then reflect those prices in the button text so the person has an accurate idea of how much they are spending.
To do this, I've pulled all unique button ID's and pushed them to a fresh Array, the idea being I can query all ID's at once using GraphQL and get one nice object back that I can then parse and update the buttons accordingly.
After hours of going in circles, I haven't gotten any closer to succeeding.
I've been reading the Query Root documentation and it seems like they do have the products connection, which I assume serves as the entry point to query all products on the store.
However under the query field itself it seems like they have every field BUT id.
available_for_sale
created_at
product_type
tag
title
updated_at
variants.price
vendor
Are all the fields they have. Since I started writing this I found sortKey, but it failed with the error "Field 'sortKey' doesn't exist on type 'QueryRoot'" sigh.
Here is my query that I'm currently rolling with. I'm not sure this idea will work because if I can only query one id at a time that'll take dozens of API calls per visitor.
$.ajax({
type : 'POST',
url : 'https://xxxxx.myshopify.com/api/2020-10/graphql.json',
headers: {
'X-Shopify-Storefront-Access-Token': 'xxxxx',
'Content-Type': 'application/graphql'
},
data: 'query products { sortKey(id: "5854987878567") }',
success: function(res) {
console.log(res)
},
error: function(status) {
console.log(status)
}
});
Any help would be appreciated!
I don't understand what you mean but the GraphQL below gets "id", "title", "description", "the first image" and "price" for each product(10 products):
{
products(first:10) {
edges {
node {
id
title
description
featuredImage {
url
}
variants(first: 1) {
edges {
node {
priceV2 {
amount
}
}
}
}
}
}
}
}
query = '''
{
productVariants(first:1,query:"title:%s")
{
edges
{
node
{
id
title
}
}
}
}
''' % title
make a function and give 'title' as a argument.
def productvariant(title):
client = shopify.GraphQL()
query = '''
{
productVariants(first:1,query:"title:%s")
{
edges
{
node
{
id
title
}
}
}
}
''' % title
you can get the price from product variant just like this - "{
productVariants(first:10, query:"{apply your condition here}") {
edges
{
node
{
storefrontId
compareAtPrice
price
}
} } } % id"

How can I filter by uid of a linked document / relationship using the prismic graphql api?

I am trying to list a set of articles by their categories, by uid, and I'm assuming that I would have to use the where query, but I'm not able to get that to worked on linked documents.
The issue seems to be that where only accepts a string on a field, but in the case of a linked document you would need to dig down to the uid field.
I'm not sure if I'm using the wrong query, but struggling to find anything in the documentation to help me out.
I tried digging into the category object:
{
allDirectoryServices(
where: { category: { _meta: { uid: "developers" } } }
) {
edges {
node {
name
city
region
country
category {
...on DirectoryTaxonomy {
_meta {
uid
}
name
}
}
}
}
}
}
But that returns an error that it's expecting a string:
"message": "Expected type String, found {_meta: {uid: \"developers\"}}.",
{
allDirectoryServices(
where: { category: "developers"}
) {
edges {
node {
name
city
region
country
category {
...on DirectoryTaxonomy {
_meta {
uid
}
name
}
}
}
}
}
}
This returns no results, obviously.
I asked this question on the Prismic Slack group too, and got the answer from them:
In order to query by a Content Relationship / Link field like this, you need to use the document ID.
where: { category: "WBsLfioAABNUo9Kk" }
Unfortunately it isn’t possible to query by the UID (or any other field).
I imagine they will be updating their documentation soonish, as this isn't covered by it.
Thanks to the Prismic guys!

Shopify GraphQL partial matching on query filter

I'm just getting started with the new Shopify GraphQL Admin API. I'm trying to retreive all products, where the title field contains a certain word.
Currently I can successfully retrieve a product by including the full product title (exact match):
{
shop {
id
name
}
products(first: 10, query:"title:'RAVEN DUSTY OLIVE/SILVER MESH'") {
edges {
node {
productType
title
}
}
}
}
However, I want to partially match the title to display all products with the word "Raven" anywhere in the title, but the following returns no results:
{
shop {
id
name
}
products(first: 10, query:"title:'RAVEN'") {
edges {
node {
productType
title
}
}
}
}
Any ideas on how to get the partial matching working?
Bjorn! This should work:
{
shop {
id
name
}
products(first: 10, query:"title:RAVEN*") {
edges {
node {
productType
title
}
}
}
}
Check out the docs: https://help.shopify.com/en/api/getting-started/search-syntax
Also you can try with
query: "title:*${searchText}*"
you can see two * at the initial and the end

Resources