graphQL 5
Hi,
Here are the notes I have been taking for the different chapters in the basic lesson. I learned a lot about GraphQL. Specifically, what GraphQL is, the types of operations (queries, mutations, and subscriptions) that are used, how the schema is structured, and the processes that occur in the background to properly process client requests to the server and GraphQL.
GraphQL Chapters:
Chapter One: Introduction
GraphQL is a server side service that allows a more efficient way to request data from the server or database. It is an alternative for the use of REST API to for some data on the backend and database. It uses queries with specific fields to request for information and the server checks queries against schemas before performing a function to send back the requested information back to the client.
Chapter Two: Queries and Mutations
Queries can be organized by objects and specific fields of that object to request for specific information Queries can contain arguments to ask for information at a specific index or place (i.e. hero (id : 1000) or hero (last : 1)) Separate queries can be made unique using aliases as their object field names. Using aliases allows for multiple queries that may be requesting the same type of information. The use of fragments is useful in defining types of queries so that you can use the “…fragmentName" format to implement similar types of queries. Operation types and names are used to identify types of operations (query, mutation, and subscription) and to name specific operations so that they are identifiable within the code and during debugging. The use of variables are important when the user of the client application can have different options to choose. When using variables you can force a non-null selection (using !) and also pick default values to use as an input for the operation. In certain situations a directive can be useful should a certain boolean be true or false. There are two types of directives: @include which includes a variable parameter in the operation should a certain value be true and @skip which skips a variable parameter in the operation should a certain value be true. Mutations help clients write into the database and update any information using the mutation operation. In-line fragments allow clients to access different types of data that depend on the input parameter for an operation.
Chapter Three: Schemas and Types
Schemas are created to determine what type of data can be asked from the server Object types can be defined using the type keyword and with each object type, fields can be added in to determine what type of information each object the client can withdraw from that type of object. Using ! can force a type to be non-nullable Each field can take in zero or multiple arguments and can have set default values for each argument if the arguments are not required. There are query and mutation types that are defined within the schema Query and mutation types describe the types of queries and mutations a client can perform on the Graph QL interface. They often contain the object types with any necessary argument parameters. Scalar types are the leaves of the schema tree The five main scalar types in GraphQL include strings, ints, floats, booleans, and ID. Enum types can be used to map a certain type to select values Non-null modifiers can be applied to specific types such as String and Arrays to force certain types of inputs Interfaces are used to define an object’s fields and it is useful to diversify different object types who might root from the same interface. A union type is similar to interfaces but without the requirement of fields. It defines a set of object types that can be returned for a certain query Input types can be objects with fields that GraphQL can expect from the client request. You can define input type objects by using the type keyword.
Chapter Four: Validation
Graph QL’s type system makes it easier to detect any invalid queries and allows for the developer to receive any errors on invalid queries. One example of an invalid query is call to a fragment within a fragment. Another common invalid query is requesting for a field of an object type that doesn’t exist. The previous example can apply to scalar types as well as they are the leaves of a schema and would not contain any accessible fields. Another invalid query is the misuse of fragments to access a root object type
Chapter Five: Execution
During execution of operations, functions called resolvers are called for each field Each function calls for the next resolver for the next field until the leaves are reached (scalar types). A resolver function contains four arguments: obj: previous object args: parameters provided by client context: a value that corresponds to contextual info info: field information Resolvers use a promise for asynchronous calls to the database To return field information, resolvers simply access the fields of the obj referenced from the previous object and returns them Scalar coercion occurs when GraphQL internally defines enum type object fields as numbers but will display them with their appropriate enum values With lists, GraphQL uses a list of promises and waits until all promises are completed before moving on During each resolved function, it is placed in a key-value map to appropriately display the information to the client
Chapter Six: Introspection
GraphQL’s introspection system uses __ to differentiate scalar and object types from the types part of the system The introspection system is valuable in determining the fields present for each object type using the following types: __ schema, __ type, __ typekind, __field, __ inputvalue, __ enumvalue, __ directive. Together these types organize the way the operations interact with the schema and its different object and scalar types.