将 Facebook GraphQL 与 Laravel 5 或 Lumen 结合使用。它是基于 PHP 实现的。您可以在 React 博客上的 GraphQL 简介中找到有关 GraphQL 的更多信息,也可以阅读 GraphQL 规范。这是一项正在进行的工作。
该包与 Eloquent 模型(或任何其他数据源)兼容。请参阅下面的示例。
1.0版本发布。如果您要从旧版本升级,可以选中升级到 1.0。
1-在您的composer.json
中通过Composer 需要该包。
{
"require" : {
"folklore/graphql" : " ~1.0.0 "
}
}
2-运行 Composer 以安装或更新新要求。
$ composer install
或者
$ composer update
1-发布配置文件
$ php artisan vendor:publish --provider= " FolkloreGraphQLServiceProvider "
2-查看配置文件
config/graphql.php
1-将服务提供者添加到您的config/app.php
文件中
Folklore GraphQL ServiceProvider::class,
2-将外观添加到您的config/app.php
文件中
' GraphQL ' => Folklore GraphQL Support Facades GraphQL::class,
3-发布配置文件
$ php artisan vendor:publish --provider= " FolkloreGraphQLServiceProvider "
4-查看配置文件
config/graphql.php
1-在bootstrap/app.php
中加载服务提供者
$ app -> register ( Folklore GraphQL LumenServiceProvider::class);
2-要使用外观,您必须取消注释行$app->withFacades();
在bootstrap/app.php
中
取消注释此行后,您将启用GraphQL
外观
$ app -> withFacades ();
3-发布配置文件
$ php artisan graphql:publish
4-在bootstrap/app.php
中加载配置文件
重要提示:该命令需要在服务提供商注册之前执行
$ app -> configure ( ' graphql ' );
. . .
$ app -> register ( Folklore GraphQL LumenServiceProvider::class)
5-查看配置文件
config/graphql.php
从1.0版本开始,您可以定义多个模式。例如,如果您希望一个端点是公共的,而另一个端点需要身份验证,则拥有多个模式可能会很有用。
您可以在配置中定义多个模式:
' schema ' => ' default ' ,
' schemas ' => [
' default ' => [
' query ' => [
// 'users' = > 'AppGraphQLQueryUsersQuery'
],
' mutation ' => [
// 'updateUserEmail' = > 'AppGraphQLQueryUpdateUserEmailMutation'
]
],
' secret ' => [
' query ' => [
// 'users' = > 'AppGraphQLQueryUsersQuery'
],
' mutation ' => [
// 'updateUserEmail' = > 'AppGraphQLQueryUpdateUserEmailMutation'
]
]
]
或者您可以使用外观添加模式:
GraphQL:: addSchema ( ' secret ' , [
' query ' => [
' users ' => ' AppGraphQLQueryUsersQuery '
],
' mutation ' => [
' updateUserEmail ' => ' AppGraphQLQueryUpdateUserEmailMutation '
]
]);
之后,您可以使用外观构建模式:
// Will return the default schema defined by 'schema' in the config
$ schema = GraphQL:: schema ();
// Will return the 'secret' schema
$ schema = GraphQL:: schema ( ' secret ' );
// Will build a new schema
$ schema = GraphQL:: schema ([
' query ' => [
// 'users' = > 'AppGraphQLQueryUsersQuery'
],
' mutation ' => [
// 'updateUserEmail' = > 'AppGraphQLQueryUpdateUserEmailMutation'
]
]);
或者您可以请求特定架构的端点
// Default schema
http://homestead.app/graphql?query=query+FetchUsers{users{id,email}}
// Secret schema
http://homestead.app/graphql/secret?query=query+FetchUsers{users{id,email}}
首先您需要创建一个类型。
namespace App GraphQL Type ;
use GraphQL Type Definition Type ;
use Folklore GraphQL Support Type as GraphQLType ;
class UserType extends GraphQLType
{
protected $ attributes = [
' name ' => ' User ' ,
' description ' => ' A user '
];
/ *
* Uncomment following line to make the type input object .
* http : // graphql . org / learn / schema / #input-types
* /
// protected $ inputObject = true ;
public function fields ()
{
return [
' id ' => [
' type ' => Type:: nonNull (Type:: string ()),
' description ' => ' The id of the user '
],
' email ' => [
' type ' => Type:: string (),
' description ' => ' The email of user '
]
];
}
// If you want to resolve the field yourself , you can declare a method
// with the following format resolve [ FIELD_NAME ] Field ()
protected function resolveEmailField ( $ root , $ args )
{
return strtolower ( $ root -> email );
}
}
将类型添加到config/graphql.php
配置文件中
' types ' => [
' User ' => ' AppGraphQLTypeUserType '
]
您还可以使用GraphQL
Facade 添加类型,例如在服务提供商中。
GraphQL:: addType ( ' AppGraphQLTypeUserType ' , ' User ' );
然后您需要定义一个返回该类型(或列表)的查询。您还可以指定可在解析方法中使用的参数。
namespace App GraphQL Query ;
use GraphQL ;
use GraphQL Type Definition Type ;
use Folklore GraphQL Support Query ;
use App User ;
class UsersQuery extends Query
{
protected $ attributes = [
' name ' => ' users '
];
public function type ()
{
return Type:: listOf (GraphQL:: type ( ' User ' ));
}
public function args ()
{
return [
' id ' => [ ' name ' => ' id ' , ' type ' => Type:: string ()],
' email ' => [ ' name ' => ' email ' , ' type ' => Type:: string ()]
];
}
public function resolve ( $ root , $ args )
{
if ( isset ( $ args [ ' id ' ])) {
return User:: where ( ' id ' , $ args [ ' id ' ])-> get ();
} else if ( isset ( $ args [ ' email ' ])) {
return User:: where ( ' email ' , $ args [ ' email ' ])-> get ();
} else {
return User:: all ();
}
}
}
将查询添加到config/graphql.php
配置文件中
' schemas ' => [
' default ' => [
' query ' => [
' users ' => ' AppGraphQLQueryUsersQuery '
],
// ...
]
]
就是这样。您应该能够通过对 url /graphql
(或您在配置中选择的任何内容)的请求来查询 GraphQL。尝试使用以下query
输入发出 GET 请求
query FetchUsers {
users {
id
email
}
}
例如,如果您使用宅基地:
http://homestead.app/graphql?query=query+FetchUsers{users{id,email}}
突变就像任何其他查询一样,它接受参数(将用于执行突变)并返回特定类型的对象。
例如更新用户密码的突变。首先您需要定义突变。
namespace App GraphQL Mutation ;
use GraphQL ;
use GraphQL Type Definition Type ;
use Folklore GraphQL Support Mutation ;
use App User ;
class UpdateUserPasswordMutation extends Mutation
{
protected $ attributes = [
' name ' => ' updateUserPassword '
];
public function type ()
{
return GraphQL:: type ( ' User ' );
}
public function args ()
{
return [
' id ' => [ ' name ' => ' id ' , ' type ' => Type:: nonNull (Type:: string ())],
' password ' => [ ' name ' => ' password ' , ' type ' => Type:: nonNull (Type:: string ())]
];
}
public function resolve ( $ root , $ args )
{
$ user = User:: find ( $ args [ ' id ' ]);
if (! $ user ) {
return null ;
}
$ user -> password = bcrypt ( $ args [ ' password ' ]);
$ user -> save ();
return $ user ;
}
}
正如您在resolve
方法中看到的,您使用参数来更新模型并返回它。
然后将突变添加到config/graphql.php
配置文件中
' schema ' => [
' default ' => [
' mutation ' => [
' updateUserPassword ' => ' AppGraphQLMutationUpdateUserPasswordMutation '
],
// ...
]
]
然后,您应该能够在端点上使用以下查询来进行更改。
mutation users {
updateUserPassword(id: "1", password: "newpassword") {
id
email
}
}
如果您使用宅基地:
http://homestead.app/graphql?query=mutation+users{updateUserPassword(id: "1", password: "newpassword"){id,email}}
可以向突变添加验证规则。它使用 laravel Validator
对args
执行验证。
创建突变时,您可以添加一个方法来定义适用的验证规则,方法如下:
namespace App GraphQL Mutation ;
use GraphQL ;
use GraphQL Type Definition Type ;
use Folklore GraphQL Support Mutation ;
use App User ;
class UpdateUserEmailMutation extends Mutation
{
protected $ attributes = [
' name ' => ' UpdateUserEmail '
];
public function type ()
{
return GraphQL:: type ( ' User ' );
}
public function args ()
{
return [
' id ' => [ ' name ' => ' id ' , ' type ' => Type:: string ()],
' email ' => [ ' name ' => ' email ' , ' type ' => Type:: string ()]
];
}
public function rules ()
{
return [
' id ' => [ ' required ' ],
' email ' => [ ' required ' , ' email ' ]
];
}
public function resolve ( $ root , $ args )
{
$ user = User:: find ( $ args [ ' id ' ]);
if (! $ user ) {
return null ;
}
$ user -> email = $ args [ ' email ' ];
$ user -> save ();
return $ user ;
}
}
或者,您可以使用每个参数定义规则
class UpdateUserEmailMutation extends Mutation
{
// ...
public function args ()
{
return [
' id ' => [
' name ' => ' id ' ,
' type ' => Type:: string (),
' rules ' => [ ' required ' ]
],
' email ' => [
' name ' => ' email ' ,
' type ' => Type:: string (),
' rules ' => [ ' required ' , ' email ' ]
]
];
}
// ...
}
当您执行突变时,它将返回验证错误。由于 GraphQL 规范定义了错误的特定格式,因此验证错误消息将作为额外的validation
属性添加到错误对象中。要查找验证错误,您应该检查message
是否等于'validation'
,然后validation
属性将包含 Laravel 验证器返回的正常错误消息。
{
"data" : {
"updateUserEmail" : null
},
"errors" : [
{
"message" : " validation " ,
"locations" : [
{
"line" : 1 ,
"column" : 20
}
],
"validation" : {
"email" : [
" The email is invalid. "
]
}
}
]
}