PHP code example of secundo / laravel-graphql-query-builder
1. Go to this page and download the library: Download secundo/laravel-graphql-query-builder library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
secundo / laravel-graphql-query-builder example snippets
use Secundo\GraphQL\Builder;
$builder = new Builder();
$query = $builder->query()
->field('products', [], ['id', 'title', 'handle'])
->toGraphQL();
// Output:
// query {
// products {
// id
// title
// handle
// }
// }
$query = $builder->query()
->field('product', ['id' => 'gid://shopify/Product/123'], ['id', 'title', 'handle'])
->toGraphQL();
// Output:
// query {
// product(id: "gid://shopify/Product/123") {
// id
// title
// handle
// }
// }
$query = $builder->query()
->field('products', ['first' => 10], function ($field) {
$field->field('edges', [], function ($field) {
$field->field('node', [], ['id', 'title']);
$field->field('cursor');
});
$field->field('pageInfo', [], ['hasNextPage', 'endCursor']);
})
->toGraphQL();
$query = $builder->query('GetProduct')
->variable('id', 'ID!', 'gid://shopify/Product/123')
->field('product', ['id' => '$id'], ['id', 'title', 'description'])
->toGraphQL();
// Get variable values for the request
$variables = $builder->getVariableValues();
// Output:
// query GetProduct($id: ID!) {
// product(id: $id) {
// id
// title
// description
// }
// }
// Variables: {"id": "gid://shopify/Product/123"}
$mutation = $builder->mutation('UpdateProduct')
->variable('id', 'ID!')
->variable('input', 'ProductInput!')
->field('productUpdate', ['id' => '$id', 'product' => '$input'], function ($field) {
$field->field('product', [], ['id', 'title']);
$field->field('userErrors', [], ['field', 'message']);
})
->toGraphQL();
$query = $builder->query()
->fragment('ProductFields', 'Product', ['id', 'title', 'handle', 'createdAt'])
->field('products', [], function ($field) {
$field->field('edges', [], function ($field) {
$field->field('node', [], ['...ProductFields']);
});
})
->toGraphQL();
$query = $builder->query()
->field('node', ['id' => '$id'], function ($field) {
$field->field('id');
$field->inlineFragment('Product', function ($fragment) {
$fragment->field('title');
$fragment->field('handle');
});
$field->inlineFragment('Collection', function ($fragment) {
$fragment->field('title');
$fragment->field('description');
});
})
->toGraphQL();
// Output:
// query {
// node(id: $id) {
// id
// ... on Product {
// title
// handle
// }
// ... on Collection {
// title
// description
// }
// }
// }
$query = $builder->query('SearchProducts')
->variables([
'first' => ['type' => 'Int', 'value' => 10],
'query' => ['type' => 'String', 'value' => 'title:shirt'],
'sortKey' => ['type' => 'ProductSortKeys', 'value' => 'CREATED_AT']
])
->field('products', [
'first' => '$first',
'query' => '$query',
'sortKey' => '$sortKey'
], ['id', 'title']);
use Secundo\GraphQL\Types\Field;
$field = new Field('product');
$field->alias('myProduct')
->arguments(['id' => 'gid://shopify/Product/123'])
->fields(['id', 'title']);
$field = new Field('expensiveField');
$field->directive(';
$field->directive('skip', ['if' => '$production'])
->fields(['logs']);
use Secundo\GraphQL\Facades\GraphQL;
$query = GraphQL::query()
->field('shop', [], ['name', 'email'])
->toGraphQL();
use Secundo\GraphQL\GraphQL;
class ProductController extends Controller
{
public function __construct(
private GraphQL $graphql
) {}
public function index()
{
$query = $this->graphql->query()
->field('products', ['first' => 10], ['id', 'title'])
->toGraphQL();
// Execute query...
}
}