0

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 Answers1

1

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.

Safwan Parkar
  • 161
  • 10
  • thank you for your reply i understand how to avoid a futur mistake but right now, the problem is the same even when i try with just on argument, i have the same error: message: "Field "databasesListDocuments" argument "queries" requires type String, found field_1." message: "Field "databasesListDocuments" argument "queries" requires type String, found a." – klopstock Jun 16 '23 at 15:11
  • another observation when i try to reach the backend end point '.../v1/graphql', on localhost or in cloud (digital ocean), i have this json on the web page: {"message":"Param \"query\" is not optional.","code":400,"type":"general_argument_invalid","version":"1.2.1"} – klopstock Jun 16 '23 at 15:20
  • I'm trying to figure out the issue by reproducing the issue on my end. Can you check if `GraphQL` is enabled in your project settings? [Projects -> My-Project-Name -> Settings -> Scroll down to Services] – Safwan Parkar Jun 17 '23 at 06:20
  • @klopstock I have just edited my answer with code that worked for me on the Web SDK. Please have a look and let me know if there's still any issues :) – Safwan Parkar Jun 17 '23 at 07:18
  • Again thanks for your time and efforts. Yes graphql is enabled in the settings Finally the query works with ' instead of ", this reminds me a similar behaviour with psgl, but the error message was clear. Now i am back to my original question, how to make combined queries ? When i try this: queries: ["equal('field_1', 'a')", "equal('field_2', 'b')"]. I get this error message: {message: 'Index not found: field_1, field_2'} – klopstock Jun 17 '23 at 11:18
  • So when i query the fields separately, it works, when combined, the query fails Even if the doc does indicate this: queries: ["equal(\"title\", [\"Avatar\", \"Lord of the Rings\"])", "greaterThan(\"year\", 1999)"] – klopstock Jun 17 '23 at 11:21
  • I have tried with an integer field and another query type: queries: ["equal('field_1', 'a')", "greaterThan('counter', 0)"] Same problem, it is not related to the query type – klopstock Jun 17 '23 at 13:29
  • The error `index not found` suggests that you don't have an index for the two fields. If you're querying the two fields separately, one index per field will work. I'm assuming you'll need to create an index with both the fields to query at the same time. Hope that makes sense! @klopstock – Safwan Parkar Jun 18 '23 at 04:33
  • thank you, Safwan Parkar very kind of you keeping helping. i created a combined index of all the fields i needed to query, now it works. – klopstock Jun 19 '23 at 13:14
  • The doc needs few updates to explain these requirements. One more last thing Safwan Parkar, as i asked in my original question. The expression ["equal('field_1', 'a')", "equal('field_2', 'b')" works as 'AND' query How do i make an 'OR' query ? – klopstock Jun 19 '23 at 13:43
  • @klopstock apologies for the late reply. The `OR` query is a feature in the works. See github issue here: [https://github.com/appwrite/appwrite/issues/2740](https://github.com/appwrite/appwrite/issues/2740) – Safwan Parkar Jul 02 '23 at 10:36