Skip to content

Commit

Permalink
Merge pull request #110 from bookernath/gql
Browse files Browse the repository at this point in the history
Add support for GraphQL, release 0.23.0
  • Loading branch information
bookernath authored Mar 30, 2023
2 parents 092c989 + 7043eb3 commit 725bbd7
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 2 deletions.
25 changes: 24 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,30 @@ it can be used to access V3 APIs using the OAuthConnection object:
api_path='/stores/{}/v3/{}')
v3client.get('/catalog/products', include_fields='name,sku', limit=5, page=1)

Managing OAuth Rate Limits
Accessing GraphQL Admin API
~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is a basic GraphQL client which allows you to submit GraphQL queries to the GraphQL Admin API.

::

gql = bigcommerce.connection.GraphQLConnection(
client_id=client_id,
store_hash=store_hash,
access_token=access_token
)
# Make a basic query
time_query_result = gql.query("""
query {
system {
time
}
}
""")
# Fetch the schema
schema = gql.introspection_query()


Managing Rate Limits
~~~~~~~~~~~~~~~~~~~~~~~~~~

You can optionally pass a ``rate_limiting_management`` object into ``bigcommerce.api.BigcommerceApi`` or ``bigcommerce.connection.OAuthConnection`` for automatic rate limiting management, ex:
Expand Down
117 changes: 117 additions & 0 deletions bigcommerce/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,120 @@ def _handle_response(self, url, res, suppress_empty=True):
callback()

return result


class GraphQLConnection(OAuthConnection):
def __init__(self, client_id, store_hash, access_token=None, host='api.bigcommerce.com',
api_path='/stores/{}/graphql', rate_limiting_management=None):
self.client_id = client_id
self.store_hash = store_hash
self.host = host
self.api_path = api_path
self.graphql_path = "https://" + self.host + self.api_path.format(self.store_hash)
self.timeout = 7.0 # can attach to session?
self.rate_limiting_management = rate_limiting_management

self._session = requests.Session()
self._session.headers = {"Accept": "application/json",
"Accept-Encoding": "gzip"}
if access_token and store_hash:
self._session.headers.update(self._oauth_headers(client_id, access_token))

self._last_response = None # for debugging

self.rate_limit = {}

def query(self, query, variables=None):
return self.post(self.graphql_path, dict(query=query, variables=variables))

def introspection_query(self):
return self.query("""
fragment FullType on __Type {
kind
name
fields(includeDeprecated: true) {
name
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
type {
...TypeRef
}
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
query IntrospectionQuery {
__schema {
queryType {
name
}
mutationType {
name
}
types {
...FullType
}
directives {
name
locations
args {
...InputValue
}
}
}
}
""")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()


VERSION = '0.22.3'
VERSION = '0.23.0'

setup(
name='bigcommerce',
Expand Down

0 comments on commit 725bbd7

Please sign in to comment.