中继编译中继现代代码时变量不在范围内错误
Variable is not in scope error when relay-compiling Relay Modern code
我有以下简单的 Relay Modern 代码:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
QueryRenderer,
graphql
} from 'react-relay'
import environment from '../../../../../environment'
const CompanyItemQuery = graphql`
query CompanyItem_Query {
node(id: $companyId) {
id
...on Company {
id
name
}
}
}
`
class CompanyItem extends Component {
render() {
return (
<QueryRenderer
environment={environment}
query={CompanyItemQuery}
variables={{
companyId: 'test',
}}
render={({error, props}) => {
<div>
<p>Relay loaded {props.name} company</p>
</div>
}}
/>
);
}
};
export default CompanyItem;
我试图在 运行 之前中继编译代码,但出现以下错误:
> relay-compiler --src ./src --schema ./src/data/schema.graphql
HINT: pass --watch to keep watching for changes.
Parsed default in 0.08s
Writing default
Invariant Violation: RelayApplyFragmentArgumentTransform: variable `companyId` is not in scope.
at invariant (D:\DEV\WORKSPACE\client\node_modules\fbjs\lib\invariant.js:44:15)
at transformValue (D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4505:28)
at D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4497:18
at Array.map (native)
at transformArguments (D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4496:16)
at transformField (D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4417:15)
at D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4476:24
at Array.forEach (native)
at transformSelections (D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4461:15)
at transformNode (D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4385:21)
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "relay"
npm ERR! node v6.9.2
npm ERR! npm v3.10.9
npm ERR! code ELIFECYCLE
npm ERR! mom-client@3.0.0 relay: `relay-compiler --src ./src --schema ./src/data/schema.graphql`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the mom-client@3.0.0 relay script 'relay-compiler --src ./src --schema ./src/data/schema.graphql'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the mom-client package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! relay-compiler --src ./src --schema ./src/data/schema.graphql
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs mom-client
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls mom-client
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! D:\DEV\WORKSPACE\client\npm-debug.log
我无法找出为什么我的变量 companyId
不在范围内,因为它在 QueryRenderer
的 variables
部分中定义,如文档中所述。
如果我为 id 使用固定值(示例 node id: "Company:59b2cda12504b914cc398100"
)代码是中继编译的,我可以正常获取我的数据。当我需要动态选择id时,问题就出现了。
您似乎错过了在查询名称中声明变量(和类型)的问题 - 因此:
query CompanyItem_Query($companyId: ID!) {
而不是这个:
query CompanyItem_Query {
您可以在中继站点的示例中看到更多上下文 -
https://facebook.github.io/relay/docs/query-renderer.html
我有以下简单的 Relay Modern 代码:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
QueryRenderer,
graphql
} from 'react-relay'
import environment from '../../../../../environment'
const CompanyItemQuery = graphql`
query CompanyItem_Query {
node(id: $companyId) {
id
...on Company {
id
name
}
}
}
`
class CompanyItem extends Component {
render() {
return (
<QueryRenderer
environment={environment}
query={CompanyItemQuery}
variables={{
companyId: 'test',
}}
render={({error, props}) => {
<div>
<p>Relay loaded {props.name} company</p>
</div>
}}
/>
);
}
};
export default CompanyItem;
我试图在 运行 之前中继编译代码,但出现以下错误:
> relay-compiler --src ./src --schema ./src/data/schema.graphql
HINT: pass --watch to keep watching for changes.
Parsed default in 0.08s
Writing default
Invariant Violation: RelayApplyFragmentArgumentTransform: variable `companyId` is not in scope.
at invariant (D:\DEV\WORKSPACE\client\node_modules\fbjs\lib\invariant.js:44:15)
at transformValue (D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4505:28)
at D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4497:18
at Array.map (native)
at transformArguments (D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4496:16)
at transformField (D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4417:15)
at D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4476:24
at Array.forEach (native)
at transformSelections (D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4461:15)
at transformNode (D:\DEV\WORKSPACE\client\node_modules\relay-compiler\bin\relay-compiler:4385:21)
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "relay"
npm ERR! node v6.9.2
npm ERR! npm v3.10.9
npm ERR! code ELIFECYCLE
npm ERR! mom-client@3.0.0 relay: `relay-compiler --src ./src --schema ./src/data/schema.graphql`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the mom-client@3.0.0 relay script 'relay-compiler --src ./src --schema ./src/data/schema.graphql'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the mom-client package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! relay-compiler --src ./src --schema ./src/data/schema.graphql
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs mom-client
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls mom-client
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! D:\DEV\WORKSPACE\client\npm-debug.log
我无法找出为什么我的变量 companyId
不在范围内,因为它在 QueryRenderer
的 variables
部分中定义,如文档中所述。
如果我为 id 使用固定值(示例 node id: "Company:59b2cda12504b914cc398100"
)代码是中继编译的,我可以正常获取我的数据。当我需要动态选择id时,问题就出现了。
您似乎错过了在查询名称中声明变量(和类型)的问题 - 因此:
query CompanyItem_Query($companyId: ID!) {
而不是这个:
query CompanyItem_Query {
您可以在中继站点的示例中看到更多上下文 - https://facebook.github.io/relay/docs/query-renderer.html