A simple library that adds support for .gql
and .graphql
files in your PHP application.
- Load GraphQL queries from
.graphql
and.gql
files. - Supports variable replacement within queries.
- Includes fragment files via
#include "fragment.graphql"
syntax. - Prevents duplicate fragment loading.
- PHP >=8.1
$ composer require cordero-digital/gql-query-loader
- Download the latest release
- Extract the
.zip
or.tar.gz
to your machine - Copy
/src/Loader.php
file into your project - Include the
Loader.php
file into the PHP file you need the library into
require_once __DIR__ . '/src/Loader.php';
use CorderoDigital\GQLQueryLoader\Loader;
$loader = new Loader;
This package supports fragments. The package assumes the fragments are located in a sub directory called /fragments
located next to the query.
This package supports passing PHP values into Graphql files by doing string replacement. This allows you to hardcode your PHP variables into the compiled Graphql query. A couple things to note:
You pass the variables in to the loadQuery()
method where the key is the variable name in the Graphql file. Ex:
// Your PHP file
$query = (new Loader)->loadQuery($this->root . 'queries/variableNotInArrayExceptionTest.gql', ['id' => 123])->query();
# Your graphql file
query {
user{{ id }} {
id
name
}
}
# result
query {
user(id: 123) {
id
name
}
}
If you want multiple hard coded variables on a single object you would do:
// your PHP file
$query = (new Loader)->loadQuery($this->root . 'queries/variableNotInArrayExceptionTest.gql', ['id' => 123, 'status' => 'active'])->query();
# Your graphql file
query {
user{{ id, status }} {
id
name
}
}
# result
query {
user(id: 123, status: "active") {
id
name
}
}