Here's your chance to speak at GraphQL Summit in New York City, October 8 - 10, 2024! 🏙️ Submit your proposal by May 31.
Launch GraphOS Studio
You're viewing documentation for a previous version of this software. Switch to the latest stable version.

Loading queries with Webpack

You can load queries over .graphql files using Webpack. The package graphql-tag comes with a loader easy to setup and with some benefits:

  1. Do not process GraphQL ASTs on client-side
  2. Enable queries to be separated from logic

In the example below, we create a new file called currentUser.graphql:

query CurrentUserForLayout {
currentUser {

You can load this file adding a rule in your webpack config file:

module: {
rules: [
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader',

As you can see, .graphql or .gql files will be parsed whenever imported:

import React, { Component } from 'react';
import { graphql } from '@apollo/react-hoc';
import currentUserQuery from './currentUser.graphql';
class Profile extends Component { ... }
Profile.propTypes = { ... };
export default graphql(currentUserQuery)(Profile)


Jest can't use the Webpack loaders. To make the same transformation work in Jest, use jest-transform-graphql.


FuseBox can't use the Webpack loaders. To make the same transformation work in FuseBox, use fuse-box-graphql-plugin.

React native

React native can't use the Webpack loaders. To make the same transformation work in React native, use babel-plugin-import-graphql.


create-react-app can't use the Webpack loaders unless ejected. To make the same transformation work in create-react-app without ejecting, use graphql.macro.

import { loader } from 'graphql.macro';
const currentUserQuery = loader('./currentUser.graphql');


You can use and include in . files and have webpack include those dependencies for you, similar to how you would use fragments and queries with the gql tag in plain JS.

#import "./UserInfoFragment.graphql"
query CurrentUserForLayout {
currentUser {

See how we import the UserInfo from another .graphql file (same way you'd import modules in JS).

And here's an example of defining the fragment in another .graphql file.

fragment UserInfo on User {
Integrating with Meteor
Network layer (Apollo Link)
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy