PHP code example of mathsgod / array_to_gql

1. Go to this page and download the library: Download mathsgod/array_to_gql 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/ */

    

mathsgod / array_to_gql example snippets




// Simple query - all non-false values only show key names
$result = array_to_gql([
    'user' => [
        'id' => 1,           // Number value → only show key name
        'name' => 'John',    // String value → only show key name
        'email' => true,     // true value → show key name
        'phone' => false     // false value → ignored
    ]
]);
// Output: user { id name email }

// Value processing rules example
$result = array_to_gql([
    'users' => [
        'name' => [
            'first' => 'John',    // String → only show first
            'last' => 'Doe'       // String → only show last
        ],
        'age' => 25,              // Number → only show age
        'active' => true,         // true → show active
        'deleted' => false,       // false → completely ignored
        'status' => 'online'      // String → only show status
    ]
]);
// Output: users { name { first last } age active status }

// Simple parameters
$result = array_to_gql([
    'users' => [
        '__args' => [
            'limit' => 10
        ],
        'id' => true,
        'name' => true
    ]
]);
// Output: users(limit: "10") { id name }

// Object parameters
$result = array_to_gql([
    'users' => [
        '__args' => [
            'search' => [
                'first_name' => 'John',
                'last_name' => 'Doe'
            ]
        ],
        'id' => true,
        'name' => true
    ]
]);
// Output: users(search: {first_name: "John", last_name: "Doe"}) { id name }

$result = array_to_gql([
    'allUsers' => [
        '__aliasFor' => 'users',
        '__args' => [
            'status' => 'active'
        ],
        'id' => true,
        'name' => true
    ]
]);
// Output: allUsers: users(status: "active") { id name }

$result = array_to_gql([
    'posts' => [
        '__args' => [
            'limit' => 10
        ],
        'id' => true,
        'title' => true,
        'author' => [
            'name' => true,
            'profile' => [
                'bio' => true,
                'avatar' => true
            ]
        ]
    ]
]);
// Output: posts(limit: "10") { id title author { name profile { bio avatar } } }