Code Monkey home page Code Monkey logo

Comments (3)

leszekhanusz avatar leszekhanusz commented on June 12, 2024

Sending a json with a query key is perfectly normal

  • Which backend are you using?
  • Are you sure you used a GraphQL endpoint url?

from gql.

mattroberts96 avatar mattroberts96 commented on June 12, 2024

I'm using python as my backend and my url is using a graphql endpoint. it isi using shopify graphql endpoint.

from gql.

leszekhanusz avatar leszekhanusz commented on June 12, 2024

Alright,

It seems the problem is that Shopify does not provide a standard GraphQL HTTP interface.

The documentation for the GraphQL API is here

And a GraphiQl interface of a demo store is available at https://shopify.dev/graphiql/admin-graphiql

Reverse engineering (with chrome dev-tools, copy as curl) what is sent on this graphiql interface shows a request looking like this:

curl 'https://shopify.dev/admin-graphql-proxy' \
  -H 'authority: shopify.dev' \
  -H 'accept: */*' \
  -H 'accept-language: en-US,en;q=0.9,la;q=0.8,fr;q=0.7,nl;q=0.6' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'cookie: shopify_experiment_assignments=%5B%5D; _shopify_dev_session=REDACTED' \
  -H 'origin: https://shopify.dev' \
  -H 'pragma: no-cache' \
  -H 'referer: https://shopify.dev/graphiql/admin-graphiql' \
  -H 'sec-ch-ua: "Not(A:Brand";v="24", "Chromium";v="122"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "Linux"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: same-origin' \
  -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36' \
  -H 'x-csrf-token: REDACTED' \
  --data-raw '{"graphQLParams":{"query":"{\n  shop {\n    name\n  }\n}","variables":null},"version":"2024-01"}'

What is strange in that request is that you can see a graphQLParams key in the request instead of directly the query key. This does not seem to be documented in the Shopify GraphQL documentation.

I was able to execute a query using gql using a modified transport for Shopify which will modify the request to put it inside the GraphQLParams key.
The following code should be executable:

import logging
logging.basicConfig(level=logging.INFO)

from gql import gql, Client
from gql.transport.httpx import HTTPXTransport

class ShopifyTransport(HTTPXTransport):

    def _prepare_request(
        self,
        document,
        variable_values = None,
        operation_name = None,
        extra_args = None,
        upload_files = False,
    ):

        payload = super()._prepare_request(
            document=document,
            variable_values=variable_values,
            operation_name=operation_name,
            extra_args=extra_args,
            upload_files=upload_files,
        )

        shopify_payload = {
            "graphQLParams": payload["json"],
            "version": "2024-01",
        }

        return {"json": shopify_payload}

transport = ShopifyTransport(url="https://shopify.dev/admin-graphql-proxy")
client = Client(transport=transport)

query = gql("""
{
  shop {
    name
  }
}
""")

result = client.execute(query)

print (f"result = {result}")

You could try using that ShopifyTransport above on your own shop to see if it solves your problem.

from gql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.