Provide sample applications or code snippets in various programming languages that demonstrate how to integrate with your API. This gives users a practical starting point for their own implementations. These samples will also be used to provide use cases for the Zapier integration.
Example 1: Retrieve a List of Items (Parts)
This example shows how to perform a GraphQL query to retrieve items from the item master database. This may be used to synchronize to another system or, in the case of our Replicator application, to download the latest updates for a local shadow copy of the item master for CAD purposes.
The following should be demonstrated:
- Downloading the full item master listing, showing how to configure the query for specific fields of interest (e.g. inventory). This example should also illustrate how to use pagination.
- Downloading the items that have been updated since “one week ago” to show how filtering could be applied.
List All Parts and Revisions
import json
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
auth_token = "oid3vLgynoy_Yl1gZkrgkLEq3J"
graphql_endpoint = "https://demo.aligni.org/api/v3/graphql"
# Create a transport to the GraphQL endpoint with the "Authentication" header
transport = RequestsHTTPTransport(
url=graphql_endpoint,
use_json=True,
headers={"Authorization": f"Token {auth_token}"}
)
# Create a GraphQL client with the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
# Define the GraphQL query with pagination parameters
graphql_query = gql('''
query ($after: String) {
parts(after: $after) {
nodes {
id
partNumber
manufacturerPn
reorderQuantity
revisions {
nodes {
id
active
description
revisionName
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
''')
all_parts = []
has_next_page = True
variables = {"after": None}
# Loop to fetch all pages
while has_next_page:
response = client.execute(graphql_query, variable_values=variables)
parts = response['parts']['nodes']
page_info = response['parts']['pageInfo']
all_parts.extend(parts)
variables['after'] = page_info['endCursor']
has_next_page = page_info['hasNextPage']
# Print the response in a nice format
formatted_response = json.dumps(all_parts, indent=2)
print(formatted_response)
List Parts Updated in the Last Week
import json
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
from datetime import datetime, timedelta
auth_token = "oid3vLgynoy_Yl1gZkrgkLEq3J"
graphql_endpoint = "https://demo.aligni.org/api/v3/graphql"
# Create a transport to the GraphQL endpoint with the "Authentication" header
transport = RequestsHTTPTransport(
url=graphql_endpoint,
use_json=True,
headers={"Authorization": f"Token {auth_token}"}
)
# Create a GraphQL client with the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
one_week_ago = (datetime.utcnow() - timedelta(weeks=1)).isoformat()
# Define the GraphQL query with the updated filter and pagination parameters
graphql_query = gql('''
query ($after: String, $updatedAt: OperatorScalar) {
parts(after: $after, filters: [{field: "updatedAt", value: {gt: $updatedAt}}]) {
nodes {
id
partNumber
manufacturerPn
updatedAt
}
pageInfo {
endCursor
hasNextPage
}
}
}
''')
all_parts = []
has_next_page = True
variables = {"after": None, "updatedAt": one_week_ago}
# Loop to fetch all pages
while has_next_page:
response = client.execute(graphql_query, variable_values=variables)
parts = response['parts']['nodes']
page_info = response['parts']['pageInfo']
all_parts.extend(parts)
variables['after'] = page_info['endCursor']
has_next_page = page_info['hasNextPage']
# Print the response in a nice format
formatted_response = json.dumps(all_parts, indent=2)
print(formatted_response)
Example 2: Creating a New Part
This example shows how to apply a mutation to create new records in the item master. It should also illustrate error handling, specifically recognizing if the subscription limit has been reached.
The following should be demonstrated:
- Create a new part and set parameters and custom parameters.
- Modify an existing part’s lifecycle parameters (no revision release).
- Create a new revision and release it.
- Create a BOM from existing parts and release it. (this may be integrated into any of the above cases)
Create New Part
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
import json
auth_token = "oid3vLgynoy_Yl1gZkrgkLEq3J"
graphql_endpoint = "https://demo.aligni.org/api/v3/graphql"
# Create a transport to the GraphQL endpoint with the "Authentication" header
transport = RequestsHTTPTransport(
url=graphql_endpoint,
use_json=True,
headers={"Authorization": f"Token {auth_token}"}
)
# Create a GraphQL client with the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
# Define the GraphQL mutation
graphql_mutation = gql('''
mutation ($partInput: PartCreateInput!) {
partCreate(partInput: $partInput) {
errors
part {
id
manufacturerPn
partNumber
}
}
}
''')
# Define the input for the mutation
part_input = {
"partInput": {
"activeRevisionAttributes": {"revisionName": "A"},
"unitId": "uom_0000000000GT76C8N91WQPVHWR",
"parttypeId": "itt_0000000000608ACZVRB1F5J7WW",
"manufacturerId": "mfr_011WVJ243RP8WGN22ZD9715MJQ",
"manufacturerPn": "TD-0025"
}
}
# Execute the mutation
response = client.execute(graphql_mutation, variable_values=part_input)
# Print the response in a nice format
formatted_response = json.dumps(response, indent=2)
print(formatted_response)
Update Lifecycle Parameter
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
import json
auth_token = "oid3vLgynoy_Yl1gZkrgkLEq3J"
graphql_endpoint = "https://demo.aligni.org/api/v3/graphql"
# Create a transport to the GraphQL endpoint with the "Authentication" header
transport = RequestsHTTPTransport(
url=graphql_endpoint,
use_json=True,
headers={"Authorization": f"Token {auth_token}"}
)
# Create a GraphQL client with the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
# Define the GraphQL mutation
graphql_mutation = gql('''
mutation ($partId: ID!, $partInput: PartInput!) {
partUpdate(partId: $partId, partInput: $partInput) {
errors
part {
id
manufacturerPn
}
}
}
''')
# Define the input variables for the mutation
part_id = "itm_0113ZW53XRJ3NRMHHE07R2NSVN"
part_input = {
"attrition": 4
}
variables = {
"partId": part_id,
"partInput": part_input
}
# Execute the mutation
response = client.execute(graphql_mutation, variable_values=variables)
# Print the response in a nice format
formatted_response = json.dumps(response, indent=2)
print(formatted_response)
Example 4: Retrieve a Purchase and Purchase Order
This example would be commonly used to synchronize a purchase order record on an accounting system to Aligni’s PO.
The following should be demonstrated:
- Retrieve a list of purchase orders from Aligni. Note: Most accounting systems don’t have a separate notion of Purchase and PurchaseOrder. This “packaging” is unique to Aligni.
- Retrieve a NEW purchase order from Aligni including all items
- Retrieve an existing purchase order that has changed.
List All Purchase Orders
The following script lists all purchase orders, including the purchase name and the buyer:
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
import json
auth_token = "oid3vLgynoy_Yl1gZkrgkLEq3J"
graphql_endpoint = "https://demo.aligni.org/api/v3/graphql"
# Create a transport to the GraphQL endpoint with the "Authentication" header
transport = RequestsHTTPTransport(
url=graphql_endpoint,
use_json=True,
headers={"Authorization": f"Token {auth_token}"}
)
# Create a GraphQL client with the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
# Define the GraphQL query with pagination
graphql_query = gql('''
query ($after: String) {
purchaseOrders(after: $after) {
nodes {
id
status
purchase {
name
account {
id
username
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
''')
all_purchase_orders = []
has_next_page = True
variables = {"after": None}
# Loop to fetch all pages
while has_next_page:
response = client.execute(graphql_query, variable_values=variables)
purchase_orders = response['purchaseOrders']['nodes']
page_info = response['purchaseOrders']['pageInfo']
# Add the fetched purchase orders to the list of all purchase orders
all_purchase_orders.extend(purchase_orders)
# Update variables for the next page
variables['after'] = page_info['endCursor']
has_next_page = page_info['hasNextPage']
# Print the response in a nice format
formatted_response = json.dumps(all_purchase_orders, indent=2)
print(formatted_response)
List All Purchase Orders Updated in the Last Three Days
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
import json
from datetime import datetime, timedelta
auth_token = "oid3vLgynoy_Yl1gZkrgkLEq3J"
graphql_endpoint = "https://demo.aligni.org/api/v3/graphql"
# Create a transport to the GraphQL endpoint with the "Authentication" header
transport = RequestsHTTPTransport(
url=graphql_endpoint,
use_json=True,
headers={"Authorization": f"Token {auth_token}"}
)
# Create a GraphQL client with the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
# Calculate the date 3 days ago
three_days_ago = (datetime.utcnow() - timedelta(days=3)).isoformat()
# Define the GraphQL query with pagination and date filter
graphql_query = gql('''
query ($after: String, $updatedAt: OperatorScalar) {
purchaseOrders(after: $after, filters: [{field: "updatedAt", value: {gt: $updatedAt}}]) {
nodes {
id
status
updatedAt
purchase {
name
account {
id
username
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
''')
all_purchase_orders = []
has_next_page = True
variables = {
"after": None,
"updatedAt": three_days_ago
}
# Loop to fetch all pages
while has_next_page:
# Make the GraphQL request using the client with pagination variables
response = client.execute(graphql_query, variable_values=variables)
# Extract purchase orders and pageInfo from the response
purchase_orders = response['purchaseOrders']['nodes']
page_info = response['purchaseOrders']['pageInfo']
# Add the fetched purchase orders to the list of all purchase orders
all_purchase_orders.extend(purchase_orders)
# Update variables for the next page
variables['after'] = page_info['endCursor']
has_next_page = page_info['hasNextPage']
# Print the response in a nice format
formatted_response = json.dumps(all_purchase_orders, indent=2)
print(formatted_response)