Back

GraphQL: get a subset of attributes from documents

  • 0
  • Databases
  • GraphQL
  • Cloud
Chinmaya
19 Jun, 2024, 19:05
  1. I'm trying to query for a subset of attributes from documents but failing. https://discord.com/channels/564160730845151244/1190627623907364965 This question about using queries in GraphQL to filter the database attributes is not answered and the docs around that are fuzzy at best. Below is the JSON request body I'm using for this request.
  2. Also, for the Appwrite cloud, is there a URL for the GraphQL Explorer? I see some tutorials around that for local instances, but no reference for cloud.
TypeScript
[
    {
        "query": "query($databaseId: String!, $collectionId: String!) { databasesListDocuments( databaseId: $databaseId, collectionId: $collectionId, queries: [ {'method': 'select', 'attributes': ['first_name', 'last_name'] } ] ) { total documents { _id _collectionId _databaseId _createdAt _updatedAt _permissions data } }}",
        "variables": {
            "databaseId": "onlinestore-xyz",
            "collectionId": "customers"
        }
    }
]
TL;DR
Developers are struggling to query for a subset of attributes from documents in GraphQL with Appwrite. The documentation is lacking on this topic. Unfortunately, Appwrite's GraphQL implementation does not support selecting attributes. Setting up GraphQL Explorer for Appwrite cloud is possible locally but lacks references/docs. Solution: In a typical GraphQL API, you would specify the attributes you want returned without needing select queries. However, Appwrite does not support selecting attributes directly through GraphQL.
Steven
19 Jun, 2024, 19:13

Also, for the Appwrite cloud, is there a URL for the GraphQL Explorer? You can set up the GraphQL explorer locally and point it to Cloud. You won't get the references/docs, though as those are disabled on cloud

Chinmaya
19 Jun, 2024, 19:14

Is this what you are talking about: https://github.com/appwrite/docker-altair ?

Steven
19 Jun, 2024, 19:14

oh we don't support selecting attributes for our graphql implementation

Steven
19 Jun, 2024, 19:15

typically, a graphql api won't require passing select queries. you would just specify which attributes you want returned

Steven
19 Jun, 2024, 19:15

you actually might be able to get the lmited attributes returned with that select query...

Chinmaya
19 Jun, 2024, 19:15

Right, how can I do that?

Steven
19 Jun, 2024, 19:15

what's happening though?

Chinmaya
19 Jun, 2024, 19:17

Well, I feel that the queries is being ignored altogether and it always returns the full document. I even tried to list the attribute names instead of the data in the document attribute list.

Steven
19 Jun, 2024, 19:18

one thing that could be a problem is:

TypeScript
{'method': 'select', 'attributes': ['first_name', 'last_name'] }

this isn't valid JSON. and, it should be stringified

Chinmaya
19 Jun, 2024, 19:20

By the way, I'm not even sure if the attributes term can be used or not, I just guessed based on the examples here: https://appwrite.io/docs/products/databases/queries#building-queries. Like I said, the documentation around this is sparse.

Steven
19 Jun, 2024, 19:21

ahhh good point. it should be values

Chinmaya
19 Jun, 2024, 19:23

Same result with queries: [ \"{\"method\": \"select\", \"values\": [\"first_name\", \"last_name\"] }\"]

Steven
19 Jun, 2024, 19:56

This worked for me:

Steven
19 Jun, 2024, 19:56
TypeScript
{
  databasesListDocuments(
    databaseId: "default"
    collectionId: "usernames"
    queries: ["{\"method\":\"select\",\"values\":[\"username\"]}"]
  ) {
    total
    documents {
      data
      _id
      _collectionId
      _databaseId
      _createdAt
      _updatedAt
      _permissions
    }
  }
}
Steven
19 Jun, 2024, 19:58

the raw payload ended up being:

TypeScript
{
  "query": "\n{\n  databasesListDocuments(\n    databaseId: \"default\"\n    collectionId: \"usernames\"\n    queries: [\"{\\\"method\\\":\\\"select\\\",\\\"values\\\":[\\\"username\\\"]}\"]\n  ) {\n    total\n    documents {\n      data\n      _id\n      _collectionId\n      _databaseId\n      _createdAt\n      _updatedAt\n      _permissions\n    }\n  }\n}\n",
  "variables": {},
  "operationName": null
}
Chinmaya
19 Jun, 2024, 19:58

Cool. Let me try.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more