pixelandtonic/graphql-php
PHP implementation of the GraphQL specification (based on graphql-js). Build schemas, execute queries, and add custom types, fields, and resolvers. Install via Composer and explore full docs and ready-to-run examples.
This is the primary facade for fulfilling GraphQL operations. See related documentation.
Class Methods:
/**
* Executes graphql query.
*
* More sophisticated GraphQL servers, such as those which persist queries,
* may wish to separate the validation and execution phases to a static time
* tooling step, and a server runtime step.
*
* Available options:
*
* schema:
* The GraphQL type system to use when validating and executing a query.
* source:
* A GraphQL language formatted string representing the requested operation.
* rootValue:
* The value provided as the first argument to resolver functions on the top
* level type (e.g. the query object type).
* contextValue:
* The context value is provided as an argument to resolver functions after
* field arguments. It is used to pass shared information useful at any point
* during executing this query, for example the currently logged in user and
* connections to databases or other services.
* variableValues:
* A mapping of variable name to runtime value to use for all variables
* defined in the requestString.
* operationName:
* The name of the operation to use if requestString contains multiple
* possible operations. Can be omitted if requestString contains only
* one operation.
* fieldResolver:
* A resolver function to use when one is not provided by the schema.
* If not provided, the default field resolver is used (which looks for a
* value on the source value with the field's name).
* validationRules:
* A set of rules for query validation step. Default value is all available rules.
* Empty array would allow to skip query validation (may be convenient for persisted
* queries which are validated before persisting and assumed valid during execution)
*
* [@param](https://github.com/param) string|DocumentNode $source
* [@param](https://github.com/param) mixed $rootValue
* [@param](https://github.com/param) mixed $contextValue
* [@param](https://github.com/param) mixed[]|null $variableValues
* [@param](https://github.com/param) ValidationRule[] $validationRules
*
* [@api](https://github.com/api)
*/
static function executeQuery(
GraphQL\Type\Schema $schema,
$source,
$rootValue = null,
$contextValue = null,
$variableValues = null,
string $operationName = null,
callable $fieldResolver = null,
array $validationRules = null
): GraphQL\Executor\ExecutionResult
/**
* Same as executeQuery(), but requires PromiseAdapter and always returns a Promise.
* Useful for Async PHP platforms.
*
* [@param](https://github.com/param) string|DocumentNode $source
* [@param](https://github.com/param) mixed $rootValue
* [@param](https://github.com/param) mixed $context
* [@param](https://github.com/param) mixed[]|null $variableValues
* [@param](https://github.com/param) ValidationRule[]|null $validationRules
*
* [@api](https://github.com/api)
*/
static function promiseToExecute(
GraphQL\Executor\Promise\PromiseAdapter $promiseAdapter,
GraphQL\Type\Schema $schema,
$source,
$rootValue = null,
$context = null,
$variableValues = null,
string $operationName = null,
callable $fieldResolver = null,
array $validationRules = null
): GraphQL\Executor\Promise\Promise
/**
* Returns directives defined in GraphQL spec
*
* [@return](https://github.com/return) Directive[]
*
* [@api](https://github.com/api)
*/
static function getStandardDirectives(): array
/**
* Returns types defined in GraphQL spec
*
* [@return](https://github.com/return) Type[]
*
* [@api](https://github.com/api)
*/
static function getStandardTypes(): array
/**
* Replaces standard types with types from this list (matching by name)
* Standard types not listed here remain untouched.
*
* [@param](https://github.com/param) array<string, ScalarType> $types
*
* [@api](https://github.com/api)
*/
static function overrideStandardTypes(array $types)
/**
* Returns standard validation rules implementing GraphQL spec
*
* [@return](https://github.com/return) ValidationRule[]
*
* [@api](https://github.com/api)
*/
static function getStandardValidationRules(): array
/**
* Set default resolver implementation
*
* [@api](https://github.com/api)
*/
static function setDefaultFieldResolver(callable $fn): void
Registry of standard GraphQL types and a base class for all other types.
Class Methods:
/**
* [@api](https://github.com/api)
*/
static function id(): GraphQL\Type\Definition\ScalarType
/**
* [@api](https://github.com/api)
*/
static function string(): GraphQL\Type\Definition\ScalarType
/**
* [@api](https://github.com/api)
*/
static function boolean(): GraphQL\Type\Definition\ScalarType
/**
* [@api](https://github.com/api)
*/
static function int(): GraphQL\Type\Definition\ScalarType
/**
* [@api](https://github.com/api)
*/
static function float(): GraphQL\Type\Definition\ScalarType
/**
* [@api](https://github.com/api)
*/
static function listOf(GraphQL\Type\Definition\Type $wrappedType): GraphQL\Type\Definition\ListOfType
/**
* [@param](https://github.com/param) callable|NullableType $wrappedType
*
* [@api](https://github.com/api)
*/
static function nonNull($wrappedType): GraphQL\Type\Definition\NonNull
/**
* [@param](https://github.com/param) Type $type
*
* [@api](https://github.com/api)
*/
static function isInputType($type): bool
/**
* [@param](https://github.com/param) Type $type
*
* [@api](https://github.com/api)
*/
static function getNamedType($type): GraphQL\Type\Definition\Type
/**
* [@param](https://github.com/param) Type $type
*
* [@api](https://github.com/api)
*/
static function isOutputType($type): bool
/**
* [@param](https://github.com/param) Type $type
*
* [@api](https://github.com/api)
*/
static function isLeafType($type): bool
/**
* [@param](https://github.com/param) Type $type
*
* [@api](https://github.com/api)
*/
static function isCompositeType($type): bool
/**
* [@param](https://github.com/param) Type $type
*
* [@api](https://github.com/api)
*/
static function isAbstractType($type): bool
/**
* [@api](https://github.com/api)
*/
static function getNullableType(GraphQL\Type\Definition\Type $type): GraphQL\Type\Definition\Type
Structure containing information useful for field resolution process.
Passed as 4th argument to every field resolver. See docs on field resolving (data fetching).
Class Props:
/**
* The definition of the field being resolved.
*
* [@api](https://github.com/api)
* [@var](https://github.com/var) FieldDefinition
*/
public $fieldDefinition;
/**
* The name of the field being resolved.
*
* [@api](https://github.com/api)
* [@var](https://github.com/var) string
*/
public $fieldName;
/**
* Expected return type of the field being resolved.
*
* [@api](https://github.com/api)
* [@var](https://github.com/var) Type
*/
public $returnType;
/**
* AST of all nodes referencing this field in the query.
*
* [@api](https://github.com/api)
* [@var](https://github.com/var) FieldNode[]
*/
public $fieldNodes;
/**
* Parent type of the field being resolved.
*
* [@api](https://github.com/api)
* [@var](https://github.com/var) ObjectType
*/
public $parentType;
/**
* Path to this field from the very root value.
*
* [@api](https://github.com/api)
* [@var](https://github.com/var) string[]
*/
public $path;
/**
* Instance of a schema used for execution.
*
* [@api](https://github.com/api)
* [@var](https://github.com/var) Schema
*/
public $schema;
/**
* AST of all fragments defined in query.
*
* [@api](https://github.com/api)
* [@var](https://github.com/var) FragmentDefinitionNode[]
*/
public $fragments;
/**
* Root value passed to query execution.
*
* [@api](https://github.com/api)
* [@var](https://github.com/var) mixed
*/
public $rootValue;
/**
* AST of operation definition node (query, mutation).
*
* [@api](https://github.com/api)
* [@var](https://github.com/var) OperationDefinitionNode|null
*/
public $operation;
/**
* Array of variables passed to query execution.
*
* [@api](https://github.com/api)
* [@var](https://github.com/var) mixed[]
*/
public $variableValues;
Class Methods:
/**
* Helper method that returns names of all fields selected in query for
* $this->fieldName up to $depth levels.
*
* Example:
* query MyQuery{
* {
* root {
* id,
* nested {
* nested1
* nested2 {
* nested3
* }
* }
* }
* }
*
* Given this ResolveInfo instance is a part of "root" field resolution, and $depth === 1,
* method will return:
* [
* 'id' => true,
* 'nested' => [
* nested1 => true,
* nested2 => true
* ]
* ]
*
* Warning: this method it is a naive implementation which does not take into account
* conditional typed fragments. So use it with care for fields of interface and union types.
*
* [@param](https://github.com/param) int $depth How many levels to include in output
*
* [@return](https://github.com/return) array<string, mixed>
*
* [@api](https://github.com/api)
*/
function getFieldSelection($depth = 0)
List of available directive locations
Class Constants:
const QUERY = "QUERY";
const MUTATION = "MUTATION";
const SUBSCRIPTION = "SUBSCRIPTION";
const FIELD = "FIELD";
const FRAGMENT_DEFINITION = "FRAGMENT_DEFINITION";
const FRAGMENT_SPREAD = "FRAGMENT_SPREAD";
const INLINE_FRAGMENT = "INLINE_FRAGMENT";
const VARIABLE_DEFINITION = "VARIABLE_DEFINITION";
const SCHEMA = "SCHEMA";
const SCALAR = "SCALAR";
const OBJECT = "OBJECT";
const FIELD_DEFINITION = "FIELD_DEFINITION";
const ARGUMENT_DEFINITION = "ARGUMENT_DEFINITION";
const IFACE = "INTERFACE";
const UNION = "UNION";
const ENUM = "ENUM";
const ENUM_VALUE = "ENUM_VALUE";
const INPUT_OBJECT = "INPUT_OBJECT";
const INPUT_FIELD_DEFINITION = "INPUT_FIELD_DEFINITION";
Schema configuration class. Could be passed directly to schema constructor. List of options accepted by create method is described in docs.
Usage example:
$config = SchemaConfig::create()
->setQuery($myQueryType)
->setTypeLoader($myTypeLoader);
$schema = new Schema($config);
Class Methods:
/**
* Converts an array of options to instance of SchemaConfig
* (or just returns empty config when array is not passed).
*
* [@param](https://github.com/param) mixed[] $options
*
* [@return](https://github.com/return) SchemaConfig
*
* [@api](https://github.com/api)
*/
static function create(array $options = [])
/**
* [@return](https://github.com/return) ObjectType|null
*
* [@api](https://github.com/api)
*/
function getQuery()
/**
* [@param](https://github.com/param) ObjectType|null $query
*
* [@return](https://github.com/return) SchemaConfig
*
* [@api](https://github.com/api)
*/
function setQuery($query)
/**
* [@return](https://github.com/return) ObjectType|null
*
* [@api](https://github.com/api)
*/
function getMutation()
/**
* [@param](https://github.com/param) ObjectType|null $mutation
*
* [@return](https://github.com/return) SchemaConfig
*
* [@api](https://github.com/api)
*/
function setMutation($mutation)
/**
* [@return](https://github.com/return) ObjectType|null
*
* [@api](https://github.com/api)
*/
function getSubscription()
/**
* [@param](https://github.com/param) ObjectType|null $subscription
*
* [@return](https://github.com/return) SchemaConfig
*
* [@api](https://github.com/api)
*/
function setSubscription($subscription)
/**
* [@return](https://github.com/return) Type[]|callable
*
* [@api](https://github.com/api)
*/
function getTypes()
/**
* [@param](https://github.com/param) Type[]|callable $types
*
* [@return](https://github.com/return) SchemaConfig
*
* [@api](https://github.com/api)
*/
function setTypes($types)
/**
* [@return](https://github.com/return) Directive[]|null
*
* [@api](https://github.com/api)
*/
function getDirectives()
/**
* [@param](https://github.com/param) Directive[] $directives
*
* [@return](https://github.com/return) SchemaConfig
*
* [@api](https://github.com/api)
*/
function setDirectives(array $directives)
/**
* [@return](https://github.com/return) callable(string $name):Type|null
*
* [@api](https://github.com/api)
*/
function getTypeLoader()
/**
* [@return](https://github.com/return) SchemaConfig
*
* [@api](https://github.com/api)
*/
function setTypeLoader(callable $typeLoader)
Schema Definition (see related docs)
A Schema is created by supplying the root types of each type of operation: query, mutation (optional) and subscription (optional). A schema definition is then supplied to the validator and executor. Usage Example:
$schema = new GraphQL\Type\Schema([
'query' => $MyAppQueryRootType,
'mutation' => $MyAppMutationRootType,
]);
Or using Schema Config instance:
$config = GraphQL\Type\SchemaConfig::create()
->setQuery($MyAppQueryRootType)
->setMutation($MyAppMutationRootType);
$schema = new GraphQL\Type\Schema($config);
Class Methods:
/**
* [@param](https://github.com/param) mixed[]|SchemaConfig $config
*
* [@api](https://github.com/api)
*/
function __construct($config)
/**
* Returns array of all types in this schema. Keys of this array represent type names, values are instances
* of corresponding type definitions
*
* This operation requires full schema scan. Do not use in production environment.
*
* [@return](https://github.com/return) Type[]
*
* [@api](https://github.com/api)
*/
function getTypeMap()
/**
* Returns a list of directives supported by this schema
*
* [@return](https://github.com/return) Directive[]
*
* [@api](https://github.com/api)
*/
function getDirectives()
/**
* Returns schema query type
*
* [@return](https://github.com/return) ObjectType
*
* [@api](https://github.com/api)
*/
function getQueryType(): GraphQL\Type\Definition\Type
/**
* Returns schema mutation type
*
* [@return](https://github.com/return) ObjectType|null
*
* [@api](https://github.com/api)
*/
function getMutationType(): GraphQL\Type\Definition\Type
/**
* Returns schema subscription
*
* [@return](https://github.com/return) ObjectType|null
*
* [@api](https://github.com/api)
*/
function getSubscriptionType(): GraphQL\Type\Definition\Type
/**
* [@return](https://github.com/return) SchemaConfig
*
* [@api](https://github.com/api)
*/
function getConfig()
/**
* Returns type by its name
*
* [@api](https://github.com/api)
*/
function getType(string $name): GraphQL\Type\Definition\Type
/**
* Returns all possible concrete types for given abstract type
* (implementations for interfaces and members of union type for unions)
*
* This operation requires full schema scan. Do not use in production environment.
*
* [@param](https://github.com/param) InterfaceType|UnionType $abstractType
*
* [@return](https://github.com/return) array<Type&ObjectType>
*
* [@api](https://github.com/api)
*/
function getPossibleTypes(GraphQL\Type\Definition\Type $abstractType): array
/**
* Returns true if object type is concrete type of given abstract type
* (implementation for interfaces and members of union type for unions)
*
* [@api](https://github.com/api)
*/
function isPossibleType(
GraphQL\Type\Definition\AbstractType $abstractType,
GraphQL\Type\Definition\ObjectType $possibleType
): bool
/**
* Returns instance of directive by name
*
* [@api](https://github.com/api)
*/
function getDirective(string $name): GraphQL\Type\Definition\Directive
/**
* Validates schema.
*
* This operation requires full schema scan. Do not use in production environment.
*
* [@throws](https://github.com/throws) InvariantViolation
*
* [@api](https://github.com/api)
*/
function assertValid()
/**
* Validates schema.
*
* This operation requires full schema scan. Do not use in production environment.
*
* [@return](https://github.com/return) InvariantViolation[]|Error[]
*
* [@api](https://github.com/api)
*/
function validate()
Parses string containing GraphQL query or type definition to Abstract Syntax Tree.
Those magic functions allow partial parsing:
@method static DocumentNode document(Source|string $source, bool[] $options = []) @method static ExecutableDefinitionNode executableDefinition(Source|string $source, bool[] $options = []) @method static string operationType(Source|string $source, bool[] $options = []) @method static VariableDefinitionNode variableDefinition(Source|string $source, bool[] $options = []) @method static SelectionSetNode selectionSet(Source|string $source, bool[] $options = []) @method static FieldNode field(Source|string $source, bool[] $options = []) @method static NodeList<ArgumentNode> constArguments(Source|string $source, bool[] $options = []) @method static ArgumentNode constArgument(Source|string $source, bool[] $options = []) @method static FragmentDefinitionNode fragmentDefinition(Source|string $source, bool[] $options = []) @method static BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|NullValueNode|ObjectValueNode|StringValueNode|VariableNode valueLiteral(Source|string $source, bool[] $options = []) @method static StringValueNode stringLiteral(Source|string $source, bool[] $options = []) @method static BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|ObjectValueNode|StringValueNode|VariableNode variableValue(Source|string $source, bool[] $options = []) @method static ListValueNode constArray(Source|string $source, bool[] $options = []) @method static ObjectValueNode constObject(Source|string $source, bool[] $options = []) @method static ObjectFieldNode constObjectField(Source|string $source, bool[] $options = []) @method static NodeList<DirectiveNode> constDirectives(Source|string $source, bool[] $options = []) @method static DirectiveNode constDirective(Source|string $source, bool[] $options = []) @method static NamedTypeNode namedType(Source|string $source, bool[] $options = []) @method static StringValueNode|null description(Source|string $source, bool[] $options = []) @method static OperationTypeDefinitionNode operationTypeDefinition(Source|string $source, bool[] $options = []) @method static ObjectTypeDefinitionNode objectTypeDefinition(Source|string $source, bool[] $options = []) @method static NodeList<FieldDefinitionNode> fieldsDefinition(Source|string $source, bool[] $options = []) @method static NodeList<InputValueDefinitionNode> argumentsDefinition(Source|string $source, bool[] $options = []) @method static InterfaceTypeDefinitionNode interfaceTypeDefinition(Source|string $source, bool[] $options = []) @method static NamedTypeNode[] unionMemberTypes(Source|string $source, bool[] $options = []) @method static NodeList<EnumValueDefinitionNode> enumValuesDefinition(Source|string $source, bool[] $options = []) @method static InputObjectTypeDefinitionNode inputObjectTypeDefinition(Source|string $source, bool[] $options = []) @method static TypeExtensionNode typeExtension(Source|string $source, bool[] $options = []) @method static ScalarTypeExtensionNode scalarTypeExtension(Source|string $source, bool[] $options = []) @method static InterfaceTypeExtensionNode interfaceTypeExtension(Source|string $source, bool[] $options = []) @method static EnumTypeExtensionNode enumTypeExtension(Source|string $source, bool[] $options = []) @method static DirectiveDefinitionNode directiveDefinition(Source|string $source, bool[] $options = []) @method static DirectiveLocation directiveLocation(Source|string $source, bool[] $options = [])
Class Methods:
/**
* Given a GraphQL source, parses it into a `GraphQL\Language\AST\DocumentNode`.
* Throws `GraphQL\Error\SyntaxError` if a syntax error is encountered.
*
* Available options:
*
* noLocation: boolean,
* (By default, the parser creates AST nodes that know the location
* in the source that they correspond to. This configuration flag
* disables that behavior for performance or testing.)
*
* allowLegacySDLEmptyFields: boolean
* If enabled, the parser will parse empty fields sets in the Schema
* Definition Language. Otherwise, the parser will follow the current
* specification.
*
* This option is provided to ease adoption of the final SDL specification
* and will be removed in a future major release.
*
* allowLegacySDLImplementsInterfaces: boolean
* If enabled, the parser will parse implemented interfaces with no `&`
* character between each interface. Otherwise, the parser will follow the
* current specification.
*
* This option is provided to ease adoption of the final SDL specification
* and will be removed in a future major release.
*
* experimentalFragmentVariables: boolean,
* (If enabled, the parser will understand and parse variable definitions
* contained in a fragment definition. They'll be represented in the
* `variableDefinitions` field of the FragmentDefinitionNode.
*
* The syntax is identical to normal, query-defined variables. For example:
*
* fragment A($var: Boolean = false) on T {
* ...
* }
*
* Note: this feature is experimental and may change or be removed in the
* future.)
*
* [@param](https://github.com/param) Source|string $source
* [@param](https://github.com/param) bool[] $options
*
* [@return](https://github.com/return) DocumentNode
*
* [@throws](https://github.com/throws) SyntaxError
*
* [@api](https://github.com/api)
*/
static function parse($source, array $options = [])
/**
* Given a string containing a GraphQL value (ex. `[42]`), parse the AST for
* that value.
* Throws `GraphQL\Error\SyntaxError` if a syntax error is encountered.
*
* This is useful within tools that operate upon GraphQL Values directly and
* in isolation of complete GraphQL documents.
*
* Consider providing the results to the utility function: `GraphQL\Utils\AST::valueFromAST()`.
*
* [@param](https://github.com/param) Source|string $source
* [@param](https://github.com/param) bool[] $options
*
* [@return](https://github.com/return) BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|ObjectValueNode|StringValueNode|VariableNode
*
* [@api](https://github.com/api)
*/
static function parseValue($source, array $options = [])
/**
* Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for
* that type.
* Throws `GraphQL\Error\SyntaxError` if a syntax error is encountered.
*
* This is useful within tools that operate upon GraphQL Types directly and
* in isolation of complete GraphQL documents.
*
* Consider providing the results to the utility function: `GraphQL\Utils\AST::typeFromAST()`.
*
* [@param](https://github.com/param) Source|string $source
* [@param](https://github.com/param) bool[] $options
*
* [@return](https://github.com/return) ListTypeNode|NamedTypeNode|NonNullTypeNode
*
* [@api](https://github.com/api)
*/
static function parseType($source, array $options = [])
Prints AST to string. Capable of printing GraphQL queries and Type definition language. Useful for pretty-printing queries or printing back AST for logging, documentation, etc.
Usage example:
$query = 'query myQuery {someField}';
$ast = GraphQL\Language\Parser::parse($query);
$printed = GraphQL\Language\Printer::doPrint($ast);
Class Methods:
/**
* Prints AST to string. Capable of printing GraphQL queries and Type definition language.
*
* [@param](https://github.com/param) Node $ast
*
* [@return](https://github.com/return) string
*
* [@api](https://github.com/api)
*/
static function doPrint($ast)
Utility for efficient AST traversal and modification.
visit() will walk through an AST using a depth first traversal, calling
the visitor's enter function at each node in the traversal, and calling the
leave function after visiting that node and all of it's child nodes.
By returning different values from the enter and leave functions, the behavior of the visitor can be altered, including skipping over a sub-tree of the AST (by returning false), editing the AST by returning a value or null to remove the value, or to stop the whole traversal by returning BREAK.
When using visit() to edit an AST, the original AST will not be modified, and
a new version of the AST with the changes applied will be returned from the
visit function.
$editedAST = Visitor::visit($ast, [
'enter' => function ($node, $key, $parent, $path, $ancestors) {
// return
// null: no action
// Visitor::skipNode(): skip visiting this node
// Visitor::stop(): stop visiting altogether
// Visitor::removeNode...
How can I help you explore Laravel packages today?