使用Web SDK通过GraphQL向Appwrite进行多选查询。

huangapple go评论55阅读模式
英文:

make multiple selection query to appwrite with graphql from web sdk

问题

使用Web SDK中的GraphQL(在本地主机上),我想执行两个请求:

  1. 获取所有文档,其中field_1 = "a" 且 field_2 = "b"

  2. 获取所有文档,其中field_1 = "a" 或 field_2 = "b"

根据文档,我编写了以下代码:

const { Client, Databases, Query, Graphql } = Appwrite;
const client = new Client()
    .setEndpoint("http://localhost:8080/v1/graphql") // 您的API端点
    .setProject(project_id); // 您的项目ID

const graphql = new Graphql(client);

const query = graphql.query({
    query: `query {
                    databasesListDocuments(
                        databaseId: "${db_id}",
                        collectionId: "${collectionId}",
                        queries: ["equal(\"field_1\", \"a\"), equal(\"field_2\", \"b\")"]
                    ) {
                        total
                        documents {
                            _id
                            data
                        }
                    }
                }`
})

结果,我得到了一个错误数组,其中包含如下消息:

message: "Field "databasesListDocuments" argument "queries" requires type String, found field_1."

message: "Field "databasesListDocuments" argument "queries" requires type String, found a."

问题是什么?

我该如何执行这个请求?

英文:

I have a list of documents, with field_1, field_2, ...

Using graphql from the web skd (on localhost), i want to perform two requests

1.get all documents as field_1 = "a" AND field_2 = "b"

2.get all documents as field_1 = "a" OR field_2 = "b"

Folowing the doc, i wrote this code:

`const {Client, Databases, Query, Graphql} =Appwrite;
const client = new Client()
.setEndpoint("http://localhost:8080/v1/graphql") // Your API Endpoint
.setProject(project_id) // Your project ID

const graphql = new Graphql(client);

const query = graphql.query({
query: query {
databasesListDocuments(
databaseId: "${db_id}",
collectionId: "${collectionId}",
queries: ["equal(\"field_1\", \"a\"), equal(\"field_2\", \"b\")"]
) {
total
documents {
_id
data
}
}
}

})`

As result i have an array of error, with messages like:

message: "Field "databasesListDocuments" argument "queries" requires type String, found field_1."

message: "Field "databasesListDocuments" argument "queries" requires type String, found a."

...

What is the problem ?

How can i perform this request ?

答案1

得分: 1

你的两个 equal 查询都在同一个字符串中。queries 参数应该接受一个查询字符串的数组,而不是一个包含所有查询的字符串。你所做的是:

queries: ["equal(\"field_1\", \"a\"), equal(\"field_2\", \"b\")"]

你应该这样做:

queries: ["equal(\"field_1\", \"a\")", "equal(\"field_2\", \"b\")"]

如果这样做可以正常工作,请告诉我!

编辑 #1
好的,在尝试了 GraphQL API 后,我成功让它工作了:

const mutation = graphql.mutation({
    query: `query {
        databasesListDocuments(
            databaseId: "[YOUR_DATABASE_ID]",
            collectionId: "[YOUR_COLLECTION_ID]",
            queries: ["equal('field_1', 'a')"]

        ) {
            total
            documents {
                _id
                data
            }
        }
    }`
});

mutation.then(response => {
    console.log(response.data.databasesListDocuments);
}).catch(error => {
    console.log(error);
});

我收到的输出是:

{
  total: 1,
  documents: [
    {
      _id: '648d58423fd043be6432',
      data: '{"field_1":"a","field_2":"b","$permissions":null}'
    }
  ]
}

当我使用转义的双引号 (") 时,我遇到了与 OP 相同的错误。切换到单引号 (') 修复了这个问题。

英文:

Both of your equal queries are in the same string. The queries param takes in an array of query strings, not just one string with all queries. What you have done is:

queries: ["equal(\"field_1\", \"a\"), equal(\"field_2\", \"b\")"]

What you should do is:

queries: ["equal(\"field_1\", \"a\")", "equal(\"field_2\", \"b\")"]

Let me know if this works!


EDIT #1

Okay so after playing around with the GraphQL API, I was able to get this to work:

const mutation = graphql.mutation({
    query: `query {
        databasesListDocuments(
            databaseId: "[YOUR_DATABASE_ID]",
            collectionId: "[YOUR_COLLECTION_ID]",
            queries: ["equal('field_1', 'a')"]

        ) {
            total
            documents {
                _id
                data
            }
        }
    }`
});

mutation.then(response => {
    console.log(response.data.databasesListDocuments);
}).catch(error => {
    console.log(error);
});

Output I recevied:

{
  total: 1,
  documents: [
    {
      _id: '648d58423fd043be6432',
      data: '{"field_1":"a","field_2":"b","$permissions":null}'
    }
  ]
}

I was getting the same error as OP when using escaped double-quotes ("). Switching to single quotes (') fixed the issue.

huangapple
  • 本文由 发表于 2023年6月15日 23:47:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定