您还可以将查询放入自己的文件中,例如带有.graphql 或.gql 文件扩展名,use the graphql-tag/loader 可以从您需要的文件中加载查询。
在它自己的文件中给出这个查询:
query CurrentUserForLayout {
currentUser {
login
avatar_url
}
}
使用 webpack,您需要以下配置:
module: {
rules: [
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader',
},
],
},
然后你可以这样加载它:
import React, { Component } from 'react';
import { graphql } from 'react-apollo';
import currentUserQuery from './currentUser.graphql';
class Profile extends Component { ... }
Profile.propTypes = { ... };
export default graphql(currentUserQuery)(Profile)
更新:
您还可以在一个文件中包含多个查询:
query MyQuery1 {
...
}
query MyQuery2 {
...
}
然后您可以像这样加载它们:
import { MyQuery1, MyQuery2 } from 'query.gql'
除此之外,我不知道用于定义内联查询的其他选项。反引号不是 graphql 特定的。它只使用es6 template tags,例如样式化的组件确实定义了内联 css。您唯一的选择是获得一个可以识别 graphql 查询以提供编辑帮助和代码突出显示的 IDE。 IntelliJ 编辑器(IDEA、WebStorm、PyCharm)有一个插件,就像@Victor Vlasenko 指出的那样。