I’m having trouble executing this API GraphQL mutation: companyAssignCustomerAsContact
I have a company and customer created but I cannot create/assign that customer as a contact to that company I was following the documentation and tried implementing it with Python.
import json
import requests
def assign_customer_as_contact(url, access_token, companyId, customerId):
# Define the GraphQL mutation
mutation = """
mutation {
companyAssignCustomerAsContact(
customerId: "gid://shopify/Product/your_contact_id"
companyId: "gid://shopify/Product/your_company_id"
) {
userErrors {
field
message
}
}
}
"""
# Prepare the variables for the mutation
mutation = mutation.replace('your_contact_id', customerId)
mutation = mutation.replace('your_company_id', companyId)
print(mutation)
# Setup headers for the request
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": access_token,
}
# Setup the request payload
payload = {"query": mutation}
# Send the request to the Shopify GraphQL API
response = requests.post(url, headers=headers, json=payload)
# Handle the response
if response.status_code == 200:
data = response.json()
if "errors" in data:
print("Error:", data["errors"])
else:
print("Customer assigned as contact successfully.")
print(json.dumps(data["data"], indent=2))
else:
print("Failed to assign customer as contact.", response.text)
if __name__ == "__main__":
api_url = ''
api_access_token = ''
company_id = "3131"
customer_id = "3131"
# Call the function to assign customer as contact
assign_customer_as_contact(api_url, api_access_token, company_id, customer_id)
I got the following error:
Error: [{‘message’: ‘invalid id’, ‘locations’: [{‘line’: 3, ‘column’: 3}], ‘path’: [‘companyAssignCustomerAsContact’]}]
What am I doing wrong I assume they are referring to these customers.