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

Fields issue on preload entity #89

Open
BenWaNH opened this issue Apr 29, 2022 · 3 comments
Open

Fields issue on preload entity #89

BenWaNH opened this issue Apr 29, 2022 · 3 comments

Comments

@BenWaNH
Copy link

BenWaNH commented Apr 29, 2022

Hi!
I'm playing with Vulcain but i'm facing to an issue...
I've 2 entities in an Api Platform project (greeting and author, author have many greetings, for relationship)

If i fetch greetings endpoint:

window.fetch('https://localhost/greetings', {
        headers: {
            preload: '"/hydra:member/*/author"',
            fields: '"/hydra:member/*/@id", "/hydra:member/*/name", "/hydra:member/*/author/@id"'
        }
    }).then(() => window.fetch('https://localhost/authors/1'))

Of course authors/1 exists and it linked to all my greetings for more simplicity.

So... In this case, with fields contains "/hydra:member/*/authors/@id" (it the same with firstname or anyother property) push doesn't work
image

But if instead "/hydra:member/*/authors/@id", i've "/hydra:member/*/authors" (without /@id) push works (but i get full object after, of course)
image

What am i missing....? Documentation seems clear about, i think this should work...

@dunglas
Copy link
Owner

dunglas commented Apr 29, 2022

Could you paste the JSON documents you request, please?

@BenWaNH
Copy link
Author

BenWaNH commented May 2, 2022

Hi @dunglas
Thanks for your time!
This is a screen of my output greetings
image
And the author
image

I tried more since my last post, it seems to work if Fields header match between author et greetings, what i mean, if for my get of authors/1 i've this:

.then(() => window.fetch('https://localhost/authors/1', {
        headers: {
            fields: '"/@id"'
        }
    })

I see this with query parameters example, so i thinks i should reporter "/hydra:member/*/author/@id" to /@id when i fetch him. Isn't it? So, why not, it works!

However, I don't know how to make work with a new level after (in my exemple, authors have comments)
So, i tried:

window.fetch('https://localhost/greetings?'/*+encodeURI('fields="/hydra:member/!*!/@id"&fields="/hydra:member/!*!/name"&fields="/hydra:member/!*!/author/firstName"')*/, {
        headers: {
            preload: '"/hydra:member/*/author/comments/*"',
            fields: '"/hydra:member/*/@id", "/hydra:member/*/name", "/hydra:member/*/author/@id", "/hydra:member/*/author/firstName", "/hydra:member/*/author/comments/*/@id"'
        }
    }).then(() => window.fetch('https://localhost/authors/1', {
        headers: {
            fields: '"/@id", "/firstName"'
        }
    }).then(() => window.fetch('https://localhost/comments/1', {
        headers: {
            fields: '"/@id"'
        }
    }))
    )

Good fields are displayed
image
image
but, no push...
image
I tried many case, but either i've good fields or push but never both :'(

I saw this syntax in your demo symfo repo:

const conferences = await fetchJson('/conferences', { headers: { Preload: "/hydra:member/*/@id/sessions/*/feedback/*" }});

I don't understand this syntax with the @id before sessions, but when i tried, all my next calls are push but without fields....

Do you have a full example somewhere with multi level and fields headers?

Thanks

@BenWaNH
Copy link
Author

BenWaNH commented May 8, 2022

Hi @dunglas ,
sorry to come back, i know you're busy.

Just, do you know if is it possible to combine Fields and Preload to a nested level of relationship with current version of Vulcain?! Or i miss something that i misunderstand
Push is stopped after 1st level if i add fields (if i comment adding Fields all is pushed but without picked fields)
My last js script with query mode :

    let url = 'https://localhost/greetings?'
    // url += encodeURI('preload="/hydra:member/*/@id/author/comments/*"')
    // url += encodeURI('preload="/hydra:member/*/author/comments/*"')
    url += encodeURI('preload="/hydra:member/*/author"&preload="/hydra:member/*/author/comments/*"')
    url += encodeURI('&fields="/hydra:member/*/@id"&fields="/hydra:member/*/name"&fields="/hydra:member/*/author/@id"&fields="/hydra:member/*/author/firstName"&fields="/hydra:member/*/author/comments/*/@id"')
    // url += encodeURI('&fields="/hydra:member/*/@id"&fields="/hydra:member/*/name"&fields="/hydra:member/*/author/firstName"&fields="/hydra:member/*/author/comments/*/@id"')
    // url += encodeURI('fields="/hydra:member/*/@id"&fields="/hydra:member/*/name"')

    window.fetch(url).then(async (response) => {
        const json = await response.json()
        const greetings = json['hydra:member']
            window.fetch(greetings[0]['author'])
                .then(async(reponse2) => {
                    const author = await reponse2.json()
                    window.fetch(author.comments[0])
                })
    }
    )

My greetings collection:

{
    "hydra:member": [
        {
            "author": "/authors/1?fields=%22%2Fcomments%2F%2A%2F%40id%22%2C+%22%2F%40id%22%2C+%22%2FfirstName%22",
            "@id": "/greetings/1",
            "name": "TestABC"
        },
        {
            "author": "/authors/1?fields=%22%2Fcomments%2F%2A%2F%40id%22%2C+%22%2F%40id%22%2C+%22%2FfirstName%22",
            "@id": "/greetings/7",
            "name": "AAA"
        },
        {
            "author": "/authors/1?fields=%22%2Fcomments%2F%2A%2F%40id%22%2C+%22%2F%40id%22%2C+%22%2FfirstName%22",
            "@id": "/greetings/8",
            "name": "fdgfdg"
        },
        {
            "author": "/authors/1?fields=%22%2Fcomments%2F%2A%2F%40id%22%2C+%22%2F%40id%22%2C+%22%2FfirstName%22",
            "@id": "/greetings/9",
            "name": "CCC"
        },
        {
            "author": "/authors/1?fields=%22%2Fcomments%2F%2A%2F%40id%22%2C+%22%2F%40id%22%2C+%22%2FfirstName%22",
            "@id": "/greetings/10",
            "name": "123456"
        }
    ]
}

My author (pushed)

{
  "comments": [
    "/comments/1?fields=%22%2F%40id%22"
  ],
  "@id": "/authors/1",
  "firstName": "BenWa"
}

My comment (not push but good fields)

{
  "@id": "/comments/1"
}

Thanks for your time.

BenWa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants