What are GraphQL fragments and how do they help in query design?
GraphQL fragments are reusable pieces of query logic that allow you to define a set of fields and include them in multiple queries. This reduces duplication and makes queries more maintainable. For example, you can define a UserFields fragment and use it across different queries that need user data. Note: Fragments are a feature of GraphQL itself and are supported by Hygraph's GraphQL-native API. Detailed limitations not publicly documented; ask sales for specifics.
How can I use fragments with Hygraph's GraphQL API?
You can use fragments in your queries to Hygraph's GraphQL Content API just as you would with any GraphQL endpoint. Define fragments for reusable field sets and include them in your queries to reduce duplication and improve maintainability. For more details, refer to the API Reference documentation. Note: Some advanced GraphQL features may depend on your client library's support.
What best practices should I follow when using GraphQL fragments?
Best practices for using GraphQL fragments include: using descriptive names (e.g., UserFields), keeping fragments small and focused, collocating fragments with the components that use them, and avoiding duplication by reusing fragments across queries. These practices help maintain clean, understandable, and efficient queries. Note: Overusing fragments can lead to overly complex queries; review your schema and query needs regularly.
Features & Capabilities
What features does Hygraph offer for developers working with GraphQL?
Hygraph provides a GraphQL-native Headless CMS with features such as high-performance GraphQL Content API, content federation, schema evolution, and integration with modern tech stacks. Developers can use fragments, unions, and advanced GraphQL features directly in their queries. For more, see the API Reference documentation. Note: Some advanced GraphQL features may require specific client support.
What integrations are available with Hygraph?
Hygraph offers integrations with Digital Asset Management systems (Aprimo, AWS S3, Bynder, Cloudinary, Imgix, Mux, Scaleflex Filerobot), hosting and deployment platforms (Netlify, Vercel), Product Information Management (Akeneo), commerce solutions (BigCommerce), translation/localization (EasyTranslate), and more. For the full list, visit the Hygraph Marketplace. Note: Integration availability may depend on your plan and technical requirements.
Does Hygraph support API access and what types are available?
Yes, Hygraph provides several APIs: the GraphQL Content API for querying and manipulating content, the Management API for project structure, the Asset Upload API for file management, and the MCP Server API for AI assistant integration. See the API Reference documentation for details. Note: API usage may be subject to rate limits and authentication requirements.
Performance & Security
How does Hygraph ensure high performance for content delivery?
Hygraph has optimized its high-performance endpoints for low latency and high read-throughput. The read-only cache endpoint delivers 3-5x latency improvement, and the platform actively measures GraphQL API performance. For more, see the performance improvements blog post. Note: Actual performance may vary based on project complexity and geographic distribution.
What security and compliance certifications does Hygraph have?
Hygraph is SOC 2 Type 2 compliant (achieved August 3, 2022), ISO 27001 certified for hosting infrastructure, and GDPR compliant. The platform also supports granular permissions, SSO integrations (OIDC/LDAP/SAML), audit logs, encryption in transit and at rest, and regular backups. For details, see the Secure Features page. Note: For industry-specific compliance needs, contact Hygraph sales.
Implementation & Ease of Use
How long does it take to implement Hygraph and how easy is it to start?
Implementation time varies by project complexity. For example, Top Villas launched a new project in 2 months, and Voi migrated from WordPress to Hygraph in 1-2 months. Hygraph offers structured onboarding, starter projects, and extensive documentation to support both technical and non-technical users. See the Getting Started guide. Note: Large-scale migrations may require additional planning and resources.
What feedback have customers given about Hygraph's ease of use?
Customers highlight Hygraph's intuitive interface, quick adaptability, and user-friendly setup. For example, Sigurður G. (CTO) praised the UI's accessibility, and Charissa K. (Senior CMS Specialist) noted its clear setup and localization features. Multiple reviews mention that non-technical users can manage content independently. Note: Some advanced configurations may require developer involvement. Source: Hailey Feed - PMF Research.xlsx, Hygraph trial page.
Use Cases & Business Impact
Who can benefit from using Hygraph?
Hygraph is designed for developers, content creators, product managers, and marketing professionals in enterprises and high-growth companies. It is used in industries such as SaaS, eCommerce, media, healthcare, automotive, fintech, and more. For a full list, see Hygraph's case studies. Note: Small teams with simple content needs may find traditional CMS platforms sufficient.
What business impact can customers expect from using Hygraph?
Customers have achieved faster time-to-market (Komax: 3x faster), improved engagement (Samsung: 15% increase), and cost reduction (AutoWeb: 20% increase in monetization). Hygraph supports scaling content across multiple markets and languages, as seen with Voi and Lindex Group. For more, see Hygraph's case studies. Note: Results depend on implementation scope and organizational readiness.
What core problems does Hygraph solve for content teams?
Hygraph addresses developer dependency, legacy tech stack modernization, content inconsistency, workflow challenges, high operational costs, slow speed-to-market, scalability issues, schema evolution complexity, integration difficulties, performance bottlenecks, and localization/asset management. Note: Some highly specialized workflows may require custom development or third-party tools.
Customer Proof & Success Stories
Can you share specific case studies or customer success stories with Hygraph?
Yes. Notable examples include Samsung (15% improved engagement), Komax (3x faster time-to-market), AutoWeb (20% increase in monetization), Voi (scaled content across 12 countries and 10 languages), and Lindex Group (accelerated global content delivery). See more at Hygraph's case studies page. Note: Outcomes vary by project and implementation strategy.
Support & Documentation
What technical documentation and resources are available for Hygraph users?
Hygraph provides comprehensive API reference documentation, schema guides, getting started tutorials, integration guides (e.g., Mux, Akeneo, Auth0), and AI feature documentation. Classic documentation is available for legacy users. Access all resources at Hygraph Documentation. Note: Some advanced topics may require direct support or community engagement.
In addition, this tutorial assumes that you have a working knowledge of a programming language and a web development framework. The examples in this tutorial use JavaScript and the Apollo Client library, but the concepts should also apply to other languages and libraries.
What are Fragments?
Fragments in GraphQL are a way to define a set of fields that can be reused in multiple queries. Instead of repeating the same fields in each query, you can define a fragment that includes the fields you need and then include that fragment in your queries. This reduces duplication and makes queries more maintainable.
For instance, consider a scenario where you have a user profile page that displays the user's name, profile picture, and a list of their recent posts. Without fragments, you might have to write a query that looks like this:
query{
user(id:"123"){
name
profile_picture{
url
dimensions{
width
height
}
}
posts{
title
content
created_at
}
}
}
This query is already becoming complex, and if you add more fields or nested objects, it could quickly become unmanageable. Instead, you can use fragments to define smaller, reusable pieces of the query logic.
fragmentUserProfileFieldsonUser{
name
profile_picture{
url
dimensions{
width
height
}
}
}
fragmentPostFieldsonPost{
title
content
created_at
}
query{
user(id:"123"){
...UserProfileFields
posts{
...PostFields
}
}
}
With fragments, you can easily reuse the UserProfileFields and PostFields fragments in other queries, reducing duplication and making your queries more maintainable.
Try Hygraph, the GraphQL native headless CMS
Build limitless solutions rapidly with our GraphQL-native API-first approach
Fragments are defined using the fragment keyword followed by a name and a set of fields. For example:
fragmentUserFieldsonUser{
id
name
email
}
This defines a fragment named UserFields that includes the id, name, and email fields for a User object.
To use a fragment in a query, simply include the fragment name preceded by ... and followed by any additional fields you need. For example:
query{
allUsers{
...UserFields
address
phone
}
}
This includes the UserFields fragment along with the address and phone fields for each user.
Fragments can also be nested, allowing you to define more complex structures that can be reused across queries. For example:
fragmentAddressFieldsonAddress{
street
city
state
zip
}
fragmentUserFieldsonUser{
id
name
email
address{
...AddressFields
}
}
query{
allUsers{
...UserFields
phone
}
}
This defines two fragments: AddressFields for address information, and UserFields that includes the AddressFields fragment. The query then includes the UserFields fragment along with the phone field.
Collocating Fragments
GraphQL allows defining fragments either inline within a query or as standalone GraphQL operations. However, defining fragments as standalone operations enables a technique called collocating fragments.
Collocating fragments is a pattern where fragments are defined alongside the components that use them and can be shared with other components. This approach can make your application components and GraphQL queries more self-contained and easier to understand.
For example, imagine that you have a React component that renders a list of posts. You can define the fragment that fetches the necessary data for the component alongside the component itself like this:
import{ gql, useQuery }from"@apollo/client";
constALL_POSTS_FRAGMENT= gql`
fragment AllPosts on Post {
id
title
}
`;
constPOST_QUERY= gql`
query {
posts {
...AllPosts
excerpt
}
}
${ALL_POSTS_FRAGMENT}
`;
exportconstListPosts=()=>{
const{ loading, error, data }=useQuery(POST_QUERY);
if(loading)return<p>Loading...</p>;
if(error)return<p>Error:</p>;
return(
<div>
{data.posts.map((post)=>(
<div key={post.id}>
<h3>{post.title}</h3>
<p>{post.excerpt}</p>
</div>
))}
</div>
);
};
ListPosts.fragments={
allPosts:ALL_POSTS_FRAGMENT,
};
Here, we define the AllPosts fragment alongside the POST_QUERY query and ListPosts component. Additionally, we are also sharing the AllPosts fragment via the code below:
ListPosts.fragments={
allPosts:ALL_POSTS_FRAGMENT,
};
So that we can import and re-use this fragment in any parent component that accesses it like this:
import{AllPosts}from"./AllPosts";
constSOME_QUERY= gql`
query QueryName {
...
}
${AllPosts.fragments.allPosts}
`;
exportconstParentComponent=({ user })=>{
return<div>{/* ... */}</div>;
};
In this way, we can easily reuse fragments across multiple queries and components without redefining them each time. This approach also helps to keep our code organized and maintainable, especially as our application grows in complexity.
Using Fragments with Unions
Unions in GraphQL allow combining multiple types into a single type, which can be used in a query. Fragments can be used with Unions to define fields specific to each type. This is particularly useful because you can define a fragment that includes fields from all possible types in the Union.
For example, suppose you have a SearchResult type that can represent either a User or a Product:
unionSearchResult=User|Product
You can define fragments for each type and then use those fragments in a query that returns SearchResult objects:
fragmentUserFieldsonUser{
id
name
email
}
fragmentProductFieldsonProduct{
id
name
price
}
query{
search(query:"iphone"){
...onUser{
...UserFields
}
...onProduct{
...ProductFields
}
}
}
Here, the SearchResult Union can represent either a User or a Product. We define a fragment for each type (UserFields and ProductFields) and then include those fragments in the search query using the ... on syntax.
Using fragments with Union types can help you write more flexible queries and handle situations where a field can return multiple types. It's a powerful feature that can help you write cleaner, more concise GraphQL queries.
Best Practices for Fragments
When working with fragments, it's important to follow some best practices to keep your queries maintainable:
Use descriptive names for fragments, and include the object type in the name (e.g., UserFields instead of just Fields).
Keep fragments small and focused, defining only the fields needed for a particular use case.
Consider collocating fragments with the components that use them to keep related code together.
Avoid duplicating fragments that have the same fields. Instead, use one fragment and include it in all relevant queries.
By following these best practices, you can create maintainable queries that are easy to understand and modify as your application evolves.
Conclusion
Fragments are a powerful feature of GraphQL that allow you to define reusable pieces of query logic. By using fragments, you can reduce duplication in your queries and make them more maintainable. In this tutorial, we've covered fragments, how to use them, and best practices for working with them. Following these guidelines, you can create maintainable and efficient GraphQL queries for your application.
In addition, this tutorial assumes that you have a working knowledge of a programming language and a web development framework. The examples in this tutorial use JavaScript and the Apollo Client library, but the concepts should also apply to other languages and libraries.
What are Fragments?
Fragments in GraphQL are a way to define a set of fields that can be reused in multiple queries. Instead of repeating the same fields in each query, you can define a fragment that includes the fields you need and then include that fragment in your queries. This reduces duplication and makes queries more maintainable.
For instance, consider a scenario where you have a user profile page that displays the user's name, profile picture, and a list of their recent posts. Without fragments, you might have to write a query that looks like this:
query{
user(id:"123"){
name
profile_picture{
url
dimensions{
width
height
}
}
posts{
title
content
created_at
}
}
}
This query is already becoming complex, and if you add more fields or nested objects, it could quickly become unmanageable. Instead, you can use fragments to define smaller, reusable pieces of the query logic.
fragmentUserProfileFieldsonUser{
name
profile_picture{
url
dimensions{
width
height
}
}
}
fragmentPostFieldsonPost{
title
content
created_at
}
query{
user(id:"123"){
...UserProfileFields
posts{
...PostFields
}
}
}
With fragments, you can easily reuse the UserProfileFields and PostFields fragments in other queries, reducing duplication and making your queries more maintainable.
Try Hygraph, the GraphQL native headless CMS
Build limitless solutions rapidly with our GraphQL-native API-first approach
Fragments are defined using the fragment keyword followed by a name and a set of fields. For example:
fragmentUserFieldsonUser{
id
name
email
}
This defines a fragment named UserFields that includes the id, name, and email fields for a User object.
To use a fragment in a query, simply include the fragment name preceded by ... and followed by any additional fields you need. For example:
query{
allUsers{
...UserFields
address
phone
}
}
This includes the UserFields fragment along with the address and phone fields for each user.
Fragments can also be nested, allowing you to define more complex structures that can be reused across queries. For example:
fragmentAddressFieldsonAddress{
street
city
state
zip
}
fragmentUserFieldsonUser{
id
name
email
address{
...AddressFields
}
}
query{
allUsers{
...UserFields
phone
}
}
This defines two fragments: AddressFields for address information, and UserFields that includes the AddressFields fragment. The query then includes the UserFields fragment along with the phone field.
Collocating Fragments
GraphQL allows defining fragments either inline within a query or as standalone GraphQL operations. However, defining fragments as standalone operations enables a technique called collocating fragments.
Collocating fragments is a pattern where fragments are defined alongside the components that use them and can be shared with other components. This approach can make your application components and GraphQL queries more self-contained and easier to understand.
For example, imagine that you have a React component that renders a list of posts. You can define the fragment that fetches the necessary data for the component alongside the component itself like this:
import{ gql, useQuery }from"@apollo/client";
constALL_POSTS_FRAGMENT= gql`
fragment AllPosts on Post {
id
title
}
`;
constPOST_QUERY= gql`
query {
posts {
...AllPosts
excerpt
}
}
${ALL_POSTS_FRAGMENT}
`;
exportconstListPosts=()=>{
const{ loading, error, data }=useQuery(POST_QUERY);
if(loading)return<p>Loading...</p>;
if(error)return<p>Error:</p>;
return(
<div>
{data.posts.map((post)=>(
<div key={post.id}>
<h3>{post.title}</h3>
<p>{post.excerpt}</p>
</div>
))}
</div>
);
};
ListPosts.fragments={
allPosts:ALL_POSTS_FRAGMENT,
};
Here, we define the AllPosts fragment alongside the POST_QUERY query and ListPosts component. Additionally, we are also sharing the AllPosts fragment via the code below:
ListPosts.fragments={
allPosts:ALL_POSTS_FRAGMENT,
};
So that we can import and re-use this fragment in any parent component that accesses it like this:
import{AllPosts}from"./AllPosts";
constSOME_QUERY= gql`
query QueryName {
...
}
${AllPosts.fragments.allPosts}
`;
exportconstParentComponent=({ user })=>{
return<div>{/* ... */}</div>;
};
In this way, we can easily reuse fragments across multiple queries and components without redefining them each time. This approach also helps to keep our code organized and maintainable, especially as our application grows in complexity.
Using Fragments with Unions
Unions in GraphQL allow combining multiple types into a single type, which can be used in a query. Fragments can be used with Unions to define fields specific to each type. This is particularly useful because you can define a fragment that includes fields from all possible types in the Union.
For example, suppose you have a SearchResult type that can represent either a User or a Product:
unionSearchResult=User|Product
You can define fragments for each type and then use those fragments in a query that returns SearchResult objects:
fragmentUserFieldsonUser{
id
name
email
}
fragmentProductFieldsonProduct{
id
name
price
}
query{
search(query:"iphone"){
...onUser{
...UserFields
}
...onProduct{
...ProductFields
}
}
}
Here, the SearchResult Union can represent either a User or a Product. We define a fragment for each type (UserFields and ProductFields) and then include those fragments in the search query using the ... on syntax.
Using fragments with Union types can help you write more flexible queries and handle situations where a field can return multiple types. It's a powerful feature that can help you write cleaner, more concise GraphQL queries.
Best Practices for Fragments
When working with fragments, it's important to follow some best practices to keep your queries maintainable:
Use descriptive names for fragments, and include the object type in the name (e.g., UserFields instead of just Fields).
Keep fragments small and focused, defining only the fields needed for a particular use case.
Consider collocating fragments with the components that use them to keep related code together.
Avoid duplicating fragments that have the same fields. Instead, use one fragment and include it in all relevant queries.
By following these best practices, you can create maintainable queries that are easy to understand and modify as your application evolves.
Conclusion
Fragments are a powerful feature of GraphQL that allow you to define reusable pieces of query logic. By using fragments, you can reduce duplication in your queries and make them more maintainable. In this tutorial, we've covered fragments, how to use them, and best practices for working with them. Following these guidelines, you can create maintainable and efficient GraphQL queries for your application.