Home › Articles › Reading

December 8, 2024

Integrated vs Connected GraphQL in Sitecore Choosing the Right Approach

One of the common decisions when developing Sitecore headless components is whether additional content should be included in the Layout Service response or retrieved independently by the frontend application.

Sitecore JSS provides two approaches to address these requirements: Integrated GraphQL and Connected GraphQL.

Both use GraphQL, but they differ in where the query is configured, when it executes and how the frontend receives the results.

Understanding Integrated GraphQL

Integrated GraphQL allows developers to associate a GraphQL query with a Sitecore rendering definition.

When Sitecore generates the layout data, it executes the configured query and includes the results in the component's data.

The frontend does not need to initiate a separate GraphQL request.

Consider a Related Articles component that requires information from multiple content items associated with its datasource.

Instead of writing a custom C# Rendering Contents Resolver, we could define a GraphQL query that retrieves the required information.

Implementing Integrated GraphQL

Navigate to the JSON Rendering definition in Sitecore and locate the Component GraphQL Query field.

Configure a query similar to the following:

query RelatedArticles(
  $datasource: String!
  $language: String!
) {
  datasource: item(
    path: $datasource
    language: $language
  ) {
    id
    name
    title: field(name: "Title") {
      value
    }
    children {
      results {
        id
        name
        title: field(name: "Title") {
          value
        }
      }
    }
  }
}

For supported Layout Service configurations, Sitecore automatically supplies variables such as:

  • $datasource — Rendering datasource.

  • $contextItem — Current page item.

  • $language — Current content language.

The configured JSS application must also be associated with an appropriate GraphQL endpoint.

The exact query structure depends on the endpoint schema.

Understanding the response

The GraphQL results become part of the rendering's fields in the layout response.

A simplified example:

{
  "componentName": "RelatedArticles",
  "fields": {
    "data": {
      "datasource": {
        "name": "Articles",
        "children": {
          "results": [
            {
              "name": "ArticleA",
              "title": {
                "value": "Headless Architecture"
              }
            }
          ]
        }
      }
    }
  }
}

The React component consumes this data as part of its existing rendering properties.

One important consideration is that Integrated GraphQL can replace the standard datasource field structure. The frontend component must therefore be designed against the resulting GraphQL contract.

Understanding Connected GraphQL

Connected GraphQL allows the frontend application to execute queries independently of the Layout Service.

Unlike Integrated GraphQL, the query does not need to be attached to a Sitecore rendering definition.

The application controls when the query executes, which variables are supplied and how the resulting data is managed.

Consider a Product Listing component with filtering, sorting and pagination.

Executing a new query whenever the visitor changes a filter is more appropriate than retrieving all products as part of the initial page layout.

Implementing Connected GraphQL

A Next.js application can execute GraphQL requests through a server-side service.

The following example demonstrates a simple implementation using fetch.

export async function executeGraphQL<T>(
  query: string,
  variables: Record<string, unknown>
): Promise<T> {

  const endpoint =
    process.env.SITECORE_GRAPHQL_ENDPOINT;

  const apiKey =
    process.env.SITECORE_API_KEY;

  if (!endpoint || !apiKey) {
    throw new Error(
      "Sitecore GraphQL configuration is missing"
    );
  }

  const response = await fetch(endpoint, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "sc_apikey": apiKey
    },
    body: JSON.stringify({
      query,
      variables
    })
  });

  if (!response.ok) {
    throw new Error(
      `GraphQL HTTP error: ${response.status}`
    );
  }

  const result = await response.json();

  if (result.errors?.length) {
    throw new Error("GraphQL execution failed");
  }

  return result.data as T;
}

This example assumes a Sitecore endpoint configured to accept the API key through the sc_apikey header.

The service should remain server-side so that sensitive credentials are not exposed to the browser.

In production, I would also introduce request timeouts, monitoring, appropriate caching and structured error handling.

When should we use Integrated GraphQL?

Integrated GraphQL is appropriate when the component requires related content as part of its initial rendering and the query can be executed efficiently.

Examples include related articles, promotional collections and content-driven navigation.

However, an expensive integrated query increases the time required to generate the layout response in a traditional direct-delivery architecture.

When should we use Connected GraphQL?

Connected GraphQL is appropriate when the application needs greater control over data retrieval.

Examples include product filtering, pagination, user interactions, deferred content and independently refreshed component data.

The trade-off is that the application becomes responsible for additional requests, loading states, caching and error handling.

A practical architecture scenario

Consider an enterprise website with three components:

  • A Hero Banner containing a title, description and image.

  • A Related Articles component displaying three articles.

  • A Product Listing component supporting thousands of products with filtering and pagination.

I would use standard Layout Service fields for the Hero Banner, Integrated GraphQL for Related Articles if additional content relationships are required, and Connected GraphQL for Product Listing.

Using GraphQL for the Hero Banner would introduce unnecessary complexity, while retrieving thousands of products through Integrated GraphQL would increase the initial page-data processing cost.

Architect's takeaway

Integrated and Connected GraphQL are not competing technologies. They address different component data requirements.

The important architecture decision is determining which data must be available during initial page composition and which data can be retrieved independently.

Choose the simplest approach that satisfies the component's requirements while maintaining performance, content freshness and editorial capabilities.

No comments:

Post a Comment