GraphQL 5 (08.01.2023 M) Flashcards
What is GraphQL schema?
Every GraphQL service defines a set of types which completely describe the set of possible data you can query on that service. Then, when queries come in, they are validated and executed against that schema.
Schema answers the questions:
- what fields can we select?
- what kinds of objects might they return?
- what fields are available on those sub-objects?
What language is used for schema definition in GraphQL?
GraphQL services can be written in any language. Since we can’t rely on a specific programming language syntax, like JavaScript, to talk about GraphQL schemas, own language is defined.
It is called “GraphQL schema language” - it’s similar to the query language, and allows us to talk about GraphQL schemas in a language-agnostic way.
Describe object type and fields.
The most basic components of a GraphQL schema are object types, which just represent a kind of object you can fetch from your service, and what fields it has.
In the GraphQL schema language, we might represent it like this (see the image).
The definition starts with ‘type’ keyword followed by type name, followed by fields definition between { }
Definition of a field looks like
< fieldName >: < fieldType >
You can use commas or new lines to separate field definitions.
Description of example image:
- Character is a GraphQL Object Type, meaning it’s a type with some fields.
- name and appearsIn are fields on the Character type. That means that name and appearsIn are the only fields that can appear in any part of a GraphQL query that operates on the Character type.
- String is one of the built-in scalar types
- String! means that the field is non-nullable, meaning that the GraphQL service promises to always give you a value when you query this field. In the type language, we’ll represent those with an exclamation mark.
- [Episode!]! represents an array of Episode objects. Since it is also non-nullable, you can always expect an array (with zero or more items) when you query the appearsIn field. And since Episode! is also non-nullable, you can always expect every item of the array to be an Episode object.
What is argument?
Every field on a GraphQL object type can have zero or more arguments.
Arguments are specified in the field declaration:
< fieldName >(< comma separated list of < argument > >): < fieldType>
< argument > = < argumentName >: < argumentType > [ = < default value > ]
All arguments are named. Unlike languages like JavaScript and Python where functions take a list of ordered arguments, all arguments in GraphQL are passed by name specifically.
Arguments can be either required or optional. When an argument is optional, we can define a default value.
Name two special GraphQL object types. What they are used for? Give an example.
There are:
- type Query
- type Mutation
They declare what queries and mutations are available for a schema.
These types are the same as a regular object type, but they are special because they define the entry point of every GraphQL operation.
It’s important to remember that other than the special status of being the “entry point” into the schema, the Query and Mutation types are the same as any other GraphQL object type, and their fields work exactly the same way.
Are Query type and Mutation type required?
Every GraphQL service has a query type and may or may not have a mutation type.
Give an example Query type declaration.
Query declaition starte from “type” keyword followed by “Query” followed by the declaration of fields between {}. See example image.
Declaration of Query type is the same as for ordinary object. And looks like this
< fieldName >(< comma separated list of < argument > >): < fieldType >
< argument > = < argumentName >: < argumentType > [ = < default value > ]