GraphQL - Find order by "name"

Billy_Griffiths
Shopify Partner
17 0 6

I'm currenlty trying to get to grips with GraphQL and wanting to query "Orders" to find an order by "name" (ie: #1003). This filter parameter does not seem to be supported by GraphQL. Can additional filter parameters be requested, or is this a fixed list? 

If I am not able to do this using GraphQL, is there a reliable way using the REST API? 

 

 

Web development using (primarily) CakePHP. Trying my hand at Shopify Apps.
Replies 22 (22)
KarlOffenberger
Shopify Partner
1873 184 890

Hi Billy G

Is there a reliable way using the REST API?

Neither unreliable or reliable - there's no way in REST Admin API either - in fact, it haseven less possibilities to filter IIRC.

I guess your only solution is to request @Shopify that the name field be added to the list of possible query filter parameters.

Liked this post? You might also like our fantastic upsell apps Candy Rack, Last Upsell post-purchase and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
Billy_Griffiths
Shopify Partner
17 0 6

Hi Carl, 

It is not documented, but I've found the following REST API request to return a `name` filtered result, for example: 

https://<storename>.myshopify.com/admin/orders.json?name=%231002

This request returns a 267-line JSON response, which I find slightly inefficient seeing as, when using GraphQL, I could select the exact nodes I need. I have also not played around with it enough to establish the reliability of this method. 

How do I go about requesting this filter be added to GraphQL by @Shopify?

 

Web development using (primarily) CakePHP. Trying my hand at Shopify Apps.
KarlOffenberger
Shopify Partner
1873 184 890

Ooo... good to know. I trued that in the past and it didn't work. I tried again now that you mentioned it and I was thinking

nah man, how on earth did he do that?

and it didn't work!

So unlike regular GET order by ID endpoint i.e. /admin/orders/{order_id}.json when you use the name filter (possibly others too) on a fulfilled order (thus, archived) it won't work.

Unfulfilled orders work like a charm using name filtering.

Cool,nice catch Billy G and thanks!

p.s. As for how to request @Shopify for a feature - no idea, try contacting support via chat to check, or ask here and pray nobody offers you an app that does "feature requests for Shopify partners" @ 20 bucks a month 😉

Liked this post? You might also like our fantastic upsell apps Candy Rack, Last Upsell post-purchase and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
Billy_Griffiths
Shopify Partner
17 0 6

So unlike regular GET order by ID endpoint i.e. /admin/orders/{order_id}.json when you use the name filter (possibly others too) on a fulfilled order (thus, archived) it won't work.

Unfulfilled orders work like a charm using name filtering.

If you are dealing with a fulfilled order, add `&status=any`. Voila! 🙂 How reliable this undocumented method is, is questionable. 

/admin/orders.json?name=%231002&status=any

As for how to request @Shopify for a feature - no idea, try contacting support via chat to check, or ask here and pray nobody offers you an app that does "feature requests for Shopify partners" @ 20 bucks a month 😉

LOL! Will do just that, thanks! 

Web development using (primarily) CakePHP. Trying my hand at Shopify Apps.
Billy_Griffiths
Shopify Partner
17 0 6

So there is a problem with this method...

/admin/orders.json?name=%231002&status=any

Under certain conditions (order older than 60-days?) it seems to be doing a "like" instead of an exact match, so searches for '1002' will return results for '10021', '10022', '10023' etc. etc.

This is not ideal. I still think being able to filter by 'name' via GraphQL (properly) would be great. 

Web development using (primarily) CakePHP. Trying my hand at Shopify Apps.
Yu727
Excursionist
38 0 10

Hi @Billy_Griffiths 

To get all orders(older than 60 days), you have to require access to read all orders for your app.

HunkyBill
Shopify Expert
4838 60 590

Is this still a thing? I am given the order name, ie) #65478 and I need that order. In the Shopify admin it comes up no trouble, but with RestAPI one could never search using the name attribute, and it seems GraphQL also is kneecapped this way. Why? Do I have to resort to calling the search endpoint for the shop to deal with this? That seems silly.

 

What is the scoop on this? Why would we not be able to search given the name? Interestingly if I search for a reference or source_identifier value, I get my record, simply by setting the query. ie)

 

{
query: "113-7672643-1234-5567"
}

Returns me order #65478 when the query matches the reference or source_identifier, but in the case where all I have is the actual Shopify order name, #65478 I do not get anything useful back. So this is a huge cosmic black hole of nothing good unless I am incorrect in all my tests. What does one do?

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
fabz
Shopify Partner
2 0 1

This leaves me scratching my head in disbelieve. The order name is usually the first thing people search for, why is this still not supported?

ritsybitsy
Tourist
6 0 11

I too am flabbergasted by the sheer stupidity of

a) not having it by order_name

b) not clearly differentiating between order_name and order_id - most people assume the order_id is the order_name

 

SO stupid.

HunkyBill
Shopify Expert
4838 60 590

I would not call it sheer stupidity but it is an inconvenience for now. It seems Shopify is moving rapidly to a superior underlying query model this year, where the current Documentation on things is already pointed. Perhaps once that is serviceable, we'll have a little more leeway there. One can only hope.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
Adaptiv
Shopify Partner
2 0 0

I was stuck with the same problem that you can not search for orders by name. I think there is no excuse that this functionality is not available to developers. Every search performed over the GUI should be available through REST/GraphQL. I am glad the https://[SHOPNAME].myshopify.com/admin/orders.json?name=[ORDERNUMBER]&status=any search still works. Can someone tell me if this is still a supported endpoint and where it is documented?

HunkyBill
Shopify Expert
4838 60 590

The better solution is to guide your queries the the general search endpoint. Although it is not part of the standard query API, it works fine for oddball searches like this and is the same one used internally by Shopify, so you get to dogfood what they use.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
policenauts1
Trailblazer
174 13 38

Hi @HunkyBill , what is the general search endpoint? Do you have a link? Thank you.

Prabhat
Shopify Expert
82 8 24

Faced The same issue after 2years

But here is the solution I came with 

    let query = `
      {
        orders(first: 1, query:"name:#49210") {
          edges {
            node {
              id
              name
              displayFulfillmentStatus
              cancelledAt
              createdAt
              shippingAddress {
                formatted(withName : true) 
              }
              lineItems(first:50) {
                edges {
                  cursor
                  node {
                    name
                    image {
                        transformedSrc(maxWidth: 60, scale: 1)
                    }
                    refundableQuantity
                    discountedTotalSet {
                        shopMoney {
                            amount
                            currencyCode
                        }
                        presentmentMoney {
                            amount
                            currencyCode
                        }
                    }
                  }
                }
              }
            }
          }
        }
      }`;

    shopify
    .graphql(query)
    .then(result => {
        res.send(result);
    })

 

There should not be any space between the name and the order name only for 60days  I guess i didn't tried the other API but its good for my use case. 

Solution Expert
- Did I solved your issue? Like & Mark As Solution to help the community
Joyen
New Member
3 0 0

Hi,

 

this is driving me crazy....It seems nothing is working.

 

I tried : https://cookinshape.myshopify.com/admin/api/2020-07/orders.json?name=%231052&status=any  for example and many other combination but nothing is working....

If someone found out how to do please help me. This problem is breaking my app.

 

(it seems before it was working but for some reason it's not working anymore....)

 

Thank you

 

Gregarican
Shopify Partner
1030 86 276

If you read some of the posts on the thread above, you'll see the general direction that Shopify is going in term of API access is GraphQL. If you utilize that endpoint and create your order search using its query parameters you will definitely have better results. Once you are able to dig into the documentation and try some samples with the GraphiQL app against a test/development shop you will surely get the hang of it!

Joyen
New Member
3 0 0

Hi,

Thanks a lot for your reply. But I read all the post actually and still can't find an answer to my problem because all of the tricks an those posts are not working for me.

That's why I asked for help. I am only familiar with REST API, I don't know anything about graphql. And all my other endpoint use REST API successfully so I would like to be consistent and to use REST API everywhere if possible.

(I don't understand why when they update the API they don't make it at least retro-compatible, that would be much easier to maintain our app)

So do you have a solution with the REST API ?

Or of not, then with graphql but I use php...Not sure if it's really convenient to use graphql with pho as I never used it :s

Thanks a lot if you can help me a bit more about this issue. (I'm not an experienced developper)

Gregarican
Shopify Partner
1030 86 276

Here is an introduction to the Shopify GraphQL API --> https://shopify.dev/docs/admin-api/graphql/reference. As for using PHP, the logic is similar to what you're employing now. You just always make a POST request to the same endpoint URL, with the request body differing depending on what you want to do.

Below is a brief example where I am just pulling the shop name using GQL. You obviously need to replace the $domain with what you need to hit, as well as the token with your API's access token. Otherwise it's good to try!

public function graph($query , $variables = []){
    $domain = 'xxx.myshopify.com';
    $url = 'https://'.$domain.'/admin/api/2019-10/graphql.json';

    $request = ['query' => $query];

    if(count($variables) > 0) { $request['variables'] = $variables; }

    $req = json_encode($request);
    $parameters['body'] = $req;

    $stack = HandlerStack::create();
    $client = new \GuzzleHttp\Client([
        'handler'  => $stack,
        'headers'  => [
            'Accept'       => 'application/json',
            'Content-Type' => 'application/json',
            'X-Shopify-Access-Token'=>$this->token // shopify app accessToken
        ],
    ]);

    $response = $client->request('post',$url,$parameters);
    return $body =  json_decode($response->getBody(),true);
 }

  $query = "{ shop { name email } }"; // this is example graphQL query

  $response = graph($query) // call this function 

 

timd_mackey
Shopify Partner
55 1 31

The above discussion is somewhat confusing, and there hasn't been a clear answer to the original request yet.

ANSWER: It is now possible to make a GraphQL request which looks up orders matching the "order name". You simply submit the request with the query parameter "name:" along with the order number (including the pound sign), e.g. "name:#2468". Here is an example query:

{
  orders(first: 1, query:"name:#2486") {
    edges {
      node {
        id
        name
        createdAt
      }
    }
  }
}

 

HunkyBill
Shopify Expert
4838 60 590

I think the RestAPI caught up to this as well. Meaning the underlying search has finally been re-factored to use more flexible parameters, meaning we can move on just get work done without obnoxious workarounds!

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
cmackie_
Shopify Partner
1 0 0

Hey I got it to work with a variable input as well if anyone needs it:

 

query getOrder($query: String){
orders(first: 1, query: $query ) {
edges {
node {
id
name
customer {
displayName
}
shippingAddress {
zip
}
lineItems (first: 10) {
edges {
node {
name
}
}
}
}
}
}
}

HunkyBill
Shopify Expert
4838 60 590

To improve your Post here, you could include the query you used so show the community what you did. Additionally, if you have an order with 11 or more line items, very common in Shopify, your GQL will fail to show you those, since you pinned your query to 10 items or less.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com