Download the PHP package tsingsun/yii2-graphql without Composer
On this page you can find all versions of the php package tsingsun/yii2-graphql. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tsingsun/yii2-graphql
More information about tsingsun/yii2-graphql
Files in tsingsun/yii2-graphql
Package yii2-graphql
Short Description facebook graphql server side for yii2 php framework
License BSD-3-Clause
Informations about the package yii2-graphql
yii-graphql
Using Facebook GraphQL PHP server implementation. Extends graphql-php to apply to YII2.
Chinese document
Features
- Configuration includes simplifying the definition of standard graphql protocols.
- Based on the full name defined by the type, implementing on-demand loading and lazy loading, and no need to define all type definitions into the system at load.
- Mutation input validation support.
- Provide controller integration and authorization support.
Install
Using composer
Type
The type system is the core of GraphQL, which is embodied in GraphQLType
. By deconstructing the GraphQL protocol and using the graph-php library to achieve fine-grained control of all elements, it is convenient to extend the class according to its own needs
The main elements of GraphQLType
The following elements can be declared in the $attributes
property of the class, or as a method, unless stated otherwise. This also applies to all elements after this.
Element | Type | Description |
---|---|---|
name |
string | Required Each type needs to be named, with unique names preferred to resolve potential conflicts. The property needs to be defined in the $attributes property. |
description |
string | A description of the type and its use. The property needs to be defined in the $attributes property. |
fields |
array | Required The included field content is represented by the fields () method. |
resolveField |
callback | function($value, $args, $context, GraphQL\Type\Definition\ResolveInfo $info) For the interpretation of a field. For example: the fields definition of the user property, the corresponding method is resolveUserField() , and $value is the passed type instance defined by type . |
Query
GraphQLQuery
and GraphQLMutation
inherit GraphQLField
. The element structure is consistent, and if you would like a reusable Field
, you can inherit it.
Each query of Graphql
needs to correspond to a GraphQLQuery
object
The main elements of GraphQLField
Element | Type | Description |
---|---|---|
type |
ObjectType | For the corresponding query type. The single type is specified by GraphQL::type , and a list by Type::listOf(GraphQL::type) . |
args |
array | The available query parameters, each of which is defined by Field . |
resolve |
callback | function($value, $args, $context, GraphQL\Type\Definition\ResolveInfo $info) $value is the root data, $args is the query parameters, $context is the yii\web\Application object, and $info resolves the object for the query. The root object is handled in this method. |
Mutation
Definition is similar to GraphQLQuery
, please refer to the above.
Simplified Field Definition
Simplifies the declarations of Field
, removing the need to defined as an array with the type key.
Standard Definition
Simplified Definition
Yii Implementation
General configuration
JsonParser configuration required
Module support
Can easily be implemented with yii\graphql\GraphQLModuleTrait
. The trait is responsible for initialization.
In your application configuration file:
Use the controller to receive requests by using yii\graphql\GraphQLAction
Component Support
also you can include the trait with your own components,then initialization yourself.
Input validation
Validation rules are supported. In addition to graphql based validation, you can also use Yii Model validation, which is currently used for the validation of input parameters. The rules method is added directly to the mutation definition.
Authorization verification
Since graphql queries can be combined, such as when a query merges two query, and the two query have different authorization constraints, custom authentication is required. I refer to this query as "graphql actions"; when all graphql actions conditions are configured, it passes the authorization check.
Authenticate
In the behavior method of controller, the authorization method is set as follows
If you want to support IntrospectionQuery authorization, the corresponding graphql action is __schema
Authorization
If the user has passed authentication, you may want to check the access for the resource. You can use GraphqlAction
's checkAccess
method
in the controller. It will check all graphql actions.
Demo
Creating queries based on graphql protocols
Each query corresponds to a GraphQLQuery file.
Define type files based on query protocols
Query instance
Exception Handling
You can config the error formater for graph. The default handle uses yii\graphql\ErrorFormatter
,
which optimizes the processing of Model validation results.
Future
ActiveRecord
tool for generating query and mutation class.- Some of the special syntax for graphql, such as
@Directives
, has not been tested