Skip to main content
drupalreleases
Release: Cms 2.2.3 — Update released for Drupal core (2.2.3)! Release: Easy Breadcrumb 2.0.11 — Minor update available for module easy_breadcrumb (2.0.11). Release: Bootstrap 8.x-3.42 — Minor update available for theme bootstrap (8.x-3.42). Release: Editoria11y Accessibility Checker 3.0.10 — Minor update available for module editoria11y (3.0.10). Release: Editoria11y Accessibility Checker 2.2.24 — Minor update available for module editoria11y (2.2.24). Release: Leaflet 10.4.13 — Minor update available for module leaflet (10.4.13). Release: Flag 5.1.1 — Minor update available for module flag (5.1.1). Release: MCP Sentinel 2.33.0 — Minor update available for module mcp_sentinel (2.33.0). Module Revived: Bootstrap 8.x-3.41 — Theme bootstrap updated after 6 months of inactivity (8.x-3.41). Security Coverage: Component Library — Module component_library now has official Drupal security advisory coverage.

GraphQL Fragment Include

17 sites Security covered Drupal 9–11

Part of the Graphql ecosystem · 39 projects

View on drupal.org

This module allows you to include reusable GraphQL fragments from separate files directly into your queries. It helps reduce redundancy when the same data structures are needed across multiple content types or queries.

A module to include fragments inside a GraphQL query, using the following syntax: # include path.gql.

This module is useful when:

  • You have a collection of paragraphs (or any other kind of entity) that is being used by multiple Content Types.
  • GraphQL is used to export content data (ideally by using Static Suite module) and you find the same repeated data structure on multiple queries.

To avoid having to repeat the same query for every content type, you should:

  • use GraphQL fragments
  • extract them to files
  • use this module to include those fragment files into your GraphQL queries.

This module transforms the following:

# include Image.gql
{
  content:nodeById(id: "1") {
    id: entityId
    ... on NodeArticle {
      title
      image: fieldImage {
        entity {
          ...Image
        }
      }
    }
  }
}

into this:

# sample content from Image.gql
fragment Image on MediaImage {
  entityLabel
  ... on MediaImage {
    mediaImage: fieldMediaImage {
      alt
      title
      url
      width
      height
    }
    credit: fieldImageCredit
    caption: fieldImageCaption
  }
}

{
  content:nodeById(id: "1") {
    id: entityId
    ... on NodeArticle {
      title
      image: fieldImage {
        entity {
          ...Image
        }
      }
    }
  }
}

How it works

  • Configure fragments base directory at /admin/config/graphql/fragment-include/config. That is were your fragment files are located. It's usually a directory inside /sites/default/.
  • Create a file inside the above directory, with the contents of your fragment, and with .gql extension. You can create subdirectories and include a fragment inside another fragment (infinite recursion protection is available).
  • Add an include to a GraqhQL query, using the following format: # include {RELATIVE_PATH_TO_FILE_INSIDE_FRAGMENTS_BASE_DIR}.gql
  • Execute the query, and the contents from the fragment file will be appended to the query before execution.

Debugging

If a fragment can not be found, a warning message is logged. Use dblog to view them.

Caveats

  • Due to the fact that the GraphQL specification does not support any kind of includes, we use comments and a syntax that is completely custom (# include path.gql). You can change that syntax to your convenience, extending graphql_fragment_include.graphql_fragment_loader service.
  • For the same above reason, GraphiQL IDE will remove the above comments when clicking "Prettify" button.

TODO

  • Find a way to maintain fragment includes when clicking GraphiQL's "Prettify" button.

Depends on

Dependencies of the latest stable release

  • graphql Drupal core ^8.x-3.2
  • graphql_core Drupal core

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
2
Tracked since
Sep 2023
Latest release
1 month ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Active

Releases

Version Type Core Release date
8.x-1.8 Stable 9–11 Aug 21, 2026
8.x-1.7 Stable 9–11 Sep 7, 2023