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] One-to-Many relations? #1964

Closed
bwegrzyn opened this issue May 22, 2018 · 6 comments
Closed

[GraphQL] One-to-Many relations? #1964

bwegrzyn opened this issue May 22, 2018 · 6 comments

Comments

@bwegrzyn
Copy link
Contributor

When defining a one-to-many relation (ex. User->Vehicles), GraphQL currently expects [String] on the create/update mutation. Is there a way to create new Vehicles in the same request as creating/updating the User? It seems like GraphQL expects you to pass a list of Vehicle IDs instead of an array of Vehicle data like I would do in a REST call.

I would like to avoid doing multiple API calls if possible. Any ideas on how to accomplish this?

@alanpoulain
Copy link
Member

It was possible before, but was removed because it was causing some issues with the introspection.
Everything is explained here: #1886.

@bwegrzyn
Copy link
Contributor Author

I see... so it seems like this feature won't be returning until Facebook agrees on the spec for it.

@alanpoulain Until the spec is agreed upon, what is the recommended approach to working with one-to-many data structures?

@alanpoulain
Copy link
Member

Yes or if the schema builder generates two fields. One for the IRI and the other for the data.
Unfortunately, you need more than one request to make it work with the current implementation.

@oleg-brizy
Copy link

I made it work like this #3422

@jnugh
Copy link
Contributor

jnugh commented Apr 19, 2020

I think having two fields is not the best solution in every case. This becomes an issue when a field may not be null. The client would then have to fill both fields which does not make sense. Alternatively both fields are nullable which makes it harder to interpret the schema and would allow invalid input data that "looks right".

How about adding a temporary, user generated id that can be used to identify an object after it has been created?

When the client adds such a temporary id to a create mutation and uses the same id as a reference later in the same request it would refer to the newly created entity.
I just implemented this pattern because we would like to be able to create multiple related entities in the same transaction and it works quite well.

mutation {
  createFoo(input: { tmpId: "myAwesomeObject", name: "test" }) {
    foo {
      id
      name
    }
  }
  createBar(input: { foo: "myAwesomeObject", name: "test2" }) {
    bar {
      id
      name
      foo {
        id
        name
      }
    }
  }
}

would return something like:

{
  "createFoo": {
    "foo": {
      "id": "/api/foo/1",
      "name": "test"
    }
  },
  "createBar": {
    "bar": {
      "id": "/api/bar/1",
      "name": "test2",
      "foo": {
        "id": "/api/foo/1",
        "name": "test"
      }
    }
  }
}

The temporary id can be stored throughout the request, along with the entity class name and the newly persisted entity.

@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.

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

4 participants