Download the PHP package hsd-tech/graphql without Composer
On this page you can find all versions of the php package hsd-tech/graphql. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hsd-tech/graphql
More information about hsd-tech/graphql
Files in hsd-tech/graphql
Package graphql
Short Description A PHP7 implementation of the GraphQL specifications. Fork
License MIT
Informations about the package graphql
GraphQL
This is a PHP implementation of the GraphQL specification based on the JavaScript reference implementation.
Related projects
Requirements
- PHP version >= 7.1
- ext-mbstring
Table of contents
- Installation
- Example
- Creating a schema
- Resolver registry
- Resolver middleware
- Execution
- Queries
- Resolvers
- The N+1 problem
- Variables
- Context
- Scalars
- Custom scalars
- Advanced usage
- Integration
- Laravel
Installation
Run the following command to install the package through Composer:
Example
Here is a simple example that demonstrates how to build an executable schema from a GraphQL schema file that contains the Schema Definition Language (SDL) for a Star Wars-themed schema (for the schema definition itself, see below). In this example we use that SDL to build an executable schema and use it to query for the name of the hero. The result of that query is an associative array with a structure that resembles the query we ran.
The script above produces the following output:
The GraphQL schema file used in this example contains the following:
Creating a schema
In order to execute queries against your GraphQL API, you first need to define the structure of your API. This is done
by creating a schema. There are two ways to do this, you can either do it using SDL or you can do it programmatically.
However, we strongly encourage you to use SDL, because it is easier to work with. To make an executable schema from
SDL you need to call the buildSchema
function.
The buildSchema
function takes three arguments:
$source
The schema definition (SDL) as aSource
instance$resolverRegistry
An associative array or aResolverRegistry
instance that contains all resolvers$options
The options for building the schema, which also includes custom types and directives
To create the Source
instance you can use the provided FileSourceBuilder
or MultiFileSourceBuilder
classes.
Resolver registry
The resolver registry is essentially a flat map with the type names as its keys and their corresponding resolver instances as its values. For smaller projects you can use an associative array and lambda functions to define your resolver registry. However, in larger projects we suggest that you implement your own resolvers instead. You can read more about resolvers under the Resolvers section.
Associative array example:
Resolver class example:
Resolver middleware
If you find yourself writing the same logic in multiple resolvers you should consider using middleware. Resolver middleware allow you to efficiently manage functionality across multiple resolvers.
Before middleware example:
After middleware example:
Resolver middleware can be useful for a number of things; such as logging, input sanitization, performance measurement, authorization and caching.
If you want to learn more about schemas you can refer to the specification.
Execution
Queries
To execute a query against your schema you need to call the graphql
function and pass it your schema and the query
you wish to execute. You can also run mutations and subscriptions by changing your query.
If you want to learn more about queries you can refer to the specification.
Resolvers
Each type in a schema has a resolver associated with it that allows for resolving the actual value. However, most
types do not need a custom resolver, because they can be resolved using the default resolver. Usually these resolvers
are lambda functions, but you can also define your own resolvers by extending AbstractTypeResolver
or AbstractFieldResolver
. Alternatively you can also implement the ResolverInterface
directly.
A resolver function receives four arguments:
$rootValue
The parent object, which can also benull
in some cases$arguments
The arguments provided to the field in the query$context
A value that is passed to every resolver that can hold important contextual information$info
A value which holds field-specific information relevant to the current query
Lambda function example:
Type resolver example:
Field resolver example:
The N+1 problem
The resolver function can return a value, a promise or an array of promises. This resolver function below illustrates how to use promise to solve the N+1 problem, the full example can be found in this test case.
Variables
You can pass in variables when executing a query by passing them to the graphql
function.
Context
In case you need to pass in some important contextual information to your queries you can use the $contextValues
argument on graphql
to do so. This data will be passed to all of your resolvers as the $context
argument.
Scalars
The leaf nodes in a schema are called scalars and each scalar resolves to some concrete data. The built-in, or specified scalars in GraphQL are the following:
- Boolean
- Float
- Int
- ID
- String
Custom scalars
In addition to the specified scalars you can also define your own custom scalars and let your schema know about
them by passing them to the buildSchema
function as part of its $options
argument.
Custom Date scalar type example:
Every scalar has to be coerced, which is done by three different functions. The serialize
function converts a
PHP value into the corresponding output value. TheparseValue
function converts a variable input value into the
corresponding PHP value and the parseLiteral
function converts an AST literal into the corresponding PHP value.
Advanced usage
If you are looking for something that isn't yet covered by this documentation your best bet is to take a look at the tests in this project. You'll be surprised how many examples you'll find there.
Integration
Laravel
Here is an example that demonstrates how you can use this library in your Laravel project. You need an application service to expose this library to your application, a service provider to register that service, a controller and a route for handling the GraphQL POST requests.
app/GraphQL/GraphQLService.php
app/GraphQL/GraphQLServiceProvider.php
app/GraphQL/GraphQLController.php
routes/api.php
Contributors
This project exists thanks to all the people who contribute. Contribute.
Backers
Thank you to all our backers! 🙏 Become a backer
Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. Become a sponsor
License
See LICENCE.
All versions of graphql with dependencies
ext-mbstring Version *
league/container Version ^3.2
react/promise Version ^2.5