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
  • I am running v1.2.1
    – klopstock
    Commented Jun 15, 2023 at 15:53

1 Answer 1

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.

11
  • 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
    Commented Jun 16, 2023 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
    Commented Jun 16, 2023 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] Commented Jun 17, 2023 at 6: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 :) Commented Jun 17, 2023 at 7: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
    Commented Jun 17, 2023 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.