Comments (9)
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.
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.
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.
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.
@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.
Thank you @KingDarBoja! Example would be wonderful.
from gql.
@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.
@gola077 is this issue solved for you?
from gql.
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)
- DSL Question HOT 5
- `KeyError` when parsing result using fragment and nested field HOT 3
- 3.5 Beta: overlapping_fields_can_be_merged: directives are None HOT 9
- DSL, Fragments and unions HOT 4
- gql should have a websockets-client transport HOT 4
- Unable to print schema with gql and error is an SSL error HOT 1
- Cannot use gql with httpx transport and trio HOT 3
- Tests fail out of the box. HOT 2
- Any best practice using async gql with FastAPI? HOT 2
- DSL how to select fields which are Python reserved keywords HOT 2
- [Feature] Support Subscriptions HTTP Multipart Protocol HOT 6
- Handling decimals with longer than 6 scale? HOT 7
- Timeout error during execution of mutation when no of async tasks is above 5000 + HOT 1
- Request/Response payloads logged at level INFO HOT 5
- requests_toolbelt isn't listed as an optional dependency although required for logging changes HOT 1
- Add retry logic to httpx Transport HOT 4
- Shopify error: Prefix "query" when sending a mutation HOT 3
- retry_execute can not handle TransportQueryError HOT 1
- Error when using gqc client under windows HOT 6
- Add change log to repository main directory? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gql.