A GraphQL schema is a collection of types that define the layout and shape of data for a GraphQL API. It specifies what data can be queried or mutated, the relationships between entities, and the operations available. The schema acts as a contract between the client and server, ensuring requests are validated and executed correctly. Learn more.
What are GraphQL types?
GraphQL types define entities and their fields within a schema. Types include scalar types (e.g., String, Int), object types (custom entities), input types (for arguments), enumeration types (enums), union types, and interface types. These types structure the data and operations available in your API. Source.
How do you define relationships between types in a GraphQL schema?
Relationships are defined by referencing one type within another. For example, a User type may have a posts field that returns a list of Post types, indicating a one-to-many relationship. This is expressed using the Schema Definition Language (SDL). Source.
What is the Schema Definition Language (SDL) in GraphQL?
The Schema Definition Language (SDL) is a human-readable syntax used to define GraphQL schemas, types, fields, and relationships. It allows developers to specify the structure of their API in a clear and concise way. Learn more.
What are scalar types in GraphQL?
Scalar types represent primitive data types such as Int, Float, String, Boolean, and ID. They are used for fields that hold basic values and cannot have nested sub-fields. Source.
What is an object type in GraphQL?
An object type is a custom type defined by the developer, containing multiple fields, each with its own type. Most entities in a schema are object types, such as User or Post. Source.
How do queries and mutations work in GraphQL schemas?
Queries are used to read data, while mutations are used to write or modify data. Both are defined as types in the schema, specifying entry points for operations. For example, a Query type may have a getAllUsers field, and a Mutation type may have an addUser field. Source.
What are input types in GraphQL?
Input types define the structure of arguments passed to queries and mutations. They are similar to object types but are used for input data. Input types are defined using the input keyword. Source.
What are enumeration types (enums) in GraphQL?
Enumeration types (enums) are scalar types with a predefined list of valid values. They are useful for fields where users must select from a set of options, such as gender or status. Source.
How do list and non-null modifiers work in GraphQL?
List modifiers ([]) indicate a field returns an array of values, while non-null modifiers (!) specify that a field cannot be null. These can be combined for validation, such as [String!]! for a non-null list of non-null strings. Source.
What is the __typename field in GraphQL?
The __typename field is automatically included in every object type and returns the name of the object's type as a string. It helps clients identify the type of returned data in queries and mutations. Source.
What are union and interface types in GraphQL?
Interface types define a set of fields that multiple object types can implement, ensuring consistency. Union types allow a field to return one of several object types, useful for flexible search results. Source.
Can you mix input and output types in GraphQL?
No, input and output types must be defined separately. Input types are used for arguments in queries and mutations, while output types are used for returned data. Mixing them is invalid. Source.
What happens if a non-null field returns null in GraphQL?
If a non-null field returns null, GraphQL triggers an execution error and informs the client that something has gone wrong. This ensures data integrity and reliability. Source.
How do you use GraphQL with Hygraph?
Hygraph provides instant GraphQL APIs once you define your schema in the dashboard. You can use these APIs to query, mutate, and subscribe to data, leveraging Hygraph's GraphQL-native headless CMS capabilities. Learn more.
What are some recommended resources for learning more about GraphQL schemas and types?
GraphQL uses type modifiers such as ! (non-null) and [] (list) to validate data types and ensure correct data is returned or accepted. Invalid data triggers errors, maintaining schema integrity. Source.
What is the difference between a scalar and an object type in GraphQL?
Scalar types are primitive and hold basic values (e.g., String, Int), while object types are custom entities with multiple fields, each of which can be a scalar or another object type. Source.
How do you define a one-to-many relationship in GraphQL?
A one-to-many relationship is defined by having a field in one type that returns a list of another type. For example, a User type with a posts field returning [Post]. Source.
What is the purpose of the Query and Mutation types in GraphQL?
The Query type defines entry points for reading data, while the Mutation type defines entry points for writing or modifying data. Both are essential for interacting with a GraphQL API. Source.
How does Hygraph support instant GraphQL APIs?
Hygraph scaffolds instant GraphQL APIs based on your schema definition in the dashboard, allowing you to start querying and mutating data immediately without manual API setup. Learn more.
Features & Capabilities
What are the key capabilities of Hygraph?
Hygraph offers a GraphQL-native headless CMS with features like Smart Edge Cache for fast content delivery, content federation, custom roles, rich text management, project backups, and enterprise-grade security. Source.
Does Hygraph support content federation?
Yes, Hygraph supports content federation, allowing integration of multiple data sources without duplication and solving data silos for consistent content delivery across platforms. Source.
What is Smart Edge Cache in Hygraph?
Smart Edge Cache is a feature in Hygraph that enhances performance and accelerates content delivery, making it ideal for high-traffic and global audiences. Learn more.
How does Hygraph ensure high performance for content delivery?
Hygraph provides high-performance endpoints and measures GraphQL API performance, offering practical advice for developers to optimize API usage. Source.
What security and compliance certifications does Hygraph have?
Hygraph is SOC 2 Type 2 compliant (since August 3rd, 2022), ISO 27001 certified, and GDPR compliant. These certifications ensure robust security and compliance for enterprise customers. Source.
What security features does Hygraph offer?
Hygraph provides granular permissions, SSO integrations, audit logs, encryption at rest and in transit, regular backups, and a process for reporting security issues. Source.
How does Hygraph support enterprise-grade compliance?
Hygraph meets enterprise requirements with dedicated hosting, custom SLAs, security certifications, and support for GDPR and CCPA compliance. Source.
What feedback have customers given about Hygraph's ease of use?
Customers praise Hygraph's intuitive editor UI, accessibility for non-technical users, and custom app integration. Hygraph was recognized for "Best Usability" in Summer 2023. Source.
How does Hygraph differentiate itself from other headless CMS platforms?
Hygraph stands out as the first GraphQL-native headless CMS, offering content federation, Smart Edge Cache, user-friendly tools, and enterprise-grade features. It focuses on flexibility, scalability, and integration capabilities. Source.
Use Cases & Benefits
Who is the target audience for Hygraph?
Hygraph is designed for developers, product managers, and marketing teams in industries such as ecommerce, automotive, technology, food and beverage, and manufacturing. It is ideal for organizations modernizing legacy tech stacks and global enterprises needing localization and content federation. Source.
What problems does Hygraph solve?
Hygraph addresses operational inefficiencies, financial challenges, and technical issues such as developer dependency, legacy tech stack modernization, content inconsistency, high costs, slow speed-to-market, integration difficulties, and performance bottlenecks. Source.
What are some customer success stories with Hygraph?
Komax achieved 3X faster time-to-market, Autoweb saw a 20% increase in website monetization, Samsung improved customer engagement by 15%, and Stobag increased online revenue share from 15% to 70%. Read more.
How long does it take to implement Hygraph?
Implementation time varies by project. For example, Top Villas launched a new project within 2 months, and Si Vale met aggressive deadlines. Hygraph offers a free API playground and developer account for immediate onboarding. Source.
What training and onboarding resources does Hygraph provide?
Hygraph offers webinars, live streams, how-to videos, and extensive documentation to support onboarding and ongoing training. Documentation.
What KPIs and metrics are associated with Hygraph's solutions?
Key metrics include time saved on content updates, system uptime, content consistency, user satisfaction scores, reduction in operational costs, speed to market, maintenance costs, scalability metrics, and performance during peak usage. Read more.
How does Hygraph help with e-learning platforms?
Hygraph supports building e-learning platforms by providing a flexible schema for courses, lessons, and interactive components. Educators can monetize expertise and deliver high-quality content. Learn more.
What is the main goal of the learning platform discussed in Hygraph's article?
The main goal is to provide students with high-quality educational content and allow educators to monetize their expertise through premium courses. Source.
How can the content model in Hygraph be extended for e-learning?
The lesson content model can be extended to include interactive components such as quizzes or supporting downloads like cheat sheets. Source.
What is e-learning according to Hygraph?
E-learning refers to the delivery of learning materials using interactive electronic systems, including enrollment, class management, online teaching, collaboration, examination, and grading systems. Source.
What does e-learning encompass beyond sharing courses?
E-learning extends to virtual classrooms, real-time online teaching, and offering entire degree programs remotely. Source.
Where can I learn more about environments in Hygraph?
What is the overarching vision and mission of Hygraph?
Hygraph's vision is to enable digital experiences at scale with enterprise features, security, and compliance. Its mission is rooted in trust, collaboration, ownership, customer focus, learning, transparency, and action-first values. Source.
Technical Requirements & Developer Resources
What languages are used to write GraphQL servers?
Popular languages for GraphQL servers include Graphene (Python), GraphQL Ruby, PostGraphile (Node.js), and Mercurius (Node.js). Source.
What are the top JavaScript GraphQL clients?
Top JavaScript GraphQL clients include urql, Relay, GraphQL-request, GenQL, and Apollo Client. Learn more.
What are the key features of GraphQL-request?
GraphQL-request is lightweight, supports queries, mutations, variables, and headers, offers TypeScript support, and is isomorphic. Source.
How does GraphQL simplify API usage?
GraphQL represents business models as graphs, provides complete API documentation, and allows clients to request granular data, preventing under- or over-fetching. Source.
What are the core concepts of GraphQL Mesh?
GraphQL Mesh shapes and builds executable GraphQL schemas, accesses data in remote APIs, and aggregates data from multiple sources. Source.
How do you implement logic into a Serverless function using Hygraph?
Use the gql import to encapsulate GraphQL queries and mutations, and check the shortUrl passed into the query from the page request. Source.
Where can I learn more about Slate nodes?
You can learn more about Slate nodes at this link.
What types are available when defining a schema in Hygraph?
Hygraph supports Model types (complex data structures) and Enumeration types (defined list of singular values). Source.
What are the three approaches to resolve schema stitching situations?
The three approaches are Schema Extensions, Type Merging, and Directive-based Type Merging. Source.
Let's take a look into what GraphQL Schemas and Types are.
We learned about “What is GraphQL” in the previous article of this series. Hygraph is a data platform that provides you with Instant GraphQL APIs; all you have to do is define your schema in the Hygraph dashboard and you’re good to go to use the instant GraphQL APIs. However, many other things happen under the hood, which eventually scaffolds those APIs for you like defining the GraphQL schema, creating resolvers for your queries, mutations, and subscriptions, etc.
In this article, we will be covering the basics of GraphQL Schemas and Types.
What is a GraphQL Schema?
A GraphQL server acts as a bridge between the client and the underlying data sources, providing a flexible and efficient way to query and manipulate data in a structured manner. When a client sends a GraphQL request to a server, the server receives the request and uses the GraphQL Schema to validate and execute the request. The server then sends back a response to the client which contains the requested data or errors, depending on the nature of the request. In order to serve these requests, and expose what operations are available to query or mutate the data, a GraphQL Server relies heavily on its Schema.
A GraphQL Type defines your entities with their fields and definitions.
For example:
typeUser{
name:String
email:String
}
This is a User Type, which defines two fields name and email, both are of the type String. Just like programming concepts both String and User are Types. String is a scalar type, whereas User is an object type created by us.
A GraphQL Schema is composed of various GraphQL types that define all your data entities, what are the relationships between these entities, and what operations are available to query and mutate your data. In order to support this GraphQL defines a human-readable Schema Definition Language (SDL).
For example:
typeUser{
name:String
email:String
posts:[Post]
}
typePost{
title:String
description:String
user:User
}
The above GraphQL schema gives us an exact picture of how User and Post entities are connected. We can clearly see a One-To-Many relationship between User-Post (one user can have many posts ) from the schema definition language itself.
Try Hygraph, the GraphQL native headless CMS
Build limitless solutions rapidly with our GraphQL-native API-first approach
As discussed above, GraphQL defines various types that we can utilize to build our schema.
Here are the different available types.
Scalar Type
Object Type
Input Types
Enumeration Type
Union and Interface Type
Lists and Non-Null
Scalar Type
Scalar types represent primitive data types for fields like Strings, Integers, etc. These types cannot have further nested sub-fields. Scalar types are automatically serialized and deserialized by GraphQL according to their respective types in the programming language.
Here are the Scalar types supported by GraphQL:
Int: A signed 32‐bit integer.
Float: A signed double-precision floating-point value.
String: A UTF‐8 character sequence.
Boolean: true or false.
ID: The ID scalar type represents a unique identifier and is serialized as a string.
Object Type
The majority of types that we define in a GraphQL schema are Object Types. Object types contain various fields and each field has its own type.
For example:
typeUser{
name:String
email:String
}
In this example:
User is a GraphQL Object Type, meaning it's a type with some fields. Object types will make up most of your schema.
name and email are fields on the User type. This also means that one cannot query anything beyond - - name and email under a User type.
name and email fields are of the Scalar Type: String
Query & Mutation Type
GraphQL provides a Query type that can be used whenever you want to query data from your data source.
For example:
typeQuery{
getAllUsers:[User]
}
This GraphQL schema defines a Query type that has a single field named getAllUsers. The getAllUsers query outputs an array of User objects. This means that when a client sends a query of getAllUsers to the server, they will receive a list of all the users in the system. With a REST-based API, this would look something like GET /api/users.
The Query type defines all entry points for read operations, similarly, there is a Mutation type that defines entry points for write operations on your GraphQLl server.
For example:
typeMutation{
addUser(name:String,email:String):User
}
This GraphQL schema defines a Mutation type that has a single field named addUser. The addUser field takes two arguments: name and email, both of which are of the String type. When a client calls this mutation field with valid name and email values, a new User object is created with the provided data and added to the system. The addUser mutation returns the newly created User object.
We have only explored how queries and mutations are defined in the GraphQL schema, we shall be exploring more details of using queries and mutations in applications in the upcoming articles.
Input Type
Input types are types that will define the data type of arguments in your queries and mutations. Input types are the same as Object types, the only difference is that to define an input type we use the keyword input instead of type.
Input types are very useful when you want to pass objects as arguments to your query or mutation instead of scalars. For instance in the addUser mutation above if you want to pass a user object as an argument, then you can define your input type and mutation like this:
inputUserInput{
name:String
email:String
}
typeMutation{
addUser(newUser:UserInput):User
}
One important thing to note is you cannot mix your input and output types. So if you have a query that outputs a user, you cannot reuse the above input type over there.
The example below is invalid:
typeQuery{
getUserById(id:ID):UserInput
}
The example below is the correct way:
inputUserInput{
name:String
email:String
}
typeUser{
name:String
email:String
}
typeMutation{
addUser(newUser:UserInput):User
}
typeQuery{
getUserById(id:ID):User
}
Enumeration Type
An enum is similar to a scalar type, but all its valid values are defined in the schema itself. Enums are most useful in situations where the user must pick from a prescribed list of options.
For example:
enumGENDER{
MALE
FEMALE
OTHER
}
Lists and Non-Null
You will be using the object, scalar, input, and enum types to define your schema. GraphQL also provides us with modifiers that can help us to do quick validations. These modifiers can be used in type definitions inside the schema and also in the arguments of queries and mutations. The modifiers are listed below
Exclamation Mark ! - Non-Null
Square Brackets [ ] - List
For example:
typeUser{
email:String!
city:String
hobbies:[String]
}
Here, we are saying that a User type will have fields email, city, and posts; an email should always exist for a user (as there is an exclamation mark in its declaration), but the city field is optional so it may or may not exist. This means that our server always expects to return a non-null value for email, If somehow, our server ends up getting a null value for the email field, it will trigger a GraphQL execution error, and let the client know that something has gone wrong.
If you take a look at the hobbies field its output is String wrapped with square brackets - [String], meaning that this field will output an array of Strings.
You can also combine the ! and [] modifiers as per your needs, here are some tricky examples:
typeUser{
...
hobbies:[String!]
# This means that the hobbies field can be null but it cannot have any null values
# hobbies: null // valid
# hobbies: [] // valid
# hobbies: ['cricket', 'movies'] // valid
# hobbies: ['cricket', null, 'movies'] // error
}
typeUser{
...
hobbies:[String]!
# This means that the hobbies field cannot be null but it can have null values
# hobbies: null // error
# hobbies: [] // valid
# hobbies: ['cricket', 'movies'] // valid
# hobbies: ['cricket', null, 'movies'] // valid
}
typeUser{
...
hobbies:[String!]!
# This means that the hobbies field cannot be null and cannot have any null values
# hobbies: null // error
# hobbies: [] // valid
# hobbies: ['cricket', 'movies'] // valid
# hobbies: ['cricket', null, 'movies'] // error
}
The __typename field
Each object type in your GraphQL schema has a field named __typename by default. We do not need to define it. This field returns the object type's name as a String (e.g., User or Post).
You can get the __typename field in a query or mutation response
For example:
# QUERY
querygetUserById{
getUserById(id:"1"){
__typename
name
email
}
}
# RESPONSE
{
"data":{
"getUserById":{
"__typename":"User",
"name":"John Doe",
"email":"johndoe@hygraph.com"
}
}
}
Union and Interface Type
Just like the programming concept we have an Interface type in GraphQL types too. An Interface defines a set of fields that can be implemented by several other Object types; when an object type implements an interface it must include all the fields of the interface and can also include additional fields.
For example
interfaceProduct{
id:ID!
name:String!
}
typeBookimplementsProduct{
id:ID!
name:String!
author:String!
isbn:String!
}
typeClothingimplementsProduct{
id:ID!
name:String!
size:String!
color:String!
}
Here we have an interface Product, which is implemented by types Book and Clothing. Type Book and Clothing need to have the fields id and name since they are defined in the interface Product.
Union types are used when you want to define a field that can output into more than one type.
For example
unionSearchResult=Movie|Music
The union type SearchResult here denotes that it can either be an Object type of Movie or Music. One important thing to note about union type is that we can only use Object types while defining a union.
The example below is invalid:
unionPostalCode=String|Int
Conclusion
In summary, understanding GraphQL schemas and types is crucial for building GraphQL APIs. A GraphQL schema is a collection of types that define the layout and shape of data. The GraphQL type system is composed of scalar types, object types, input types, enumeration types, union types, interface types, and type modifiers. We can use the GraphQL type system to define the GraphQL schema as per our use case and then proceed on building powerful APIs that can acquire data from various sources.