0
votes

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!

1

1 Answers

2
votes

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.