Socket
Socket
Sign inDemoInstall

graphql-raw-loader

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    graphql-raw-loader

Load GraphQL files as raw strings and handle it's import directive & comment statement.


Version published
Weekly downloads
9.4K
increased by19.84%
Maintainers
1
Install size
5.82 kB
Created
Weekly downloads
 

Readme

Source

GraphQL raw loader

Load GraphQL files as raw strings and handle it's import directive & comment statement.

Installation

Install it using NPM/Yarn.

# using NPM
npm i -D graphlq-raw-loader

# using Yarn
yarn add graphql-raw-loader --dev

Configuration

Add graphql-raw-loader to your webpack configuration:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.(graphql|gql)$/,
        loader: 'graphql-raw-loader'
      }
    ]
  }
}

Usage example

fragment UserProfile on User {
  # Some fields.
}
query Users {
  users () {
    ...UserProfile @import(from: "./UserProfile.graphql")
  }
}
import query from './User.graphl'

export const getUsers = async () => {
  const response = await fetch('/api/graphql', {
    body: JSON.stringify({ query }),
    headers: {
      'Content-Type': 'application/json',
    },
  })
  const users = await response.json()
  return users
}

Importing on GraphQL

Officially there's no way to import GraphQL files inside each other. Other loaders fixed it by parsing import statement inside a comment.

Also, theres a thread about implementing import statements on GraphQL. One of the best suggestions is about using a directive to import fragments.

This loader supports both ways. :sunglasses:

GraphQL loaders parses import statement inside comments.

Using comment import statement

# import "./UserDataFragment.graphql"
# The comment statement above is importing a file with UserDataFragment fragment
# inside it.

query Users {
  users (is_active: True) {
    data {
      ...UserDataFragment
    }
  }
}

Using @import directive

query Users {
  users (is_active: True) {
    data {
      ...
    }
    ...PaginationFragment @import(from: "./PaginationFragment.graphql")
    # The directive above is importing a file with PaginationFragment fragment
    # inside it.
  }
}

This loader supports both ways!

Keywords

FAQs

Last updated on 28 Feb 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc