Code Monkey home page Code Monkey logo

Comments (9)

KingDarBoja avatar KingDarBoja commented on May 20, 2024

Hi @gola077

I think something like this issue should help you.

It is hard to know what's wrong as there is no reproducible example of any source code to look at it.

from gql.

gola077 avatar gola077 commented on May 20, 2024

Hi @KingDarBoja
I'm consuming Shopify GraphQL Admin endpoint; I don't have access to the source code. Query with multiple params works as expected in Insomnia (https://insomnia.rest/). When I run the same query in gql the query fails. However, when running queryx with single param in gql, the results are as expected. That leads me to believe that issue is not with source code (Shopify GraphQL Admin endpoint).

from gql.

gola077 avatar gola077 commented on May 20, 2024

Here is the code for the query with single param:

from gql.transport.requests import RequestsHTTPTransport

token = 'Some token'
sample_transport=RequestsHTTPTransport(
    url='https://storename-dev.myshopify.com/admin/api/2020-04/graphql.json',
    use_json=True,
    headers={
        "Content-type": "application/json",
        "X-Shopify-Access-Token": token
    },
    verify=True
)



client = Client(
    retries=3,
    transport=sample_transport,
    fetch_schema_from_transport=False,
)

queryx = gql("""query getDraftOrderByID($id: ID!) {

        draftOrder (id: $id) {
            totalPrice
            lineItems (first: 5) {
                edges {
                    node {
                        originalUnitPrice
                        quantity
              title
                    }
                }
            }
        }
    }  
"""
)
params = {
    "id": "gid://shopify/DraftOrder/561251483788"
}
client.execute(queryx, variable_values=params)```

from gql.

gola077 avatar gola077 commented on May 20, 2024

query with multiple params

{
        orders(first:$numOrders) {
            pageInfo {
                hasNextPage
            }
            edges {
                node {
                    id
                    lineItems (first:$numLineItems) {
                        edges {
                            node {
                                id
                            }
                        }
                    }
                }
            }
        }
}

"""
)
params = {
    "numOrders": 20,
    "numLineItems": 5
}

client.execute(query6, variable_values=params)```

from gql.

KingDarBoja avatar KingDarBoja commented on May 20, 2024

@gola077 It's not possible to use those queries as this would require some setup using Shopify API to use the token and all that stuff.

I will make an example with nested queries just for sake of simplicity.

from gql.

gola077 avatar gola077 commented on May 20, 2024

Thank you @KingDarBoja! Example would be wonderful.

from gql.

KingDarBoja avatar KingDarBoja commented on May 20, 2024

@gola077 I have made a example with a dummy schema and some resolvers to test this GraphQL functionality, which seems to work with this example.

nested_schema.py
from graphql import GraphQLObjectType, GraphQLSchema, GraphQLField, GraphQLNonNull, GraphQLInt, GraphQLString, \
    GraphQLBoolean, GraphQLList, GraphQLArgument

from .nested_fixtures import getResource, getLink

'''
type Query { links(id: Int, ): [Link]} 
type Link { id: Int, url: String, resources(id: Int): [Resource] }
type Resource {id: Int, type: String, active: Boolean, cacheable: Boolean}

    {
        id: 1, url: "http://bit.com/xDerS",
        resources: [
            {id: 8901, type: "file", active: true, cacheable: true},
            {id: 8902, type: "file", active: false, cacheable: true}
        ]
    },
    {
        id: 2,
        url: "http://bit.com/aDeRe",
        resources: [{id: 8903, type: "file", active: true, cacheable: true}]
    }
'''

resourceType = GraphQLObjectType(
    "Resource",
    description="Set of url resources",
    fields=lambda: {
        "id": GraphQLField(
            GraphQLInt,
            description="The id of the resource"
        ),
        "type": GraphQLField(
            GraphQLString,
            description="The resource type",
        ),
        "active": GraphQLField(
            GraphQLBoolean,
            description="Flag to address if the resource is active"
        ),
        "cacheable": GraphQLField(
            GraphQLBoolean,
            description=""
        )
    },
)

linkType = GraphQLObjectType(
    "Link",
    description="Store url path and resources",
    fields=lambda: {
        "id": GraphQLField(
            GraphQLInt,
            description="The id of the link"
        ),
        "url": GraphQLField(
            GraphQLString,
            description="The url path"
        ),
        "resources": GraphQLField(
            GraphQLList(resourceType),
            args={
                "id": GraphQLArgument(
                    description="",
                    type=GraphQLString
                )
            },
            resolver=lambda root, info, **args: [getResource(args["id"])] if args.get("id") is not None else root["resources"]
        )
    }
)


queryType = GraphQLObjectType(
    "Query",
    fields=lambda: {
        "links": GraphQLField(
            GraphQLList(linkType),
            args={
                "id": GraphQLArgument(
                    description="The id of the link to look for",
                    type=GraphQLNonNull(GraphQLString)
                ),
            },
            resolver=lambda root, info, **args: [getLink(args["id"])],
        ),
        "resource": GraphQLField(
            GraphQLList(resourceType),
            args={
                "id": GraphQLArgument(
                    description="The id of the resource to look for",
                    type=GraphQLString
                )
            },
            resolver=lambda root, info, **args: getResource(args["id"]),
        )
    }
)

NestedSchema = GraphQLSchema(
    query=queryType
)
nested_fixtures.py
resourceOne = {
    "id": 8901,
    "type": "file",
    "active": True,
    "cacheable": True
}

resourceTwo = {
    "id": 8902,
    "type": "file",
    "active": False,
    "cacheable": True
}

resourceThree = {
    "id": 8903,
    "type": "file",
    "active": True,
    "cacheable": True
}

resourceList = {
    "8901": resourceOne,
    "8902": resourceTwo,
    "8903": resourceThree
}

linkOne = {
    "id": 1,
    "url": "http://bit.com/xDerS",
    "resources": [
        resourceOne,
        resourceTwo
    ]
}

linkTwo = {
    "id": 2,
    "url": "http://bit.com/aDeRe",
    "resources": [
        resourceThree
    ]
}


linkData = {
    "1": linkOne,
    "2": linkTwo,
}


def getLink(id):
    print(f'Link Data ID: {id}')
    print(linkData.get(id))
    return linkData.get(id)


def getResource(id):
    print(f'Resource Data ID: {id}')
    print(resourceList.get(id))
    return resourceList.get(id)
test_nested_queries.py
import pytest

from gql import Client, gql
from .nested_schema import NestedSchema


@pytest.fixture
def client():
    return Client(schema=NestedSchema)


def test_nested_query_without_nested_params(client):
    query = gql(
        """
        query getLinks($someId: String!){
            links(id: $someId) {
                id
                url
                resources {
                    id
                    type
                    active
                }
            }
        }
        """
    )
    params = {
        "someId": "1",
    }
    expected = {
        "links": [
            {
                'id': 1,
                'url': 'http://bit.com/xDerS',
                'resources': [
                    {'id': 8901, 'type': 'file', 'active': True},
                    {'id': 8902, 'type': 'file', 'active': False}
                ]
            }
        ]
    }
    result = client.execute(query, variable_values=params)
    print(result)
    assert result == expected


def test_nested_query_with_nested_params(client):
    query = gql(
        """
        query getLinks($linkId: String!, $resourceId: String!){
            links(id: $linkId) {
                id
                url
                resources(id: $resourceId) {
                    id
                    type
                    active
                }
            }
        }
        """
    )
    params = {
        "linkId": "1",
        "resourceId": "8902"
    }
    expected = {
        "links": [
            {
                'id': 1,
                'url': 'http://bit.com/xDerS',
                'resources': [
                    {'id': 8902, 'type': 'file', 'active': False}
                ]
            }
        ]
    }
    result = client.execute(query, variable_values=params)
    print(result)
    assert result == expected

Hope it helps.

from gql.

Cito avatar Cito commented on May 20, 2024

@gola077 is this issue solved for you?

from gql.

KingDarBoja avatar KingDarBoja commented on May 20, 2024

Looks like the author hasn't answered in a while so I will close this issue and mark as solved after the next 15 days.

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.