All Projects → detrohutt → Babel Plugin Import Graphql

detrohutt / Babel Plugin Import Graphql

Licence: mit
Enables import syntax for .graphql and .gql files

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Babel Plugin Import Graphql

Crypto Grommet
Crypto and equities app
Stars: ✭ 39 (-86.27%)
Mutual labels:  graphql, apollo, nextjs
Next Advanced Apollo Starter
Advanced, but minimalistic Next.js pre-configured starter with focus on DX
Stars: ✭ 131 (-53.87%)
Mutual labels:  graphql, apollo, nextjs
Next React Graphql Apollo boostrap
React + GraphQL + Next.js project architecture that I play with right now
Stars: ✭ 59 (-79.23%)
Mutual labels:  graphql, apollo, nextjs
Brian Lovin Next
My personal site
Stars: ✭ 522 (+83.8%)
Mutual labels:  graphql, apollo, nextjs
Next Apollo Auth
Authentication Boilerplate with Next.js and Apollo GraphQL
Stars: ✭ 159 (-44.01%)
Mutual labels:  graphql, apollo, nextjs
Coolqlcool
Nextjs server to query websites with GraphQL
Stars: ✭ 623 (+119.37%)
Mutual labels:  graphql, schema, nextjs
Join Monster Graphql Tools Adapter
Use Join Monster to fetch your data with Apollo Server.
Stars: ✭ 130 (-54.23%)
Mutual labels:  graphql, apollo, schema
Apollo Upload Examples
A full stack demo of file uploads via GraphQL mutations using Apollo Server and apollo-upload-client.
Stars: ✭ 358 (+26.06%)
Mutual labels:  graphql, apollo, nextjs
Countries
🌎 Public GraphQL API for information about countries
Stars: ✭ 156 (-45.07%)
Mutual labels:  graphql, apollo, schema
Next Graphql Blog
🖊 A Blog including a server and a client. Server is built with Node, Express & a customized GraphQL-yoga server. Client is built with React, Next js & Apollo client.
Stars: ✭ 152 (-46.48%)
Mutual labels:  graphql, apollo, nextjs
Example Storefront
Example Storefront is Reaction Commerce’s headless ecommerce storefront - Next.js, GraphQL, React. Built using Apollo Client and the commerce-focused React UI components provided in the Storefront Component Library (reactioncommerce/reaction-component-library). It connects with Reaction backend with the GraphQL API.
Stars: ✭ 471 (+65.85%)
Mutual labels:  graphql, apollo, nextjs
Next React Graphql Apollo Hooks
React, Apollo, Next.js, GraphQL, Node.js, TypeScript high performance boilerplate with React hooks GraphQL implementation and automatic static type generation
Stars: ✭ 186 (-34.51%)
Mutual labels:  graphql, apollo, nextjs
Hackernews React Graphql
Hacker News clone rewritten with universal JavaScript, using React and GraphQL.
Stars: ✭ 4,242 (+1393.66%)
Mutual labels:  graphql, apollo, nextjs
Pizzaql
🍕 Modern OSS Order Management System for Pizza Restaurants
Stars: ✭ 631 (+122.18%)
Mutual labels:  graphql, apollo, nextjs
Next Apollo Example
Next & Apollo Example
Stars: ✭ 413 (+45.42%)
Mutual labels:  graphql, apollo, nextjs
Apollo Server Vercel
⚫ Production-ready Node.js GraphQL server for Vercel Serverless Functions
Stars: ✭ 69 (-75.7%)
Mutual labels:  graphql, apollo, nextjs
Graphback
Graphback - Out of the box GraphQL server and client
Stars: ✭ 323 (+13.73%)
Mutual labels:  graphql, apollo, schema
Graphql Cli
📟 Command line tool for common GraphQL development workflows
Stars: ✭ 1,814 (+538.73%)
Mutual labels:  graphql, apollo, schema
Ran
⚡ RAN! React . GraphQL . Next.js Toolkit ⚡ - SEO-Ready, Production-Ready, SSR, Hot-Reload, CSS-in-JS, Caching, CLI commands and more...
Stars: ✭ 2,128 (+649.3%)
Mutual labels:  graphql, apollo, nextjs
Next Ecommerce
⚡️ Quantum Ecommerce. Made with Next.js | GraphQL | Apollo Server | Apollo Client | SSR
Stars: ✭ 186 (-34.51%)
Mutual labels:  graphql, apollo, nextjs

npm Version npm Downloads npm License donate

babel-plugin-import-graphql

Babel plugin enabling import syntax for .graphql and .gql files.

For users of the old package name (babel-plugin-inline-import-graphql-ast)
Deprecation/Migration notes

As of May 27, 2018, the babel-plugin-inline-import-graphql-ast package name is deprecated. Please use babel-plugin-import-graphql (NPM) instead.

Migrating to babel-plugin-import-graphql

Update your babel configuration

Update plugins array:

babel-plugin-inline-import-graphql-ast (or inline-import-graphql-ast) -> import-graphql.

Update your package.json file

Update the package name in devDependencies:

babel-plugin-inline-import-graphql-ast -> babel-plugin-import-graphql.

Make sure your version string is compatible:

The first correct version of babel-plugin-import-graphql is 2.4.4 so please make sure your version string matches that. For instance, "babel-plugin-import-graphql": "^2.0.0" is fine because of the caret.

If you've pinned to a specific version, you'll need to upgrade and pin to at least 2.4.4 or widen your version range to include it.

Congratulations, you're all set!


If you enjoy my package please star the GitHub repo or share on Twitter (and follow me for updates)!

Prerequisites

Install

$ yarn add -D babel-plugin-import-graphql

In .babelrc file:

{
  "plugins": ["import-graphql"]
}

Each time you modify a GraphQL file, the cache must be cleared for the changes to take effect.

If using node then the node_modules/.cache/babel-loader folder must be cleared. I recommend prepending the relevant script in your package.json and rerunning the script when you change a GraphQL file:

{
  "scripts": {
    "start": "rm -rf ./node_modules/.cache/babel-loader && node index.js"
  }
}

If using React Native then the metro cache must be reset every time you change a GraphQL file:

react-native start --reset-cache

Note: Windows users would need to use rimraf or another solution in place of rm -rf.

Basic Usage

...
import myQuery from './query.graphql'
...
export default graphql(myQuery)(myComponent)

Supported features

Schema files

Feature Description
Default import The entire source code for the file will act as the default export.
#import syntax Types, etc. in one GraphQL file can be imported into another GraphQL file using this syntax: #import "./types.graphql". These imports will be resolved recursively to any reasonable depth of files. Currently, all content in the named file will be imported and there is no way to import specific types. If you want that behavior, you can store a single type in each file.

Operation/fragment files

All variants of the import syntax are supported for non-schema files, except import './filename'.

Feature Description
Multiple operations/fragments per file Multiple operations (queries/mutations/subscriptions) and/or fragments can be placed in a single file. However, in this case you cannot use unnamed operations/fragments. For example, query { test } would need to be query someName { test }.
Default import The first or only operation/fragment in a file will act as the default export. However, for backwards compatibility reasons, if there are both operations and fragments in a file, the first operation will act as the default export.
Named imports All operations/fragments, including the default, act as named exports.
#import syntax Fragments in one GraphQL file can be imported into another GraphQL file using this syntax: #import "./fragment.graphql". These imports will be resolved recursively to any reasonable depth of files. Currently, all fragments in the named file will be imported and there is no way to import specific fragments. If you want that behavior, you can store a single fragment in each file.

Full example

Before (without this plugin):

ProductsPage.js

import React, { Component } from 'react'
import gql from 'graphql-tag'
import { graphql } from 'react-apollo'

class ProductsPage extends Component {
  render() {
    if (this.props.data.loading) return <h3>Loading...</h3>
    return <div>{`This is my data: ${this.props.data.queryName}`}</div>
  }
}

const productsQuery = gql`
  query products {
    products {
      productId
      name
      description
      weight
    }
  }
`

export default graphql(productsQuery)(ProductsPage)

After (with this plugin):

productFragment.graphql

fragment productFragment on Product {
  name
  description
  weight
}

productsQuery.graphql

#import "./productFragment.graphql"
query products {
  products {
    productId
    ...productFragment
  }
}

ProductsPage.js

import React, { Component } from 'react'
import { graphql } from 'react-apollo'
import myImportedQuery from './productsQuery.graphql'

class ProductsPage extends Component {
  render() {
    if (this.props.data.loading) return <h3>Loading...</h3>
    return <div>{`This is my data: ${this.props.data.queryName}`}</div>
  }
}

export default graphql(myImportedQuery)(ProductsPage)

Options

Option Type Default Description
nodePath String value of NODE_PATH environment variable Intended for use with react-app-rewire-inline-import-graphql-ast -- Used to allow resolution of absolute paths to your .gql/.graphql files. If you already have your NODE_PATH variable set in your environment, you don't need to set this option. Not currently respected by #import syntax.
runtime Boolean false Enabling this option requires graphql-tag to be installed as a peerDependency. -- Instead of inlining the parsed AST object, which is very large, this option inlines your GraphQL source code along with an import of the gql function from graphql-tag and parses your GraphQL source code with gql at runtime.
extensions Array [] Enables loading GraphQL SDL files that have a custom extension, e.g. '.prisma'
emitDeclarations Boolean false Enables emmitting .d.ts files next to GraphQL query/fragment source file.

For users of create-react-app

create-react-app users can use this package without ejecting via react-app-rewire-inline-import-graphql-ast

Credits

The behavior of this plugin is inspired by and mostly mirrors the graphql-tag Webpack loader

This package started out as a modified version of babel-plugin-inline-import

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].