Symfony: Primeros pasos con overblog/GraphQLBundle

Aprende a desarrollar una API GraphQL con Symfony y GraphQLBndle

Image for post
Image for post

Instalando overBlog/GraphQLBundle

composer require overblog/graphql-bundle
composer require --dev overblog/graphiql-bundle
{"error":{"code":500,"message":"Internal Server Error",
"exception":[{"message":"Unknown type with alias \"Query\"
(verified service tag)","class":"Overblog\\
GraphQLBundle\\Resolver\\UnresolvableException","trace"...
# config/routes/graphql.yamloverblog_graphql_endpoint:    resource: "@OverblogGraphQLBundle/Resources/config/routing/graphql.yml"
Image for post
Image for post
overblog_graphql_endpoint:    resource: "@OverblogGraphQLBundle/Resources/config/routing/graphql.yml"    prefix: graphql

Configurando nuestro primer esquema

Article:
type: object
config:
description: "An article"
fields:
id:
type: "Int!"
description: "The ID of the article."
title:
type: "String"
description: "Title of the article"
subtitle:
type: "String"
description: "Subtitle of the article"
Query:
type: object
config:
description: ''
fields:
allArticles:
type: '[Article]'
{
allArticles {id, title}
}
{
"data": {
"allArticles": null
}
}

Creando nuestro primer Resolver

Query:
type: object
config:
description: ''
fields:
allArticles:
type: '[Article]'
resolve: "@=resolver('all_articles', [args])"
Image for post
Image for post
resolve: "@=resolver('all_articles', [args])"
{
allArticles {id, title}
}
Image for post
Image for post
Image for post
Image for post
{
articleById(id: 1) {id, title}
}

Reusando tipos

Image for post
Image for post
Image for post
Image for post
public function findAll(Argument $args)
{
$articles = $this->em->getRepository(Article::class)
->findAll();
return ['articles' => $articles];
}
{
allArticles { article { id } }
}

Conclusión

¿Quieres recibir más artículos como este?

Entre paseo y paseo con Simba desarrollo en Symfony y React

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store