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

[GraphQL] Embedded resources does not work for mutations #3258

Closed
Fgruntjes opened this issue Nov 14, 2019 · 8 comments
Closed

[GraphQL] Embedded resources does not work for mutations #3258

Fgruntjes opened this issue Nov 14, 2019 · 8 comments

Comments

@Fgruntjes
Copy link

Fgruntjes commented Nov 14, 2019

API Platform version(s) affected: 2.5.1

Description
When resources are defined in yaml instead of Doctrine GraphQL will not show them as embedded resources for mutations.

The schema definition

App\Entity\Book:
    collectionOperations: ~
    itemOperations: ~
    attributes:
        normalization_context:
            groups: ['Book']
        denormalization_context:
            groups: ['Book']
    properties:
        id:
            readonly: true
        title:
            required: true
        pages:
            subresource:
                resourceClass: 'App\Entity\Page'
                collection: true

App\Entity\Page:
    collectionOperations: ['get']
    itemOperations: ['get']
    attributes:
        normalization_context:
            groups: ['Book']
        denormalization_context:
            groups: ['Book']
    properties:
        id:
            readonly: true
        content:
            required: true

And serialization config

App\Entity\Book:
    attributes:
        id:
            groups: ['Book']
        title:
            groups: ['Book']
        pages:
            groups: ['Book']

App\Entity\Page:
    attributes:
        id:
            groups: ['Book']
        content:
            groups: ['Book']

Will create the following createBookInput object in graphql
image

How to reproduce

Additional Context
Similar to these issues but @alanpoulain suggested that they are likely not the same.

@alanpoulain
Copy link
Member

It's a known issue. More info here: #1964 and here: #1886.
There is an RFC ongoing for the input union type: https://github.com/graphql/graphql-spec/blob/master/rfcs/InputUnion.md but we can't know when it will be accepted.
The other solution, as I have described here: #1964 (comment) would be to have two fields (IRI + data).

@epashkov
Copy link

epashkov commented Nov 16, 2020

Is this issue appears only with One-To-Many relation types? I am trying to understand how to get graphql schema with embedded input type for One-To-One relation. And I see that the result just the same.

For example, if we have two entities: User and UserProfile defined as

/**
 * @ApiResource()
 */
class User
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var UserProfile
     */
    private $profile;
}
/**
 * @ApiResource()
 */
class UserProfile
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var User
     */
    private $user;
}

Then I expect that the profile field in updateUserInput will be type of updateUserProfileInput but in generated schema I get this type

input updateUserInput {
  id: ID!
  profile: String
}

Is it correct? Is there a way to update both User and it's profile in one mutation?

@epashkov
Copy link

Found this #1750 Is it still unsupported?

@iluuu1994
Copy link

iluuu1994 commented Aug 26, 2021

Are there any workarounds for this? E.g. can I implement the persisting logic manually? This is currently a pretty huge limitation. Multiple requests work, but they are very fragile. Partial failure (one of the N requests fails) makes it impossible to revert to the previous state. The only way around this is de-normalizing my database but that's also a pretty major drawback.

Edit: This can be implemented manually, should've searched the docs before. https://api-platform.com/docs/core/graphql/#custom-mutations Either way, it would be nice if this didn't require manual intervention.

@alanpoulain
Copy link
Member

See https://api-platform.com/docs/main/core/graphql/#embedded-relation-input-creation-of-relation-in-mutation for a way to solve this issue.

@iluuu1994
Copy link

@alanpoulain Thank you! (Unfortunately?) I switched from GraphQL to Rest after a few days. Nested selects can easily be done with the GroupFilter. Ironically, inserting/updating nested data with Rest worked much better than with GraphQL. Hopefully GraphQL is less limiting now.

@aelfannir
Copy link

Any update ?

@eradionov
Copy link

embedded resource does not work for query as well

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

No branches or pull requests

6 participants