Graphqlbundle: Relay connection configuration

Created on 15 Oct 2017  路  1Comment  路  Source: overblog/GraphQLBundle

| Q | A
| ---------------- | -----
| Bug report? | no
| Feature request? | no
| BC Break report? | no
| RFC? | yes
| Version/Branch | master

Can someone help with connection configuration, I want to configure following relay connection:

Query:
    type: object
    config:
        fields:
            allLinks:
                type: LinkConnection!
                argsBuilder: "Relay::Connection"
                resolve: '@=resolver("allLinks", [args])'
            viewer:
                type: Viewer!
                resolve: []
            node:
                builder: 'Relay::Node'
                builderConfig:
                    nodeInterfaceType: Node
                    idFetcher: '@=resolver("node_id_fetcher", [value])'

Node:
    type: relay-node
    config:
        resolveType: '@=resolver("node_type", [value])'

LinkConnection:
    type: relay-connection
    config:
        nodeType: Link

Now my query is:

{
  allLinks {
    edges {
      cursor
      node {
        __typename
        id
      }
    }
    pageInfo {
      hasPreviousPage
      startCursor
    }
  }
}

Should I prepare Connection object in allLinks resolver using paginator or there is better solution using type config and return plain array with entities from resolver?

Most helpful comment

My solution is

class LinkResolver
{
    /**
     * @var RegistryInterface
     */
    private $doctrine;

    /**
     * LinkResolver constructor.
     * @param RegistryInterface $doctrine
     */
    public function __construct(RegistryInterface $doctrine)
    {
        $this->doctrine = $doctrine;
    }

    public function getAll(Argument $args)
    {
        $linkRepository = $this->doctrine
            ->getManager()
            ->getRepository(Link::class);

        $paginator = new Paginator(
            function ($offset, $limit) use ($linkRepository, $args) {
                return $linkRepository->findByFilter($offset, $limit, $args);
            }
        );

        return $paginator->auto($args, $linkRepository->getTotal());
    }
}

>All comments

My solution is

class LinkResolver
{
    /**
     * @var RegistryInterface
     */
    private $doctrine;

    /**
     * LinkResolver constructor.
     * @param RegistryInterface $doctrine
     */
    public function __construct(RegistryInterface $doctrine)
    {
        $this->doctrine = $doctrine;
    }

    public function getAll(Argument $args)
    {
        $linkRepository = $this->doctrine
            ->getManager()
            ->getRepository(Link::class);

        $paginator = new Paginator(
            function ($offset, $limit) use ($linkRepository, $args) {
                return $linkRepository->findByFilter($offset, $limit, $args);
            }
        );

        return $paginator->auto($args, $linkRepository->getTotal());
    }
}
Was this page helpful?
0 / 5 - 0 ratings