在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):folkloreinc/laravel-graphql开源软件地址(OpenSource Url):https://github.com/folkloreinc/laravel-graphql开源编程语言(OpenSource Language):PHP 99.4%开源软件介绍(OpenSource Introduction):Laravel GraphQLrebing/graphql-laravel or other Laravel GraphQL packagesThis package is no longuer maintained. Please useUse Facebook GraphQL with Laravel 5 or Lumen. It is based on the PHP implementation here. You can find more information about GraphQL in the GraphQL Introduction on the React blog or you can read the GraphQL specifications. This is a work in progress. This package is compatible with Eloquent model (or any other data source). See the example below. feature/relay branch.To use laravel-graphql with Relay, check theInstallationVersion 1.0 is released. If you are upgrading from older version, you can check Upgrade to 1.0. Dependencies:1- Require the package via Composer in your {
"require": {
"folklore/graphql": "~1.0.0"
}
} 2- Run Composer to install or update the new requirement. $ composer install or $ composer update Laravel >= 5.5.x1- Publish the configuration file $ php artisan vendor:publish --provider="Folklore\GraphQL\ServiceProvider" 2- Review the configuration file
Laravel <= 5.4.x1- Add the service provider to your Folklore\GraphQL\ServiceProvider::class, 2- Add the facade to your 'GraphQL' => Folklore\GraphQL\Support\Facades\GraphQL::class, 3- Publish the configuration file $ php artisan vendor:publish --provider="Folklore\GraphQL\ServiceProvider" 4- Review the configuration file
Lumen1- Load the service provider in $app->register(Folklore\GraphQL\LumenServiceProvider::class); 2- For using the facade you have to uncomment the line After uncommenting this line you have the $app->withFacades(); 3- Publish the configuration file $ php artisan graphql:publish 4- Load configuration file in Important: this command needs to be executed before the registration of the service provider $app->configure('graphql');
...
$app->register(Folklore\GraphQL\LumenServiceProvider::class) 5- Review the configuration file
DocumentationUsageAdvanced UsageSchemasStarting from version 1.0, you can define multiple schemas. Having multiple schemas can be useful if, for example, you want an endpoint that is public and another one that needs authentication. You can define multiple schemas in the config: 'schema' => 'default',
'schemas' => [
'default' => [
'query' => [
//'users' => 'App\GraphQL\Query\UsersQuery'
],
'mutation' => [
//'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation'
]
],
'secret' => [
'query' => [
//'users' => 'App\GraphQL\Query\UsersQuery'
],
'mutation' => [
//'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation'
]
]
] Or you can add schema using the facade: GraphQL::addSchema('secret', [
'query' => [
'users' => 'App\GraphQL\Query\UsersQuery'
],
'mutation' => [
'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation'
]
]); Afterwards, you can build the schema using the facade: // 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' => 'App\GraphQL\Query\UsersQuery'
],
'mutation' => [
//'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation'
]
]); Or you can request the endpoint for a specific schema
Creating a queryFirst you need to create a type. 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);
}
} Add the type to the 'types' => [
'User' => 'App\GraphQL\Type\UserType'
] You could also add the type with the GraphQL::addType('App\GraphQL\Type\UserType', 'User'); Then you need to define a query that returns this type (or a list). You can also specify arguments that you can use in the resolve method. 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();
}
}
} Add the query to the 'schemas' => [
'default' => [
'query' => [
'users' => 'App\GraphQL\Query\UsersQuery'
],
// ...
]
] And that's it. You should be able to query GraphQL with a request to the url
For example, if you use homestead:
Creating a mutationA mutation is like any other query, it accepts arguments (which will be used to do the mutation) and return an object of a certain type. For example a mutation to update the password of a user. First you need to define the Mutation. 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;
}
} As you can see in the You then add the mutation to the 'schema' => [
'default' => [
'mutation' => [
'updateUserPassword' => 'App\GraphQL\Mutation\UpdateUserPasswordMutation'
],
// ...
]
] You should then be able to use the following query on your endpoint to do the mutation.
if you use homestead:
Adding validation to mutationIt is possible to add validation rules to mutation. It uses the laravel When creating a mutation, you can add a method to define the validation rules that apply by doing the following: 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;
}
} Alternatively you can define rules with each args 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']
]
];
}
//...
} When you execute a mutation, it will returns the validation errors. Since GraphQL specifications define a certain format for errors, the validation errors messages are added to the error object as a extra {
"data": {
"updateUserEmail": null
},
"errors": [
{
"message": "validation",
"locations": [
{
"line": 1,
"column": 20
}
],
"validation": {
"email": [
"The email is invalid."
]
}
}
]
} |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论