GraphQL: DraftOrderInput isn't a defined input type (on $input)

Solved
Highlighted
Excursionist
22 2 1

I'm trying to create a draft order using the GraphQL API with the following mutation:

 

mutation draftOrderCreate($input: DraftOrderInput!) {
  draftOrderCreate(input: $input) {
    userErrors {
      field
      message
    }
    draftOrder {
      id
    }
  }
}

# Variables
{
  "operationName": "draftOrderCreate",
  "variables": {
    "input": {
      "email": "-----@gmail.com",
      "lineItems": [
        {
          "variantId": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8xMzYxNjg0ODY2NjY3OQ==",
          "quantity": 1
        },
        {
          "variantId": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8xMzYxNjg1MjEwNzMxOQ==",
          "quantity": 1
        }
      ]
    }
  },
  "query": "mutation draftOrderCreate($input: DraftOrderInput!) {  draftOrderCreate(input: $input) {    userErrors {      field      message      __typename    }    draftOrder {      id      __typename    }    __typename  }}"
}

This returns the error: DraftOrderInput isn't a defined input type (on $input)

 

However, multiple errors are actually returned by the API:

 

{
  "errors": [
    {
      "message": "DraftOrderInput isn't a defined input type (on $input)",
      "locations": [{ "line": 1, "column": 27 }],
      "path": ["mutation draftOrderCreate"],
      "extensions": {
        "code": "variableRequiresValidType",
        "typeName": "DraftOrderInput",
        "variableName": "input"
      }
    },
    {
      "message": "Field 'draftOrderCreate' doesn't exist on type 'Mutation'",
      "locations": [{ "line": 2, "column": 3 }],
      "path": ["mutation draftOrderCreate", "draftOrderCreate"],
      "extensions": {
        "code": "undefinedField",
        "typeName": "Mutation",
        "fieldName": "draftOrderCreate"
      }
    },
    {
      "message": "Variable $input is declared by draftOrderCreate but not used",
      "locations": [{ "line": 1, "column": 1 }],
      "path": ["mutation draftOrderCreate"],
      "extensions": { "code": "variableNotUsed", "variableName": "input" }
    }
  ]
}

 

 

0 Likes
Highlighted
Shopify Staff
Shopify Staff
491 97 88

Hi @kneeki 

 

Can you try again with only the required fields as stated in the docs. Here is a working example: 

 

mutation draftOrderCreate($input: DraftOrderInput!) {  
draftOrderCreate(input: $input) {
    userErrors {
      field
      message
    }
    draftOrder {
      id
    }
  }
}

With the variables (please replace with your customer + variant): 

{
	"input": {
		"customerId": "gid://shopify/Customer/123456789",
		"lineItems": [
			{
				"quantity": 1,
				"variantId": "gid:\/\/shopify\/ProductVariant\/12345678"
			}
		]
	}
}

Vix | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 


0 Likes
Highlighted
Excursionist
12 1 0

That's the exact mutation I am using. :) Is customerId a requirement? It doesn't say so in the docs.

0 Likes
Highlighted
Shopify Expert
4178 29 401

You do not need a customer ID. My mutation works with an email, some line items, tags and a billing address. That is it, and it worked just fine. Anything you leave out is filled in later in the admin.

Note that your input data looked weird. You pasted in cursor data to variant IDs? Why?

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes
Highlighted
Shopify Staff
Shopify Staff
491 97 88

@HunkyBill @kneeki 

 

The IDs being used are the Storefront API id (base64 encoded versions of gid). Please use the admin API id as noted in my example and ensure you are using the correct endpoint. 

Vix | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 


0 Likes
Highlighted
Excursionist
22 2 1

That's good to know, @HunkyBill. I'm retrieving the Id from the .id property as shown in the docs. Is there some other Id I should be using?

@vix, I'm trying to avoid using the admin API because it requires the use of a server. If possible, I'd like to create checkouts, orders, and fetch products with variants all while using the GraphQL API.

0 Likes
Highlighted
Shopify Staff
Shopify Staff
491 97 88

This is an accepted solution.

@kneeki 

 

The mutation you are using is for the admin api. Only what is listed in the Storefront API reference is able to be used: https://shopify.dev/docs/storefront-api/reference you can create orders, but not draft orders from the Storefront. This is done because the Storefront API is used to power buyer facing experiences, and typically a draft order is created as an invoice by the brand itself vs. the customer. 

You would need to use the admin API server side to create a draft order, or find a workaround with a free checkout on the Storefront API etc. Note: they are both in GraphQL but there is still a difference! 

 

Vix | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 


0 Likes
Highlighted
Excursionist
22 2 1

@vix 

Okay, so I've been using the admin GraphQL API instead of the Storefront API, that's a bummer. When trying to use the checkoutCompleteFree mutation I'm given the error: 

{
  "data": {
    "checkoutCompleteFree": {
      "checkout": null,
      "checkoutUserErrors": [
        {
          "code": "MISSING_PAYMENT_INPUT",
          "field": null,
          "message": "Checkout requires payment.",
          "__typename": "CheckoutUserError"
        }
      ],
      "__typename": "CheckoutCompleteFreePayload"
    }
  }
}

With the payload:

{
  "operationName": "checkoutCompleteFree",
  "variables": {
    "checkoutId": "Z2lkOi8vc2hvcGlmeS9DaGVja291dC9hM2I1NjdjYmI5ODJiNmVkNjUzNDE1NTJmMDM0YWRhZT9rZXk9OWViNWI1YjZhN2E2MDc2NDU3YjQ4MGFmZWMyMDM2ZTQ="
  },
  "query": "mutation checkoutCompleteFree($checkoutId: ID!) {  checkoutCompleteFree(checkoutId: $checkoutId) {    checkout {      id      __typename    }    checkoutUserErrors {      code      field      message      __typename    }    __typename  }}"
}

I'm creating the checkout with the following mutation on the Storefront API:

mutation checkoutCreate($input: CheckoutCreateInput!) {
  checkoutCreate(input: $input) {
    checkout {
      id
      availableShippingRates {
        ready
        shippingRates {
          handle
          priceV2 {
            amount
            currencyCode
          }
          title
        }
      }
      orderStatusUrl
      requiresShipping
      totalTax
      taxesIncluded
      taxExempt
      subtotalPrice
      totalPrice
      email
      webUrl
    }
    checkoutUserErrors {
      code
      field
      message
    }
  }
}

It could totally be that I'm misunderstanding checkoutCompleteFree. I'm wanting to finalize the checkout to create an order because the payment processing is being done through a 3rd party.

0 Likes