Is the GraphQL Admin documentation for PHP correct? Getting syntax errors

Topic summary

Shopify Admin GraphQL PHP example for creating an app subscription (appSubscriptionCreate) triggers PHP 8.3 syntax errors when using the documentation’s variables snippet.

  • Initial issue: PHP parse error “unexpected token ‘{’, expecting ‘]’” originates from JSON-like braces in the $variables array (especially lineItems) shown in the docs, which is invalid PHP array syntax.
  • Second attempt: After converting to PHP-style arrays, new errors report undefined variables like $name, suggesting a mismatch between the GraphQL mutation’s declared variables and the provided variables payload/structure.

Latest update: The original poster resolved all errors and indicated the docs likely need updates, but the shared “working” code sample is truncated, so the exact fix is not visible.

Outcome and status:

  • Issue appears resolved for the poster.
  • No complete, verified working PHP snippet is available in the thread.
  • Key takeaway: Use proper PHP array syntax for variables (no JSON-style {}) and ensure all mutation variables ($name, $returnUrl, $trialDays, $lineItems) are supplied with the correct structure (lineItems as a list of items). Code snippets are central to this discussion.
Summarized with AI on December 31. AI used: gpt-5.

Hello, first time using shopify (and GraphQL for that matter). We are looking to create a subscription for our app but the PHP code to initiate this is giving errors: https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/appSubscriptionCreate#examples-Create_a_subscription_with_a_free_trial_

use Shopify\Clients\Graphql;

$client = new Graphql("your-development-store.myshopify.com", $accessToken);
$query = <<<QUERY
mutation AppSubscriptionCreate($name: String!, $lineItems: [AppSubscriptionLineItemInput!]!, $returnUrl: URL!, $trialDays: Int) {
appSubscriptionCreate(name: $name, returnUrl: $returnUrl, lineItems: $lineItems, trialDays: $trialDays) {
userErrors {
field
message
}
appSubscription {
id
}
confirmationUrl
}
}
QUERY;

$variables = [
"name" => "Super Duper Recurring Plan with a Trial",
"returnUrl" => "http://super-duper.shopifyapps.com/",
"trialDays" => 7,
"lineItems" => [{"plan"=>{"appRecurringPricingDetails"=>{"price"=>{"amount"=>10.0, "currencyCode"=>"USD"}}}}],
];

$response = $client->query(["query" => $query, "variables" => $variables]);

“Parse error: syntax error, unexpected token “{”, expecting “]””

If I change the $variables to the following to match what I know as PHP’s style:

$variables = [
"name" => "Super Duper Recurring Plan with a Trial",
 "returnUrl" => "http://super-duper.shopifyapps.com/",
 "trialDays" => 7,
 "lineItems" => ["plan"=> [
"appRecurringPricingDetails"=> [
"price"=>["amount"=>10.0, "currencyCode"=>"USD"]
]
]
]];

I now get undefined variables for $name etc, but my understanding of GraphQL is getting in the way.

Can anyone provide a working example? Running PHP 8.3

west coast bump :slightly_smiling_face:

OK worked out all the errors, for anyone who runs into this in the future and if shopify doesn’t update their docs

$client = new Graphql("your-development-store.myshopify.com", $accessToken);
    $query = <<