Giter Site home page Giter Site logo

vitorluizc / graphql-raw-loader Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 2.0 1.2 MB

:meat_on_bone: With Webpack, loads GraphQL files as raw strings and handle it's import directive & comment statement.

License: MIT License

JavaScript 60.07% TypeScript 39.93%
graphql webpack-loader webpack

graphql-raw-loader's Introduction

GraphQL raw loader

Build Status

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 graphql-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. ๐Ÿ˜Ž

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.
  }
}

graphql-raw-loader's People

Contributors

jagreehal avatar vitorluizc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

graphql-raw-loader's Issues

Import a file just once

Importing, using comment or directive, a file more than once results into repeated concatenated bundle. This breaks GraphQL.

So,

query GetA ($page: Int = 1, $limit: Int = 10, $term: String, $isSearch: Boolean = false) {
  getA (page: $page, limit: $limit) @skip(if: $isSearch) {
    ...FragmentA @import(from: "./FragmentA.graphql")
  }
  searchA (page: $page, limit: $limit, term: $term) @include(if: $isSearch) {
    ...FragmentA @import(from: "./FragmentA.graphql")
  }
}

results into

module.exports = [
  require('./FragmentA.graphql'),
  require('./FragmentA.graphql'), `
  query GetA ($page: Int = 1, $limit: Int = 10, $term: String, $isSearch: Boolean = false) {
    getA (page: $page, limit: $limit) @skip(if: $isSearch) {
      ...FragmentA
    }
    searchA (page: $page, limit: $limit, term: $term) @include(if: $isSearch) {
      ...FragmentA
    }
  }
`].join('\n\n')

We need some kind of cache to import a file just once.

Add unit tests

It need to be tested.

ava is a great option to write unit tests, it runs parallel tests and don't mess up the global scope.

  • Write unit tests
  • Add to Travis CI

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.