错误:类型的期望值' string!'但是价值不确定
我在Stelve Kit应用程序中使用了此代码。 我想将电子邮件发送到来自GraphQL的查询数据。 我尝试使用此代码,但有错误。
<script lang="js">
import { gql, operationStore, query, setClient } from '@urql/svelte';
import client from '../client';
setClient(client);
import { userSession } from '../store.js';
let user;
userSession.subscribe((val) => {
user = val;
});
console.log("user = ");
console.log(user.id);
console.log(user.email);
const postsQuery = gql`
query GetAllPosts($size: Int!, $cursor: String, $email: String!) {
GetPostByUsersEmail(_size: $size, _cursor: $cursor, email: $email) {
data {
email
username
posts {
data {
title
}
}
}
}
}
`
const allPosts = operationStore(
postsQuery,
{ size: 100 },
{ requestPolicy: 'network-only' },
{ email: '[email protected]' }
)
query(allPosts);
console.log(allPosts)
</script>
使用模式,
type User @auth(primary: "email") {
username: String!
email: String!
posts: [Post!] @relation
}
type Post @protected(membership: "User", rule: ["read", "write", "create"]) {
title: String!
content: String!
author: User!
}
type Query {
listPosts: [Post]
users(username: String!): [User]
posts(title: String!): [Post]
GetPostByUsersEmail(email: String!): [User]
}
我认为一切都很好,但是 AllPosts的数据并没有附加 并获得了 console.log(AllPosts)的错误表格数据
GraphQLError: Variable '$email' expected value of type 'String!' but value is undefined.
有人可以帮忙吗?
I used this code in my stelve kit app.
I want to sent the email to query data from graphql.
I try to use this code but it got error.
<script lang="js">
import { gql, operationStore, query, setClient } from '@urql/svelte';
import client from '../client';
setClient(client);
import { userSession } from '../store.js';
let user;
userSession.subscribe((val) => {
user = val;
});
console.log("user = ");
console.log(user.id);
console.log(user.email);
const postsQuery = gql`
query GetAllPosts($size: Int!, $cursor: String, $email: String!) {
GetPostByUsersEmail(_size: $size, _cursor: $cursor, email: $email) {
data {
email
username
posts {
data {
title
}
}
}
}
}
`
const allPosts = operationStore(
postsQuery,
{ size: 100 },
{ requestPolicy: 'network-only' },
{ email: '[email protected]' }
)
query(allPosts);
console.log(allPosts)
</script>
with schema
type User @auth(primary: "email") {
username: String!
email: String!
posts: [Post!] @relation
}
type Post @protected(membership: "User", rule: ["read", "write", "create"]) {
title: String!
content: String!
author: User!
}
type Query {
listPosts: [Post]
users(username: String!): [User]
posts(title: String!): [Post]
GetPostByUsersEmail(email: String!): [User]
}
I think everthing is good but the data of allPosts is not append
and got this error form data of console.log(allPosts)
GraphQLError: Variable '$email' expected value of type 'String!' but value is undefined.
Can anyone help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有几件事。
在您的错误涉及的情况下,您对
operationStore
的调用格式不佳。第一个参数应该是您的gql构型查询(它是),第二个参数期望包含 ash al your变量的对象,第三个参数期望一个选项对象(request> requestpolicy
等)。然而,您尝试在两个单独的参数(第二和第四)中传递变量。因此,您呼叫操作store
应该是:这将解决您的初始错误。但是...
此外,您的客户端查询与您的架构定义不符。
_size
和_cursor
变量不存在于您的模式中,并且您在请求中添加了不匹配的外部
键用户和data
键发布
架构类型。您的查询应该是:因此,您的最终修订
操作store
应为:There are several things wrong in your code.
Where your error is concerned, your call to
operationStore
is badly formatted. The first argument should be your gql-formatted query (which it is), the second argument expects an object containing all your variables, and the third argument expects an options object (requestPolicy
, etc.). Yet you attempt to pass your variables in two separate arguments (2nd and 4th). So your call tooperationStore
should be:This would fix your initial error. However...
In addition, your client-side query does not match your schema definition. The
_size
and_cursor
variables do not exist in your schema, and you added extraneousdata
keys in your request that do not match yourUser
andPost
schema types. Your query should be:And thus your final, amended
operationStore
call should be: