Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

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

Solved

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

chrisan
Tourist
7 1 1

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...

 

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

Accepted Solution (1)

chrisan
Tourist
7 1 1

This is an accepted solution.

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 = <<<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]);

View solution in original post

Replies 2 (2)

chrisan
Tourist
7 1 1

west coast bump 🙂

chrisan
Tourist
7 1 1

This is an accepted solution.

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 = <<<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]);