Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rest_api: Enable resolve in query parameters #1978

Open
jlopezpena opened this issue Oct 22, 2024 · 2 comments
Open

rest_api: Enable resolve in query parameters #1978

jlopezpena opened this issue Oct 22, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@jlopezpena
Copy link

jlopezpena commented Oct 22, 2024

Feature description

For REST APIs, it should be possible to dynamically populate query parameters based on values obtained from an existing resource.

This can be currently done only if the resolved parameter is part of the path, but not if it is used inside the query.

Eg, this works in a source resource definition:

{
    "name": "seasons",
    "endpoint": {
        "path": "competitions/{competition_id}/seasons",
        "params": {
            "competition_id": {
                "type": "resolve",
                "resource": "competitions",
                "field": "id",
            }
        }
    },
    "include_from_parent": ["id"],
}

but this doesn't:

{
    "name": "seasons",
    "endpoint": {
        "path": "competitions/seasons/?competition_id={competition_id}",
        "params": {
            "competition_id": {
                "type": "resolve",
                "resource": "competitions",
                "field": "id",
            }
        }
    },
    "include_from_parent": ["id"],
}

This forces creating a resource or transformer outside of the declarative API, using @dlt.transformer or @dlt.resource decorators, and makes for overall messy code

Are you a dlt user?

I am scoping it for our company's use case

Use case

It is a very common pattern to find REST APIs where the id's of documents coming from a query are used in subsequent queries. Sometimes the API defines this as a path parameter, and sometimes as a query parameter. This is just an implementation detail that should be abstracted away in dlt.

Proposed solution

The resolve mechanism should also work for query parameters. An ideal solution would work either with explicity inclusion of the query parameter in the path (as in the example above) or (even better!) in a declarative format, with something like this:

{
    "name": "seasons",
    "endpoint": {
        "path": "competitions/seasons/",
        "params": {
            "competition_id": {
                "type": "resolve",
                "resource": "competitions",
                "field": "id",
                "location": "query"  # <- This field would ensure the resolved parameter gets injected in the query
            }
        }
    },
    "include_from_parent": ["id"],
}

Related issues

None found

@burnash burnash self-assigned this Oct 23, 2024
@burnash burnash added the enhancement New feature or request label Oct 23, 2024
@joscha
Copy link
Contributor

joscha commented Nov 21, 2024

Use case

We have a similar use-case:

The Affinity CRM has three main entities: Persons, Companies and Opportunities.
The CRM allows to attach notes to each of these entities. They are always connected to at least one to many of each type of these entities, but never to different types. The way the API models this is described here:
Screenshot 2024-11-21 at 10 05 11 am

Basically it's three different query parameters: person_id, organization_id and opportunity_id. You can pass none, then you get all notes or you can pass exactly one of the query parameters, then you get the notes related to that entity.
Now I want to fetch all persons and augment each person with their notes, same for organizations and opportunities.
For that I need to resolve a query parameter to another entity in my resources.

This forces creating a resource or transformer outside of the declarative API, using @dlt.transformer or @dlt.resource decorators, and makes for overall messy code

@jlopezpena do you have an example for this by any chance?

@burnash burnash changed the title Enable resolve in query parameters rest_api: Enable resolve in query parameters Dec 3, 2024
@francescomucio
Copy link
Contributor

Another +1 for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

4 participants