Core: [question] Two APIs inside single app

Created on 24 Oct 2018  Â·  16Comments  Â·  Source: api-platform/core

What do you recommend in order to create API within two contexts using API Platform: first — publicly available one and second: used by microservices within application cluster? To simplify — I rely on DTOs and do not expose any entities directly.

question

Most helpful comment

You could decorate the Docker normalizer to filter the docs by prefix.

All 16 comments

Bump.

Using DTO looks good. You can also leverage the new Input/Output feature if you want to have a resource class but different input/output classes. see https://github.com/api-platform/core/pull/2235 for more informations!

You didn't understand: I want to have separated APIs — one public, one internal. How do you recommend to achieve this?

I don't think that it's possible yet without having two kernels. Anyway, you can have a single API, but using different paths per resources (some in /internal, and some in /publish for instance) using the path and the prefix options.

But how about swagger docs? And is this possible to have some common logic if two kernels are being used?

Just have 2 separate apps, but you can structure your code for reuse between the 2 apps.

Yes, but's it's too early for this pre-mature optimization and I'm looking for a solution how to keep these within one app (both APIs share Entities, DTOs). Maintaining two apps is more absorbing than single one (at least at this product stadium). Isn't this possible?

What about keeping them as a single API then? But using different prefixes as @dunglas has suggested.

But they will share Swagger docs what is the thing I don't want to.

You could decorate the Docker normalizer to filter the docs by prefix.

And then, how to access two swagger-based functionality — eg. API website and generate separated swagger.json|yml?

Same, decorate the documentation normalizer accordingly and allow only the prefixed data.

I know but how to access first and second one (mean how to distinguish which one I want to get/open at time)?

Made something like this: https://github.com/er1z/api-platform-two-apis

PoC that separates DTOs: internal and external.

CLI:

  • IS_INTERNAL=true bin/console api:swag:ex
  • bin/console api:swag:ex

API Docs:

  • header x-internal exists / query string with key is_internal any content <-> internal API
  • otherwise: external API

Based on decoration of provided services and conditional routes.

Please review and point how to do it better and what are pitfalls?

Bundled: https://github.com/er1z/multi-api-platform-bundle

My question, how to create unit-tests against such a decoration?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DenisVorobyov picture DenisVorobyov  Â·  3Comments

theshaunwalker picture theshaunwalker  Â·  3Comments

rockyweng picture rockyweng  Â·  3Comments

silverbackdan picture silverbackdan  Â·  3Comments

bendavies picture bendavies  Â·  3Comments