错误:类型的期望值' string!'但是价值不确定

发布于 2025-01-30 03:58:01 字数 1878 浏览 6 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千里故人稀 2025-02-06 03:58:01

您的代码中有几件事。

在您的错误涉及的情况下,您对operationStore的调用格式不佳。第一个参数应该是您的gql构型查询(它是),第二个参数期望包含 ash al your变量的对象,第三个参数期望一个选项对象(request> requestpolicy等)。然而,您尝试在两个单独的参数(第二和第四)中传递变量。因此,您呼叫操作store应该是:

const allPosts = operationStore(
    postsQuery,
    { size: 100, email: '[email protected]' },
    { requestPolicy: 'network-only' },
)

这将解决您的初始错误。但是...

此外,您的客户端查询与您的架构定义不符。 _size_cursor变量不存在于您的模式中,并且您在请求中添加了不匹配的外部data键用户和发布架构类型。您的查询应该是:

const postsQuery = gql`
    query GetAllPosts($email: String!) {
        GetPostByUsersEmail(email: $email) {
            email
            username
            posts {
                title
            }
        }
    }
`

因此,您的最终修订操作store应为:

const allPosts = operationStore(
    postsQuery,
    { email: '[email protected]' },
    { requestPolicy: 'network-only' },
)

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 to operationStore should be:

const allPosts = operationStore(
    postsQuery,
    { size: 100, email: '[email protected]' },
    { requestPolicy: 'network-only' },
)

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 extraneous data keys in your request that do not match your User and Post schema types. Your query should be:

const postsQuery = gql`
    query GetAllPosts($email: String!) {
        GetPostByUsersEmail(email: $email) {
            email
            username
            posts {
                title
            }
        }
    }
`

And thus your final, amended operationStore call should be:

const allPosts = operationStore(
    postsQuery,
    { email: '[email protected]' },
    { requestPolicy: 'network-only' },
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文