0

I'm using a GraphQL federation architecture and I am trying to add a new field to an extended type. For example, imagine I have a type in a remote federated schema:

type Book {
   id: ID!
   authorFirstName: String
   authorLastName: String
}

And I want to extend this type based on it's data, for example:

extend type Book @key(fields: "id") {
   id: ID! @external
   authorFullName: String
}

And in the resolver I will write:

Book: {
    authorFullName: async (parent) => {
       return `${parent.authorFirstName} ${parent.authorLastName}`
    }
}

But unfortunately, it doesn't work. The extending schema only receives the ID and the __typename and nothing more. Do you know how can I receive not only the "id" but also other necessary fields?

Thanks alot!

TalS
  • 69
  • 6
  • It looks like you may have tried to edit this when you weren't logged in. That isn't going to work. There is no way to verify that anonymous edits are from your accounts and edits that add to the question get rejected as they conflict with the author's intent. Please log in to make any edits to your own posts. – Stephen Ostermiller Apr 24 '22 at 09:03

1 Answers1

2

So I figured it out and what I needed is to add the necessary fields on the extended type and mark them with "@external" directive and in the computed field to add the "@requires" directive with the external fields.

In my example, the solution is: (Understandably that the federated external schema isn't changed.)

extend type Book @key(fields: "id") {
   id: ID! @external
   authorFirstName: String @external
   authorLastName: String @external
   authorFullName: String @requires(fields: "authorFirstName authorLastName")
}

the resolver stays as I mentioned above and that's it.

Dharman
  • 30,962
  • 25
  • 85
  • 135
TalS
  • 69
  • 6