Complete the code to define a federated service with a key directive.
type Product @key(fields: "[1]") { id: ID! name: String }
The @key directive specifies the primary field used to uniquely identify the entity across services. Here, id is the correct key field.
Complete the code to extend a type from another federated service.
extend type User @key(fields: "[1]") { id: ID! @external reviews: [Review] }
The extend keyword is used to add fields to a type defined in another service. The @key directive must reference the unique identifier field, which is usually id.
Fix the error in the code by completing the directive to specify the field used for reference resolution.
type Review @key(fields: "[1]") { id: ID! body: String author: User @provides(fields: "username") }
The @key directive must reference the unique identifier field of the type, which is id in this case.
Fill both blanks to correctly define a federated entity with a reference resolver.
extend type Product @key(fields: "[1]") { id: ID! @external price: Int weight: Int } const resolvers = { Product: { __resolveReference(product) { return [2]; } } };
The @key directive must specify the unique identifier field id. The __resolveReference function receives the reference object and returns the full entity, so returning product is correct.
Fill all three blanks to define a federated schema with a service and a query that fetches an entity by key.
type Query {
productById(id: ID!): Product
}
type Product @key(fields: "[1]") {
id: ID!
name: String
price: Int
}
const resolvers = {
Query: {
productById(_, args) {
return [2];
}
},
Product: {
__resolveReference(reference) {
return [3];
}
}
};The @key directive uses id as the unique identifier. The productById query resolver finds the product by matching args.id. The __resolveReference resolver finds the product by matching reference.id.