GraphQL:variableValues 中的意外变量:首先在 args 中不使用变量时
Posted
技术标签:
【中文标题】GraphQL:variableValues 中的意外变量:首先在 args 中不使用变量时【英文标题】:GraphQL: unexpected variables in variableValues: first when using no variables in args 【发布时间】:2021-07-26 12:25:42 【问题描述】:我的 GraphQL 查询在 Hasura 客户端中工作得很好,但是当我使用 Apollo 在我的 React 代码中使用查询时无法工作,我不明白为什么,因为我什至没有在查询中使用变量!这是一个返回一些参数的简单查询,仅此而已。
我得到的错误:错误:GraphQL 错误:variableValues 中的意外变量:第一个
查询
query MyQuery
account_ledger
transaction_id
transaction_amount
回复
"data":
"account_ledger": [
"transaction_id": 10049661150662448,
"transaction_amount": 16850
,
"transaction_id": 10094930139273068,
"transaction_amount": 1600
,
"transaction_id": 10061887587054946,
"transaction_amount": 3760
,
"transaction_id": 10056380885643976,
"transaction_amount": 2460
,
"transaction_id": 10010518554558650,
"transaction_amount": 1100
,
"transaction_id": 10000853716813392,
"transaction_amount": 17190
,
"transaction_id": 10070427147011012,
"transaction_amount": 1410
,
"transaction_id": 1240017190513613300,
"transaction_amount": 16240
,
"transaction_id": 10053817340697928,
"transaction_amount": 2600
,
"transaction_id": 10034938191128572,
"transaction_amount": 16440
,
"transaction_id": 660038224661446400,
"transaction_amount": 130
,
"transaction_id": 10006434778973120,
"transaction_amount": 2120
,
"transaction_id": 10095565557984748,
"transaction_amount": 16430
,
"transaction_id": 660004583855052700,
"transaction_amount": 9320
,
"transaction_id": 10019142513951312,
"transaction_amount": 17000
,
"transaction_id": 10027966903605936,
"transaction_amount": 2300
,
"transaction_id": 10011672164287928,
"transaction_amount": 16250
,
"transaction_id": 980086033556355200,
"transaction_amount": 4850
,
"transaction_id": 980028014087081700,
"transaction_amount": 5050
,
"transaction_id": 980055033146164600,
"transaction_amount": 5150
,
"transaction_id": 980005568496321000,
"transaction_amount": 5000
,
"transaction_id": 10030827020448600,
"transaction_amount": 17000
,
"transaction_id": 10063142951225168,
"transaction_amount": 18900
,
"transaction_id": 10047316425266172,
"transaction_amount": 16900
,
"transaction_id": 10097336206029004,
"transaction_amount": 18700
,
"transaction_id": 10068032212373680,
"transaction_amount": 17000
,
"transaction_id": 10082050265318940,
"transaction_amount": 19500
,
"transaction_id": 10092276780229294,
"transaction_amount": 17180
,
"transaction_id": 10006145444961766,
"transaction_amount": 1620
,
"transaction_id": 660085561999932800,
"transaction_amount": 17280
,
"transaction_id": 660043628285005400,
"transaction_amount": 1820
,
"transaction_id": 660044030786432300,
"transaction_amount": 600
,
"transaction_id": 761172323,
"transaction_amount": 1100
,
"transaction_id": 7611723231,
"transaction_amount": 1100
,
"transaction_id": 76117232312,
"transaction_amount": 1100
,
"transaction_id": 761172323122,
"transaction_amount": 1100
,
"transaction_id": 7611723231232,
"transaction_amount": 1100
,
"transaction_id": 76117232312320,
"transaction_amount": 1100
,
"transaction_id": 761,
"transaction_amount": 1100
,
"transaction_id": 860,
"transaction_amount": 1100
,
"transaction_id": 862,
"transaction_amount": 1100
,
"transaction_id": 863,
"transaction_amount": 1100
,
"transaction_id": 864,
"transaction_amount": 1100
,
"transaction_id": 865,
"transaction_amount": 1100
,
"transaction_id": 866,
"transaction_amount": 1100
,
"transaction_id": 861,
"transaction_amount": 1100
,
"transaction_id": 867,
"transaction_amount": 1100
,
"transaction_id": 660,
"transaction_amount": 1100
,
"transaction_id": 661,
"transaction_amount": 1100
,
"transaction_id": 662,
"transaction_amount": 1100
,
"transaction_id": 663,
"transaction_amount": 1100
,
"transaction_id": 664,
"transaction_amount": 1100
,
"transaction_id": 665,
"transaction_amount": 1100
,
"transaction_id": 666,
"transaction_amount": 1200
,
"transaction_id": 660031381225350700,
"transaction_amount": 17000
,
"transaction_id": 660055622930881700,
"transaction_amount": 16950
,
"transaction_id": 10067652780504088,
"transaction_amount": 16850
,
"transaction_id": 10064787647732094,
"transaction_amount": 18050
,
"transaction_id": 10084582774676448,
"transaction_amount": 16950
,
"transaction_id": 10017650062766650,
"transaction_amount": 2300
,
"transaction_id": 10006881632039372,
"transaction_amount": 17050
,
"transaction_id": 10053397273173264,
"transaction_amount": 1250
,
"transaction_id": 930095201762428900,
"transaction_amount": 1750
,
"transaction_id": 10088957448686158,
"transaction_amount": 50
,
"transaction_id": 10005735742902668,
"transaction_amount": 100
,
"transaction_id": 10039337345886142,
"transaction_amount": 50
,
"transaction_id": 10095366516766772,
"transaction_amount": 100
,
"transaction_id": 10002284245705388,
"transaction_amount": 50
,
"transaction_id": 10017259791814902,
"transaction_amount": 100
,
"transaction_id": 10044872520277520,
"transaction_amount": 50
,
"transaction_id": 10035665897251460,
"transaction_amount": 100
,
"transaction_id": 10061642617112248,
"transaction_amount": 40
,
"transaction_id": 10039927385620880,
"transaction_amount": 10
,
"transaction_id": 10083939482614400,
"transaction_amount": 10
,
"transaction_id": 10073607403232724,
"transaction_amount": 30
,
"transaction_id": 10033371940279358,
"transaction_amount": 10
,
"transaction_id": 10088685020263100,
"transaction_amount": 20
,
"transaction_id": 22,
"transaction_amount": 16000
,
"transaction_id": 23,
"transaction_amount": 16000
,
"transaction_id": 24,
"transaction_amount": 10
,
"transaction_id": 25,
"transaction_amount": 10
,
"transaction_id": 26,
"transaction_amount": 10
,
"transaction_id": 27,
"transaction_amount": 10
,
"transaction_id": 28,
"transaction_amount": 10
,
"transaction_id": 29,
"transaction_amount": 10
,
"transaction_id": 10052970351706668,
"transaction_amount": 150
,
"transaction_id": 10011441832995856,
"transaction_amount": 140
,
"transaction_id": 10074836284836260,
"transaction_amount": 140
,
"transaction_id": 10060348035311556,
"transaction_amount": 140
,
"transaction_id": 10023908034579110,
"transaction_amount": 150
,
"transaction_id": 10003729765773552,
"transaction_amount": 150
,
"transaction_id": 10009852337755392,
"transaction_amount": 150
,
"transaction_id": 10010841803656860,
"transaction_amount": 140
,
"transaction_id": 10082042605144656,
"transaction_amount": 150
,
"transaction_id": 10097882731962284,
"transaction_amount": 150
,
"transaction_id": 10034187763149790,
"transaction_amount": 150
,
"transaction_id": 10019268324692048,
"transaction_amount": 150
,
"transaction_id": 10073354499357328,
"transaction_amount": 150
,
"transaction_id": 10022720971260844,
"transaction_amount": 150
,
"transaction_id": 10036105037082676,
"transaction_amount": 150
,
"transaction_id": 1240012452291359700,
"transaction_amount": 800
,
"transaction_id": 65,
"transaction_amount": 50
,
"transaction_id": 66,
"transaction_amount": 50
,
"transaction_id": 67,
"transaction_amount": 50
,
"transaction_id": 660074765219966200,
"transaction_amount": 150
,
"transaction_id": 1210003769118153700,
"transaction_amount": 90
,
"transaction_id": 10003251950168978,
"transaction_amount": 150
,
"transaction_id": 10042118465931976,
"transaction_amount": 10330
,
"transaction_id": 1210053252806492200,
"transaction_amount": 6780
,
"transaction_id": 1210018828770363100,
"transaction_amount": 6780
,
"transaction_id": 1210051091991692500,
"transaction_amount": 7950
,
"transaction_id": 1210072103849991200,
"transaction_amount": 6570
,
"transaction_id": 1210086083636617200,
"transaction_amount": 16900
,
"transaction_id": 1210029898681119200,
"transaction_amount": 17240
,
"transaction_id": 1210085007415861000,
"transaction_amount": 1970
,
"transaction_id": 1210049037843518000,
"transaction_amount": 600
,
"transaction_id": 1210098702262983700,
"transaction_amount": 16860
,
"transaction_id": 1210064164940352500,
"transaction_amount": 2100
,
"transaction_id": 1210019858403530200,
"transaction_amount": 8380
,
"transaction_id": 1210034988617398500,
"transaction_amount": 11020
,
"transaction_id": 1210051731223911000,
"transaction_amount": 16790
,
"transaction_id": 1210009587405115100,
"transaction_amount": 10400
,
"transaction_id": 1210034874768594200,
"transaction_amount": 8810
,
"transaction_id": 1210075996428822300,
"transaction_amount": 400
,
"transaction_id": 1210025972523952600,
"transaction_amount": 400
,
"transaction_id": 1210074234210654700,
"transaction_amount": 100
,
"transaction_id": 1210018864025111600,
"transaction_amount": 100
,
"transaction_id": 1220046466945273000,
"transaction_amount": 8810
,
"transaction_id": 1220039368270792000,
"transaction_amount": 300
,
"transaction_id": 1220055686726592800,
"transaction_amount": 100
,
"transaction_id": 1230069608553236700,
"transaction_amount": 14130
,
"transaction_id": 1230049281258490400,
"transaction_amount": 700
,
"transaction_id": 1230076765081214200,
"transaction_amount": 450
,
"transaction_id": 1250016782944385000,
"transaction_amount": 11530
,
"transaction_id": 1250006322554225700,
"transaction_amount": 100
,
"transaction_id": 92,
"transaction_amount": 0
,
"transaction_id": 1260056740683290000,
"transaction_amount": 14680
,
"transaction_id": 1260000587430961200,
"transaction_amount": 850
,
"transaction_id": 1270009889953397500,
"transaction_amount": 10030
,
"transaction_id": 1270069561523095000,
"transaction_amount": 200
,
"transaction_id": 1270040728281643300,
"transaction_amount": 100
,
"transaction_id": 1280089283063438300,
"transaction_amount": 12130
,
"transaction_id": 1290018809331110700,
"transaction_amount": 9580
,
"transaction_id": 1290018531674695000,
"transaction_amount": 900
,
"transaction_id": 1300092766392609800,
"transaction_amount": 10690
,
"transaction_id": 1310032366810782200,
"transaction_amount": 6710
,
"transaction_id": 1320039283411532800,
"transaction_amount": 12930
,
"transaction_id": 1320089148138786600,
"transaction_amount": 7530
,
"transaction_id": 1310055191630637600,
"transaction_amount": 4820
,
"transaction_id": 1280027283326259000,
"transaction_amount": 3870
,
"transaction_id": 1270067351481026000,
"transaction_amount": 3760
,
"transaction_id": 1210086571828474600,
"transaction_amount": 4860
,
"transaction_id": 1210085983026421800,
"transaction_amount": 5410
,
"transaction_id": 10073356779506812,
"transaction_amount": 100
,
"transaction_id": 1210038099742391800,
"transaction_amount": 40
,
"transaction_id": 1210058115288376600,
"transaction_amount": 50
,
"transaction_id": 1210057437787905000,
"transaction_amount": 60
,
"transaction_id": 1210015214279570200,
"transaction_amount": 150
,
"transaction_id": 1210090907123932200,
"transaction_amount": 150
,
"transaction_id": 1210058639382087400,
"transaction_amount": 100
,
"transaction_id": 1320035737920998700,
"transaction_amount": 30
,
"transaction_id": 1310051492659050500,
"transaction_amount": 50
,
"transaction_id": 1300026927515702500,
"transaction_amount": 70
,
"transaction_id": 1290091342549902300,
"transaction_amount": 120
,
"transaction_id": 1320036065217106200,
"transaction_amount": 20
,
"transaction_id": 1210049711662146000,
"transaction_amount": 5690
,
"transaction_id": 1220017854791655400,
"transaction_amount": 700
,
"transaction_id": 1220078421115568000,
"transaction_amount": 1750
,
"transaction_id": 1220083395141670100,
"transaction_amount": 2960
,
"transaction_id": 1220030012017524700,
"transaction_amount": 1200
,
"transaction_id": 1220055092313143300,
"transaction_amount": 2150
,
"transaction_id": 1320090855530790400,
"transaction_amount": 1450
,
"transaction_id": 1320078099626775600,
"transaction_amount": 100
,
"transaction_id": 1310032467080039000,
"transaction_amount": 1650
,
"transaction_id": 1300045829832479500,
"transaction_amount": 560
,
"transaction_id": 1300013466915302400,
"transaction_amount": 14780
,
"transaction_id": 1300099794055548400,
"transaction_amount": 3520
,
"transaction_id": 1290072756390477000,
"transaction_amount": 9320
,
"transaction_id": 1280011366293507800,
"transaction_amount": 15040
,
"transaction_id": 1270012525410060800,
"transaction_amount": 10880
,
"transaction_id": 1260070060344229600,
"transaction_amount": 11880
,
"transaction_id": 1250032130776101600,
"transaction_amount": 13940
,
"transaction_id": 1240053126654134800,
"transaction_amount": 15490
,
"transaction_id": 1220060884920798200,
"transaction_amount": 9920
,
"transaction_id": 1210080012744477200,
"transaction_amount": 9740
,
"transaction_id": 1210052702048120000,
"transaction_amount": 4700
,
"transaction_id": 1320024252859871500,
"transaction_amount": 9650
,
"transaction_id": 1310094414527099400,
"transaction_amount": 6460
,
"transaction_id": 1310056800863432400,
"transaction_amount": 4070
,
"transaction_id": 1300014658278860800,
"transaction_amount": 6630
,
"transaction_id": 1270021744715695400,
"transaction_amount": 6560
,
"transaction_id": 1280078733105218300,
"transaction_amount": 100
,
"transaction_id": 1290031146040661800,
"transaction_amount": 800
,
"transaction_id": 1250043243699198700,
"transaction_amount": 1060
,
"transaction_id": 1260074577088146400,
"transaction_amount": 1550
,
"transaction_id": 1240079422127403800,
"transaction_amount": 1210
,
"transaction_id": 1230066909606585900,
"transaction_amount": 860
,
"transaction_id": 1220093146897979600,
"transaction_amount": 1950
,
"transaction_id": 660054591134494100,
"transaction_amount": 150
,
"transaction_id": 1210042973935054800,
"transaction_amount": 8530
,
"transaction_id": 1110005056867557900,
"transaction_amount": 12560
,
"transaction_id": 660065337505725200,
"transaction_amount": 2830
,
"transaction_id": 1210048633076725500,
"transaction_amount": 10080
,
"transaction_id": 660042962566730400,
"transaction_amount": 70
,
"transaction_id": 1220019457336956200,
"transaction_amount": 10480
,
"transaction_id": 1230049773618436600,
"transaction_amount": 8590
,
"transaction_id": 1230005784509563400,
"transaction_amount": 200
,
"transaction_id": 1240008046475998200,
"transaction_amount": 9570
,
"transaction_id": 1250099610071777800,
"transaction_amount": 8470
,
"transaction_id": 1260069686742661600,
"transaction_amount": 10080
,
"transaction_id": 1270011524033094100,
"transaction_amount": 7140
,
"transaction_id": 1270019972074937900,
"transaction_amount": 1650
,
"transaction_id": 1280073924483977200,
"transaction_amount": 9580
,
"transaction_id": 1290037476572969700,
"transaction_amount": 8560
,
"transaction_id": 1300082875136748000,
"transaction_amount": 9800
,
"transaction_id": 1310051688097257000,
"transaction_amount": 9170
,
"transaction_id": 1320098945494239000,
"transaction_amount": 10050
,
"transaction_id": 1320034084409829400,
"transaction_amount": 8820
,
"transaction_id": 1320054553717965600,
"transaction_amount": 200
,
"transaction_id": 1210049795161025000,
"transaction_amount": 11410
,
"transaction_id": 1220012186401975000,
"transaction_amount": 7160
,
"transaction_id": 1220041269729137200,
"transaction_amount": 7680
,
"transaction_id": 1230003827607650300,
"transaction_amount": 4240
,
"transaction_id": 1240075832098474000,
"transaction_amount": 6900
,
"transaction_id": 1250055044105082000,
"transaction_amount": 4650
,
"transaction_id": 1260079730354037200,
"transaction_amount": 5870
,
"transaction_id": 1270049938656869000,
"transaction_amount": 3270
,
"transaction_id": 1280024380770806800,
"transaction_amount": 4130
,
"transaction_id": 1290068008641940500,
"transaction_amount": 3840
,
"transaction_id": 1300003731064551000,
"transaction_amount": 4090
,
"transaction_id": 1310066167877878500,
"transaction_amount": 4270
,
"transaction_id": 1320013107002314000,
"transaction_amount": 10310
,
"transaction_id": 980036644687427800,
"transaction_amount": 80
,
"transaction_id": 1210076855150837500,
"transaction_amount": 3480
,
"transaction_id": 1210067195942165000,
"transaction_amount": 4800
,
"transaction_id": 1220061444025905400,
"transaction_amount": 4160
,
"transaction_id": 1220078616596051700,
"transaction_amount": 3570
,
"transaction_id": 1230067056292351500,
"transaction_amount": 1970
,
"transaction_id": 1230067494033167400,
"transaction_amount": 2420
,
"transaction_id": 1240045245048555000,
"transaction_amount": 2520
,
"transaction_id": 1240072489837079600,
"transaction_amount": 8390
,
"transaction_id": 1250014234261423400,
"transaction_amount": 2770
,
"transaction_id": 1290096273215510300,
"transaction_amount": 810
,
"transaction_id": 1290059061625999000,
"transaction_amount": 2260
,
"transaction_id": 1300037633594074000,
"transaction_amount": 3860
,
"transaction_id": 1300097680810671000,
"transaction_amount": 2750
,
"transaction_id": 1310044781273419000,
"transaction_amount": 3170
,
"transaction_id": 1310079256318178600,
"transaction_amount": 3450
,
"transaction_id": 1320045676617182000,
"transaction_amount": 3430
,
"transaction_id": 1320028368751801300,
"transaction_amount": 7570
,
"transaction_id": 1320060246343053000,
"transaction_amount": 5110
,
"transaction_id": 1250036598503759600,
"transaction_amount": 12410
,
"transaction_id": 1250069488455480300,
"transaction_amount": 6920
,
"transaction_id": 1290068027769932800,
"transaction_amount": 5650
,
"transaction_id": 1210060048671908400,
"transaction_amount": 3380
,
"transaction_id": 1210034313810243800,
"transaction_amount": 1160
,
"transaction_id": 1220020357549118700,
"transaction_amount": 610
,
"transaction_id": 1230094506505520000,
"transaction_amount": 3160
,
"transaction_id": 30,
"transaction_amount": 10
,
"transaction_id": 31,
"transaction_amount": 10
,
"transaction_id": 32,
"transaction_amount": 10
,
"transaction_id": 33,
"transaction_amount": 10
,
"transaction_id": 34,
"transaction_amount": 10
,
"transaction_id": 35,
"transaction_amount": 10
,
"transaction_id": 36,
"transaction_amount": 10
,
"transaction_id": 37,
"transaction_amount": 10
,
"transaction_id": 660060829304660200,
"transaction_amount": 80
,
"transaction_id": 10020030573503408,
"transaction_amount": 7320
,
"transaction_id": 930040052470111600,
"transaction_amount": 15680
,
"transaction_id": 38,
"transaction_amount": 10
,
"transaction_id": 39,
"transaction_amount": 10
,
"transaction_id": 40,
"transaction_amount": 10
,
"transaction_id": 41,
"transaction_amount": 10
,
"transaction_id": 1210095979140740900,
"transaction_amount": 500
,
"transaction_id": 10045419846093900,
"transaction_amount": 700
,
"transaction_id": 930096692414933100,
"transaction_amount": 1210
,
"transaction_id": 660067907945384100,
"transaction_amount": 6350
,
"transaction_id": 980016732844407800,
"transaction_amount": 7410
,
"transaction_id": 990026098619609200,
"transaction_amount": 11980
,
"transaction_id": 660055069426220000,
"transaction_amount": 11880
,
"transaction_id": 48,
"transaction_amount": 10
,
"transaction_id": 49,
"transaction_amount": 10
,
"transaction_id": 660016665040405800,
"transaction_amount": 10460
]
App.js
import React from "react";
import useQuery from "@apollo/react-hooks";
import gql from "apollo-boost";
const GET_USERS = gql`
query
account_ledger
transaction_id
transaction_amount
`;
const App = () =>
const loading, error, data = useQuery(GET_USERS,
variables: first: 10 ,
);
if (loading) return <p>Loading...</p>;
if (error)
console.log("error graphql ", error);
return <p>`Error: $error.message`</p>;
console.log(data);
return (
<div className="App">
<p>Hello, World</p>
</div>
);
;
export default App;
index.js
import React from "react";
import ReactDOM from "react-dom";
import ApolloProvider from "@apollo/react-hooks";
import ApolloClient from "apollo-boost";
import InMemoryCache from "apollo-cache-inmemory";
import App from "./App";
// Initialize a new cache
const cache = new InMemoryCache();
// Creating the client
const client = new ApolloClient(
uri: "http://localhost:8080/v1/graphql",
cache,
);
ReactDOM.render(
<ApolloProvider client=client>
<App />
</ApolloProvider>,
document.getElementById("root")
);
【问题讨论】:
您在 useQuery 中使用了变量,这就是您收到错误消息的原因。请参阅 appolo doc 中的示例。您需要两个从useQuery
中删除第二个参数或更改您的查询
【参考方案1】:
您必须执行以下任一操作。这些是
1.当您请求服务器时,您必须删除变量字段。 正如@antoineso 建议的那样。
const loading, error, data = useQuery(GET_USERS, variables: first: 10 , );
把上面的代码改成
const loading, error, data = useQuery(GET_USERS);
在这种情况下,您将从服务器获取所有分类帐信息。
但是,如果您想限制传入客户端的数据量,则必须使用 Hasura 的限制和偏移量。像这样
2。将您的查询定义更改为这样的内容。
const GET_USERS = gql`query ledger_query($limit: Int!, $offset:Int!) account_ledger(limit: $limit, offset: $offset) transaction_id transaction_amount `;
现在,您可以像这样调用您的 graphql 服务器 (Hasura)。
const loading, error, data = useQuery(GET_USERS, variables: limit: 10, offset: 0 , );
做完上述操作后,服务器应该返回前10个account_ledger
信息。
如果你不清楚,问我。
【讨论】:
以上是关于GraphQL:variableValues 中的意外变量:首先在 args 中不使用变量时的主要内容,如果未能解决你的问题,请参考以下文章
graphql-java 中的 GraphQL 模式定义语言支持
无法从模式拼接中的另一个 schema.graphql 文件中的其他 schema.graphql 文件访问共享模式