在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):detrohutt/babel-plugin-import-graphql开源软件地址(OpenSource Url):https://github.com/detrohutt/babel-plugin-import-graphql开源编程语言(OpenSource Language):JavaScript 98.7%开源软件介绍(OpenSource Introduction):babel-plugin-import-graphqlBabel plugin enabling For users of the old package name (babel-plugin-inline-import-graphql-ast)Deprecation/Migration notesAs of May 27, 2018, the Migrating to babel-plugin-import-graphqlUpdate your babel configuration
Update |
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. |
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. |
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)
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)
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. |
create-react-app users can use this package without ejecting via react-app-rewire-inline-import-graphql-ast
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
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论