使用 React Native 创建 GraphQL 搜索查询

发布于 2025-01-16 10:31:55 字数 886 浏览 4 评论 0原文

我正在尝试实现一个搜索查询来帮助我按名称或类别进行搜索。我只需要后端部分的帮助,即设置解析器和索引,我不确定我做得是否正确

Resolver.js

//search for single user
    user: async ({_id}) => {
        try {
            const foundUser = await User.findOne({_id: _id})
            return foundUser;
        } catch (err){
            throw err;
        }
    },
//search for all users
    users: async () => {
        try{
            const users = await User.find()
            return users;
        } catch(err) {
            throw err;
        }
    },

我希望能够搜索具有“Star”角色的所有用户并执行以下操作仅搜索这些用户。不太确定从这里去哪里 索引.js

type User {
    _id: ID!
    username: String
    password: String
    name: String
    role: String
    enterCategory: String
}
//not sure if search is right
type RootQuery {
    user(_id: ID!): User!
    users: [User!]!
    searchStars(search: String): [Users]!
}

I am trying to implement a search query to help me search by name or category. I need help with only the backend part of it which is setting up the resolver and the index which im not sure if I m doing it right

Resolver.js

//search for single user
    user: async ({_id}) => {
        try {
            const foundUser = await User.findOne({_id: _id})
            return foundUser;
        } catch (err){
            throw err;
        }
    },
//search for all users
    users: async () => {
        try{
            const users = await User.find()
            return users;
        } catch(err) {
            throw err;
        }
    },

I want to be able to search all users with the role "Star" and do the search only on those users. Not really sure where to go from here
Index.js

type User {
    _id: ID!
    username: String
    password: String
    name: String
    role: String
    enterCategory: String
}
//not sure if search is right
type RootQuery {
    user(_id: ID!): User!
    users: [User!]!
    searchStars(search: String): [Users]!
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

旧城烟雨 2025-01-23 10:31:55

Index.js

    searchCategories(search: String): [User]!

解析器

 searchCategories: async ({ search }) => {
        try {
            const users = await User.find()
            return users.filter(x => x.enterCat === search);
        } catch (err) {
            throw err;
        }
    }

Index.js

    searchCategories(search: String): [User]!

Resolver

 searchCategories: async ({ search }) => {
        try {
            const users = await User.find()
            return users.filter(x => x.enterCat === search);
        } catch (err) {
            throw err;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文