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

Example for recursive object creation / reference fields #6

Open
ghost opened this issue Jul 2, 2018 · 1 comment
Open

Example for recursive object creation / reference fields #6

ghost opened this issue Jul 2, 2018 · 1 comment

Comments

@ghost
Copy link

ghost commented Jul 2, 2018

Could you please provide an example for a mutation that recursively creates an object? On the Drupal side we expect the "A" bundle to contain a field referencing the "B" bundle.

Expected usage from the GraphQL side:

type B {
  z: String
}
type A {
  b: B
}
input BInput {
  z: String
}
input AInput {
  b: BInput
}
mutation CreateA($a: AInput) {
  createA(a: $a) {
    id
    b {
      id
      z
    }
  }
}

Note that XXXInput has the exact same structure and types as XXX.

With $a = { b: { z: "example" } } it should return { id: 1234, b: { id: 3456, z: "example" } } (e.g.). In particular, A and B should be created as distinct Drupal nodes.

We already had a look at the following resources, but without finding examples for anything but the most simple cases:

@pmelab
Copy link
Contributor

pmelab commented Oct 9, 2018

There is no way to do this automatically. You could use CreateEntityBase (as illustrated in https://www.amazeelabs.com/en/blog/extending-graphql-part-3-mutations) in for the host entity, but you have to create the nested entity manually (untested pseudocode):

protected function extractEntityInput(array $inputArgs, InputObjectType $inputType, ResolveInfo $info) {
  return [
    'b' => new B($inputArgs['b']).save(),
  ];
}

Or you could execute two mutations and look the nested entity up during creation of the host:

mutation CreateA($a: AInput) {
  createB(b: $b) {
    id
    z
  }
  createA(a: $a) {
    id
    b {
      id
      z
    }
  }
}
protected function extractEntityInput(array $inputArgs, InputObjectType $inputType, ResolveInfo $info) {
  return [
    'b' => B::lookupBbyZ($inputArgs['b']['z']),
  ];
}

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

1 participant