Reputation: 275
I am new to graphQL, Apollo etc. Soon I will be working on a low-cost app with 3 (rest-api) datasources and only 1 consumer to create (mainly) a User Type. It's planned to use serverless functions as hosting. I've been reading on schema stitching, federation and modules, but can't find a good answer on why I should use Apollo federation in this small project. As I understand it, you need multiple apollo-servers and will have a higher deploy/serverless cost because of it compared to a monolithic Apollo-server.
A simplified example:
Server 1:
type User {
id: ID!
firstname: String
}
Server 2:
extend type User @key(fields: "id") {
lastname: String
}
Server 3:
extend type User @key(fields: "id") {
email: String
}
Would you suggest me still using Apollo federation or the deprecated schema stitching. Can graphql-modules be a good solution or another plugin?
Upvotes: 2
Views: 250
Reputation: 1799
If you just have a single User schema, its not a good idea to use apollo federation. Single Apollo server would suffice. You can have a single server talking to multiple data sources. Apollo federation is basically a new version of stitching. (Stitching is deprecated) Both schema stitching and Apollo federation are for micro services.
For an example, if you were building an e-commerce backend. You could have a server working on User type related queries and mutations(login, signup, user info etc.). You would have other services for order, products, stocks etc.
In general, one domain(schema) per service.
About extending types, the example you have shown is not ideal. lastname and email should probably be declared in the user service.
You would extend User type in one of the following scenarios:
type Order {
id:ID!
products: [Product!]!
user: User! //the user which did the order
}
2. If you want to add a field to the User schema.
For example, you might want to relate all the orders made by a certain user.
extend type User @key(fields:"id") { id: ID! orders:[Order!]! }
Then in your order service, you can add a resolver that returns all the orders according to user's `id`
Upvotes: 1