how to create custom object type (resource) api platform

37 Views Asked by At

I am setting up Bramble Graphql Federation for a project. Aim is to serve graphql API's of different projects via single API.

I have built a GraphQl API using API-Platform, but to make it work with Bramble I need to add following query type into the GraphQl schema:

type Service {
  name: String! # unique name for the service
  version: String! # any string
  schema: String! # the full schema for the service
}

type Query {
  service: Service!
}

which is described here Bramble requires this type to be exactly in this format, anything else (for example Service with additional id: ID! field which is added automatically by API-Platform) causes error on Bramble service.

I am not able find any documentation related to implementing this kind of custom object type/resource.

What I tried following resource configuration:

<?php

declare(strict_types=1);

namespace App\ApiResource;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GraphQl\Query;
use App\Resolver\BrambleServiceResolver;

#[ApiResource(
    shortName: 'service',
    types: [],
    operations: [
        // Although we dont need RestAPI endpoint, without this operation we get error: "Not able to retrieve identifiers."
        new Get(),
    ],
    graphQlOperations: [
        new Query(
            resolver: BrambleServiceResolver::class,
            args: [ // set args empty so that it does not require ID input
            ],
        ),
    ],
)]
class BrambleService
{
    public function __construct(
        public readonly string $version,
        public readonly string $schema,
        public readonly string $name = 'my-api',
    )
    {
    }
}

and with it I can API platform generates following schema:

type Query {

  service: service

  node(id: ID!): Node
}

type service implements Node {
  id: ID!
  version: String!
  schema: String!
  name: String!
}

used api-platform v3.2

0

There are 0 best solutions below