How to write extended types from an Apollo federated schema to its database? - apollo-server

How are mutations handled in a federated schema? If I have an accounts service and a course service, the course service will likely extend the User type by some fields. How these fields are resolved for a query using the __resolveReference() function is well documented, however, if it comes to mutations I can not really find anything. How am I supposed to handle writes from the accounts service to its database without knowing all the fields a user has? I imagine a mutation that looks like this:
mutation{
createUser(username: "Jack" course: "whatever"){
some data...
}
}
Which service is now responsible for writing the course field to the users database? The course microservice has also its own database and shouldn't write anything to the users database.
Is this still an unsolved problem with Apollo schema federation?

Related

How to create return objects when schema (objectGraphType, QueryType, etc) generated during runtime

In multi tenants environment where each tenant can evolve into different database schema design after lauhching the services, graphGL solution seems not straightforward.
What I was able to complete, is using single schema 'Root' as ISchema within a wrapper schema (also as ISchema), which is actually the one exposed to other layers of GraphQL architecture, and create GraphTypes (QueryTypes, etc) as Fields of the 'Root' during runtime.
It seems good at least in creating those GraphTypes as run-time registered Fields of Root schema, however when I added resolver using resolve : context, I am not able to display the returned object(s) from repository, let's say to altair with error "Error trying to resolve field '{fieldname}'"
How can I return the data so those can be actually bound to the graphQL layers up to altair?

Postgraphile VS custom graphql resolver/schema in express server

I was experimenting with Postgraphile and it is a great tool to auto-generate the GraphQL API but I am still a little bit confused why should I use it. (P.S. I am not very experienced with GraphQL and PostgreSQL.)
Question 1:
Can I think of it this way? Postgraphile generates the code(query, mutation, resolvers, schema, types) for a server and these code are the code that we are going to write anyways if we are not using Postgraphile?
Question 2:
An example, a server receives a string James from the front end and I want to concat Bond to it before storing it in the db's full name column. How do I achieve this mutation? Am I going to need the makeExtendSchemaPlugin to merge my schema with the resolver in Postgraphile?
Question 1: Postgraphile generates the code(query, mutation, resolvers, schema, types) for a server and these code are the code that we are going to write anyways if we are not using Postgraphile
Correct. postgraphile will create CRUD operations that are optimized and many other features which otherwise you would need to implement.
Question 2: An example, a server receives a string James from the front end and I want to concat Bond to it before storing it in the db's full name column. How do I achieve this mutation?
You can create postgreSQL function and implement business logic. https://www.graphile.org/postgraphile/custom-mutations/

Can I keep a copy of a table of one database in another database in a microservice architecture?

I am currently new to microservice architecture so thanks in advance.
I have two different services a User Service and a Footballer Service each having their individual databases.(User database and Footballer database).
The Footballer service has a database with a single table storing footballer informations.
The User service has a database which stores User details along with other user related data.
Now a User can add footballers to their team by querying the Footballer service and I need to store them somewhere in order to be displayed later.
Currently I'm storing the footballers for each user in a table in the User database whereby I make a call to the Footballer service to give me the details of a specific footballer by ID and save them in the USer database by mapping against the USer ID.
So is this a good idea to do that and by any chance does it mean im replicating data between two services
and if it is than what other ways can i achieve the same functionality ?
Currently I'm storing the footballers for each user in a table in the User database whereby I make a call to the Footballer service to give me the details of a specific footballer by ID and save them in the USer database by mapping against the USer ID.
"Caching" is a fairly common pattern. From the perspective of the User microservice, the data from Footballer is just another input which you might save or not. If you are caching, you'll usually want to have some sort of timestamp/version on the cached data.
Caching identifiers is pretty normal - we often need some kind of correlation identifier to connect data in two different places.
If you find yourself using Footballer data in your User domain logic (that is to say, the way that User changes depends on the Footballer data available)... that's more suspicious, and may indicate that your boundaries are incorrectly drawn / some of your capabilities are in the wrong place.
If you are expecting the User Service to be autonomous - that is to say, to be able to continue serving its purpose even when Footballer is out of service, then your code needs to be able to work from cached copies of the data from Footballer and/or be able to suspend some parts of its work until fresh copies of that data are available.
People usually follow DDD (Domain driven design) in case of micro-services :
So here in your case there are two domains i.e. 2 services :
Users
Footballers
So, user service should only do user specific tasks, it should not be concerned about footballer's data.
Hence, according to DDD, the footballers that are linked to the user should be stored in football service.
Replicating the ID wouldn't be considered replication in case of microservices architecture.

AppSync update two tables with one mutation

I am a little confused on the best approach in how to update two tables with on GraphQL mutation, I am using AWS AppSync.
I have an application where I need a User to be able to register for an Event. Given I am using DynamoDB as the database, I had thought about a denormalized data structure for the User and Event tables. I am thinking of storing an array of brief Event details, such as eventID and title in the User table and an array of entrants in the Events table, holding only brief user info, such as userID and name. Firstly, is this a good approach or should I have a third join table to hold these 'relationships'.
If it's OK, I am needing to update both tables during the signUp mutation, but I am struggling to get my head around how to update 2 tables with the one mutation and in turn, one request mapping template.
Am I right in thinking I need to use a Pipeline resolver? Or is there another way to do this?
There are multiple options for this:
AppSync supports BatchWrite operations to update multiple DynamoDb tables at the same time
AppSync supports DynamoDb transactions to update multiple DynamoDb tables transactionally at the same time
Pipeline resolvers

How to fetch the data from database using spring boot without mapping

I have a database and in that database there are many tables of data. I want to fetch the data from any one of those tables by entering a query from the front-end application. I'm not doing any manipulation to the data, doing just retrieving the data from database.
Also, mapping the data requires writing so many entity or POJO classes, so I don't want to map the data to any object. How can I achieve this?
In this case, assuming the mapping of tables if not relevant, you don't need to use JPA/Hibernate at all.
You can use an old, battle tested jdbc template that can execute a query of your choice (that you'll pass from client), will serialize the response to JSONObject and return it as a response in your controller.
The client side will be responsible to rendering the result.
You might also query the database metadata to obtain the information about column names, types, etc. so that the client side will also get this information and will be able to show the results in a more convenient / "advanced" way.
Beware of security implications, though. Basically it means that the client will be able to delete all the records from the database by a simple query and you won't be able to avoid it :)

Resources