Using GraphQL Fragments in PHP

You can execute GraphQL queries in PHP. In this case, we even show using a GraphQL Fragment.

add_action( 'init', function() {

	$results = graphql([
		'query' => '
		{
		  posts {
		    nodes {
		      ...PostFields
		    }
		  }
		}
		fragment PostFields on Post {
		  id
		  title
		}
		',
	]);

	var_dump( $results );
	die();

} );

Executing this code leads to the following output:

PHP output of executing Graphql

Additionally, if you were to define your fragment in another file, such as the file that is rendering the data, you can define fragments as variables and concatenate them like so:

$fragment = '
  fragment PostFields on Post {
    id
    title
}
';

$results = graphql([
  'query' => '
    {
      posts {
        nodes {
	  ...PostFields
	}
      }
    }
  ' . $fragment ,
]);