Skip to content

Commit

Permalink
Merge 057d0cf into 69fd92c
Browse files Browse the repository at this point in the history
  • Loading branch information
barbieri committed Dec 17, 2020
2 parents 69fd92c + 057d0cf commit b37f65e
Show file tree
Hide file tree
Showing 12 changed files with 1,707 additions and 594 deletions.
2 changes: 1 addition & 1 deletion examples/github/update-schema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ python3 \
https://api.github.com/graphql \
github_schema.json || exit 1

sgqlc-codegen github_schema.json github_schema.py
sgqlc-codegen schema github_schema.json github_schema.py
38 changes: 9 additions & 29 deletions examples/shopify/list_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import logging
import sys

from sgqlc.operation import Operation # noqa: I900
from sgqlc.endpoint.http import HTTPEndpoint # noqa: I900
from shopify_schema import shopify_schema as schema # noqa: I900

from shopify_operations import Operations # noqa: I900

logger = logging.getLogger(__name__)

Expand All @@ -24,19 +22,6 @@ def price_range_to_text(price_range):
return '%s...%s' % (min_range, max_range)


def select_products(op, first, after):
products = op.products(first=first, after=after)
edges = products.edges
edges.cursor()
edges.node.__fields__(
'id', 'handle', 'description', 'title', 'total_inventory',
)
price_range = edges.node.price_range_v2()
price_range.min_variant_price.__fields__('amount', 'currency_code')
price_range.max_variant_price.__fields__('amount', 'currency_code')
products.page_info.has_next_page()


def print_product(product):
sys.stdout.write('''\
%(handle)s (%(total_inventory)s) - %(title)s
Expand Down Expand Up @@ -77,15 +62,12 @@ def print_product(product):
page_size = args.page_size


op = Operation(schema.QueryRoot)
shop = op.shop
shop.__fields__('name', 'description', 'url')
select_products(op, page_size, None)
op = Operations.query.initial_query

logger.info('Endpoint %s', endpoint)
logger.debug('Operation:\n%s', op)

d = endpoint(op)
d = endpoint(op, {'first': page_size})
errors = d.get('errors')
if errors:
raise SystemExit(errors)
Expand All @@ -100,19 +82,17 @@ def print_product(product):
raise SystemExit()


# NOTE: Shopify doesn't expose page_info.end_cursor,
# then we must get the last one:
last_edge = data.products.edges[-1]

while data.products.page_info.has_next_page and last_edge:
op = Operations.query.load_products
while data.products.page_info.has_next_page:
# NOTE: Shopify doesn't expose page_info.end_cursor,
# then we must get the last one:
last_edge = data.products.edges[-1]
after = last_edge.cursor
op = Operation(schema.QueryRoot)
select_products(op, page_size, after)

logger.info('Downloading next page after: %s', after)
logger.debug('Operation:\n%s', op)

d = endpoint(op)
d = endpoint(op, {'first': page_size, 'after': after})
errors = d.get('errors')
if errors:
raise SystemExit(errors)
Expand Down
43 changes: 43 additions & 0 deletions examples/shopify/shopify_operations.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
query InitialQuery($first: Int!, $after: String=null) {
shop {
name
description
url
}
products(first: $first, after: $after) {
...SelectProducts
}
}

query LoadProducts($first: Int!, $after: String!) {
products(first: $first, after: $after) {
...SelectProducts
}
}

fragment SelectProducts on ProductConnection {
pageInfo { hasNextPage }
edges {
cursor
node {
id
handle
description
title
totalInventory
priceRangeV2 {
minVariantPrice {
...Money
}
maxVariantPrice {
...Money
}
}
}
}
}

fragment Money on MoneyV2 {
amount
currencyCode
}
65 changes: 65 additions & 0 deletions examples/shopify/shopify_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import sgqlc.types
import sgqlc.operation
import shopify_schema

__all__ = ('Operations',)


def fragment_select_products():
_frag = sgqlc.operation.Fragment(shopify_schema.ProductConnection, 'SelectProducts')
_frag_page_info = _frag.page_info()
_frag_page_info.has_next_page()
_frag_edges = _frag.edges()
_frag_edges.cursor()
_frag_edges_node = _frag_edges.node()
_frag_edges_node.id()
_frag_edges_node.handle()
_frag_edges_node.description()
_frag_edges_node.title()
_frag_edges_node.total_inventory()
_frag_edges_node_price_range_v2 = _frag_edges_node.price_range_v2()
_frag_edges_node_price_range_v2_min_variant_price = _frag_edges_node_price_range_v2.min_variant_price()
_frag_edges_node_price_range_v2_min_variant_price.__fragment__(fragment_money())
_frag_edges_node_price_range_v2_max_variant_price = _frag_edges_node_price_range_v2.max_variant_price()
_frag_edges_node_price_range_v2_max_variant_price.__fragment__(fragment_money())
return _frag


def fragment_money():
_frag = sgqlc.operation.Fragment(shopify_schema.MoneyV2, 'Money')
_frag.amount()
_frag.currency_code()
return _frag


class Fragment:
money = fragment_money()
select_products = fragment_select_products()


def query_initial_query():
_op = sgqlc.operation.Operation(shopify_schema.shopify_schema.query_type, name='InitialQuery', first=sgqlc.types.Arg(sgqlc.types.non_null(shopify_schema.shopify_schema.Int)), after=sgqlc.types.Arg(shopify_schema.shopify_schema.String, default=None))
_op_shop = _op.shop()
_op_shop.name()
_op_shop.description()
_op_shop.url()
_op_products = _op.products(first=sgqlc.types.Variable('first'), after=sgqlc.types.Variable('after'))
_op_products.__fragment__(fragment_select_products())
return _op


def query_load_products():
_op = sgqlc.operation.Operation(shopify_schema.shopify_schema.query_type, name='LoadProducts', first=sgqlc.types.Arg(sgqlc.types.non_null(shopify_schema.shopify_schema.Int)), after=sgqlc.types.Arg(sgqlc.types.non_null(shopify_schema.shopify_schema.String)))
_op_products = _op.products(first=sgqlc.types.Variable('first'), after=sgqlc.types.Variable('after'))
_op_products.__fragment__(fragment_select_products())
return _op


class Query:
initial_query = query_initial_query()
load_products = query_load_products()


class Operations:
fragment = Fragment
query = Query
7 changes: 7 additions & 0 deletions examples/shopify/update-operations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

sgqlc-codegen operation \
--schema shopify_schema.json \
shopify_schema \
shopify_operations.py \
shopify_operations.gql
2 changes: 1 addition & 1 deletion examples/shopify/update-schema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ python3 \
https://${SHOP_STORE}.myshopify.com/admin/api/2020-10/graphql.json \
shopify_schema.json || exit 1

sgqlc-codegen shopify_schema.json shopify_schema.py
sgqlc-codegen schema shopify_schema.json shopify_schema.py
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tests= sgqlc/__init__.py,
sgqlc/types/datetime.py,
sgqlc/types/relay.py,
sgqlc/operation/__init__.py,
sgqlc/codegen/operation.py,
tests/test-endpoint-http.py,
tests/test-endpoint-websocket.py,
tests/test-endpoint-requests.py,
Expand All @@ -42,7 +43,8 @@ exclude =
build,
dist,
examples/github/github_schema.py,
examples/shopify/shopify_schema.py
examples/shopify/shopify_schema.py,
examples/shopify/shopify_operations.py

# I801: from x import Y
# RST304: :class:, :func:, :mod: are supported by sphinx
Expand Down

0 comments on commit b37f65e

Please sign in to comment.