GraphQL Key Concepts

What is GraphQL?
GraphQL is a query language for APIs – not databases. In that sense it’s database agnostic and effectively can be used in any context where an API is used. It fetches data from a server where that data is stored in a database.

Why is GraphQL considered better then REST?
– REST too inflexible to keep up with the rapidly changing requirements of the clients that access them
– more flexibility and efficiency needed
– With a REST API, you would typically gather the data by accessing multiple endpoints
– In GraphQL on the other hand, you’d simply send a single query to the GraphQL server that includes the concrete data requirements.

What problems are GraphQL trying to solve?
– REST hit endpoints that return fixed data structures. It’s very difficult to design the API in a way that it’s able to provide clients with their exact data needs.
– No more over-fetching and downloading of superfluous data.
– No more under-fetching , e.g. for the endpoint /users//followers, a request have to be made to the /users endpoint and then hit the /users//followers endpoint for each user.
– Graphql allow for rapid iterations on the front-end, usually backend needs to be adjusted as well to account for the new data needs and this kills productivity.
– Able to get fine-grained insights about the data that’s requested on the backend as each client specifies exactly what information it’s interested in. This can help in evolving an API and deprecating specific fields that are not requested any more.
– Strong type system to define the capabilities of an API in GraphQL. Once the schema is defined, the teams working on frontend and backends can do their work without further communication since they both are aware of the definite structure of the data that’s sent over the network.

How does GraphQL work with a CQRS implementation
– You can use a different model to update information than the model you use to read information
– Split that conceptual model into separate models for update and display, which it refers to as Command and Query respectively
– Having the same conceptual model for commands and queries leads to a more complex model

What is a mutation? What does it do?
– Making changes to the data stored in backend
– creating new data
– updating existing data
– deleting existing data

– Mutations follow the same syntactical structure as queries
mutation {
createPerson(name: "Bob", age: 36) {

What is a resolver, and what is the function of it?
– Each field on each type is backed by a function called the resolver which is provided by the GraphQL server developer.
– When a field is executed, the corresponding resolver is called to produce the next value.
– At the top level of every GraphQL server is a type that represents all of the possible entry points into the GraphQL API, it’s often called the Root type or the Query type.

Why is GraphQL a good fit for a for a (micro)services architecture?
– GraphQL hides the fact that you have a microservice architecture from the clients.
– From a backend perspective, you want to split everything into microservices, but from a frontend perspective, you would like all your data to come from a single API.
– Using GraphQL is the best way that lets you do both.
– It lets you split up your backend into microservices, while still providing a single API to all your application, and allowing joins across data from different services.

How does GraphQL prevent over querying?
– Overfetching means that a client downloads more information than is actually required in the app.
– E.g. screen that needs to display a list of users only with their names.
– In a REST API, this app would usually hit the /users endpoint and receive a JSON array with user data.
– This response however might contain more info about the users that are returned, e.g. their birthdays or addresses – information that is useless for the client because it only needs to display the users’ names.

Tell me 2 ways of limiting complex queries.
– Simple timeout to defend against large queries, e.g. server configured with a 5 seconds timeout would stop the execution of any query that is taking more than 5 seconds to execute.
– Maximum Query Depth, e.g. server configured with a Maximum Query Depth of 3, everything within the red marker is considered too deep and the query is invalid.

Explain me how one builds a GraphQL API?
– Create a simple relationship schema.
– Define our somethingType with a name, a description, and then a list of fields. A field is defined by a symbol indicating its name, and then what type that field is.
– Query the data by defining QueryType (how our query is going to appear), schema and route to send the queries to.

Explain how GraphQL enables rapid prototyping?
– No fixed data structures needed so it is flexible.
– Teams working on frontend and backends can do their work without further communication due to the definite structure of the data that’s sent over the network

Whats the role of the backend?
– Store data for GraphQL to query from

Describe how you do versioning in GraphQL.
– GraphQL takes a strong opinion on avoiding versioning by providing the tools for the continuous evolution of a GraphQL schema.
– GraphQL only returns the data that’s explicitly requested, so new capabilities can be added via new types and new fields on those types without creating a breaking change.
– This has lead to a common practice of always avoiding breaking changes and serving a versionless API.

Leave a Reply

Your email address will not be published. Required fields are marked *