-City
-Event (que se realiza en una city)
php artisan make:model City -m
php artisan make:model Event -m
editar el archivo de migracion de City:
public function up()
{
Schema::create('cities', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 50);
$table->timestamps();
});
}
en el archivo de migracion de Event:
public function up()
{
Schema::create('events', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title', 50);
$table->string('description', 200);
$table->unsignedBigInteger('city_id');
$table->timestamps();
$table->foreign('city_id')->references('id')->on('cities');
});
}
en el modelo de City, lo podemos dejar como está.
al modelo de Event le agregamos la funcion de relacion con city:
public function city(){
return $this->belongsTo(City::class);
}
crear el Type de City:
php artisan make:graphql:type CityType
use Rebing\GraphQL\Support\Type as GraphQLType;
use App\City;
use GraphQL\Type\Definition\Type;
class CityType extends GraphQLType
{
protected $attributes = [
'name' => 'city',
'description' => 'A type',
'model' => City::class
];
public function fields()
{
return [
'id' => [
'type' => Type::nonNull(Type::int()),
'description' => 'Id de la city',
],
'name' => [
'type' => Type::nonNull(Type::string()),
'description' => 'nombre de la city',
],
];
}
}
creamos la query para City:
php artisan make:graphql:query CitiesQuery
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Rebing\GraphQL\Support\SelectFields;
use Rebing\GraphQL\Support\Query;
use GraphQL;
use App\City;
class CitiesQuery extends Query
{
protected $attributes = [
'name' => 'cities',
'description' => 'A query'
];
public function type()
{
return Type::listOf(GraphQL::type('city'));
}
public function args()
{
return [
'id' => [
'name' => 'id',
'type' => Type::int()
],
];
}
public function resolve($root, $args, SelectFields $fields, ResolveInfo $info)
{
$select = $fields->getSelect();
$with = $fields->getRelations();
if(isset($args['id']))
{
return City::where('id','=',$args['id'])->get();
}
$cities = City::with($with)->select($select)->get();
return $cities;
}
}
del mismo modo, creamos el type de Event:
php artisan make:graphql:type EventType
use Rebing\GraphQL\Support\Type as GraphQLType;
use App\Event;
use GraphQL\Type\Definition\Type;
use GraphQL;
class EventType extends GraphQLType
{
protected $attributes = [
'name' => 'event',
'description' => 'A type',
'model' => Event::class
];
public function fields()
{
return [
'id' => [
'type' => Type::nonNull(Type::int()),
'description' => 'Id del Evento',
],
'title' => [
'type' => Type::string(),
'description' => 'title del Evento',
],
'description' => [
'type' => Type::string(),
'description' => 'description del Evento',
],
'city' => [
'type' => GraphQL::type('city'),
'description' => 'ciudad del Evento',
]
];
}
}
creamos la query de Event:
php artisan make:graphql:query EventsQuery
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Rebing\GraphQL\Support\SelectFields;
use Rebing\GraphQL\Support\Query;
use App\Event;
use GraphQL;
class EventsQuery extends Query
{
protected $attributes = [
'name' => 'events',
'description' => 'A query'
];
public function type()
{
return Type::listOf(GraphQL::type('event'));
}
public function args()
{
return [
'id' => [
'name' => 'id',
'type' => Type::int()
],
];
}
public function resolve($root, $args, SelectFields $fields, ResolveInfo $info)
{
$select = $fields->getSelect();
$with = $fields->getRelations();
if(isset($args['id']))
{
return Event::where('id','=',$args['id'])->get();
}
$events = Event::with($with)->select($select)->get();
return $events;
}
}
registramos los types y las querys,
en el archivo graphql.php:
'schemas' => [
'default' => [
'query' => [
'cities' => App\GraphQL\Query\CitiesQuery::class,
'events' => App\GraphQL\Query\EventsQuery::class
]
],
],
'types' => [
'city' => App\GraphQL\Type\CityType::class,
'event' => App\GraphQL\Type\EventType::class
],
probando la api:
{ events{ title city{ name } } }
resultado:
{ "data": { "events": [ { "title": "Feria de tecnologia", "city": { "name": "Lima" } }, { "title": "Muestra regional de material radiactivo", "city": { "name": "Tokio" } }, { "title": "Encuentro de emprendedores naturistas", "city": { "name": "Buenos Aires" } } ] } }
No hay comentarios:
Publicar un comentario