1

I need to create a GET endpoint to return a resource which is fetched from another application via http client, not based on entity. The resource I fetched is an array:

[
    "id" => 1234
    "first_name" => ""
    "last_name" => ""
    "email" => ""
    "country" => 1
    "country_code" => 93
    "phone_number" => "3434343"
    "nationality" => "AF"
    "professional_industry" => "Health Care"
    "job_title" => "Medical Doctor - General Practitioner"
    "specialisation" => "No specialisation"
    "career_length_month" => 1
    "career_length_year" => 1
  ]

Then I need to query database to fetch some data to add to the resource array.

So I created it in src/Controller/MyController.php:

    /**
     * @Route("/api/v1/get-profile", name="get_profile")
     * @return JsonResponse
     */
    public function getMemberInfo(): JsonResponse
    {
        // step 1 : use http client to request data from another application
        // step 2 : query DB to fetch some data and add to data array
        return new JsonResponse($data, Response::HTTP_OK);
    }

But now, I would like my api return json api response format : https://jsonapi.org/.

With resource based on entity, it is supported completely by api-platform. I don't need to do much. I just adding "key" #[ApiResource] in entity class and config some things. Then I have a api with json api format, so easily:

{
    "data": {
        "id": "",
        "type": "",
        "attributes": {}
    }
}

But how about with resource not based on Entity? Is there any built-in feature of api-platform I could use, or I have to do a transformer by myself ?

2 Answers 2

1

You need override the OpenAPI specification how described in documentation: https://api-platform.com/docs/core/openapi/#overriding-the-openapi-specification

Then, in the new class, you must add the schema definition that you require, adding new PathItem instances, each PathItem require new Operation instances, etc.

Sorry for not paste examples, it require so many code. I'm working this way in a project, but it's not public for now.

I hope I put you in the way.

Sign up to request clarification or add additional context in comments.

Comments

0

The good practice is to create a custom data provider. This custom state provider that will be responsible to fetch data from the external source. Then, API-Platform will be able to use it like a regular entity.

3 Comments

Both broken links :/
Yes, documentation evolves. If there is no redirection, you have 404 errors. I have updated the link. The example in the demo has been deleted.
I know the reason is not your fault, hence no downvote :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.