Back

[SOLVED] Starting with GraphQL

  • 0
  • Web
loup
28 Jul, 2023, 10:30

How do u make multiple request inside one graphql request with Appwrite and NextJS, there is my code :

TypeScript
const q = (await graphql.query([
                {
                    query: `query {
                        databasesListDocuments(
                            databaseId: "${String(process.env.NEXT_PUBLIC_APPWRITE_DATABASE_USERS)}",
                            collectionId: "${String(process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_MOVIE_LIKED)}",
                            queries: ["equal('userId', ${userId})", "equal('movieId', ${movieId})"]
                        ) {
                            total
                            documents {
                                _id
                                _collectionId
                                _databaseId
                                _createdAt
                                _updatedAt
                                _permissions
                                data
                            }
                        }
                    }`
                }
            ]))

Here I only take the likes from a specific user and specific movie. Now I also want to get the "asSeen" state from another collection, how can I do ?

TL;DR
The user is trying to make multiple requests using GraphQL in Appwrite and NextJS but is running into the error "Max query complexity should be 250 but got 450." They are using the `databasesListDocuments` method call multiple times and are unsure how to include multiple requests in one GraphQL query. The user also mentions wanting to add custom names for each call. Solution: The user can use query batching to include multiple requests in one GraphQL query. They can separate each request with an operation name, which will act as a custom name for that specific call. The user can also specify what fields they want to include in the response
loup
28 Jul, 2023, 10:53

I saw some exmaple like this :

TypeScript
query getAuthors {
  authors {
    firstName
    lastName
  }
}

// Query to get all posts
query getPosts {
  posts {
    id
    title
    exert
  }
}

But how add custom name from each call ? like "getAuthors", "getPosts"

loup
28 Jul, 2023, 11:05

I dont understand the utility of "operationName" if I can call multiple operationName :/

loup
28 Jul, 2023, 11:13

from "Appwrite GraphQL Overview" (youtube video) I also dont understand how he got custom name like this

Drake
28 Jul, 2023, 18:55

what do you mean? you can put whatever you like

Drake
28 Jul, 2023, 18:56

these are not custom names. these are method calls

Drake
28 Jul, 2023, 18:57
loup
28 Jul, 2023, 19:49

well there is something I dont understand ahah, how can I do multiple listDocument (for getting likes and "asSeen" state for example) :

loup
28 Jul, 2023, 19:49
TypeScript
const q = await graphql.query(
                {
                    query: `
                        query GetAction {
                            databasesListDocuments(
                                databaseId: "${String(process.env.NEXT_PUBLIC_APPWRITE_DATABASE_USERS)}",
                                collectionId: "${String(process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_MOVIE_LIKED)}",
                                queries: ["equal('userId', ${userId})", "equal('movieId', ${movieId})"]
                            ) {
                                total
                                documents {
                                    _id
                                    _collectionId
                                    _databaseId
                                    _createdAt
                                    _updatedAt
                                    _permissions
                                    data
                                }
                            }
                            databasesListDocuments(
                                databaseId: "${String(process.env.NEXT_PUBLIC_APPWRITE_DATABASE_USERS)}",
                                collectionId: "${String(process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_MOVIE_WATCHED)}",
                                queries: ["equal('userId', ${userId})", "equal('movieId', ${movieId})"]
                            ) {
                                total
                                documents {
                                    _id
                                    _collectionId
                                    _databaseId
                                    _createdAt
                                    _updatedAt
                                    _permissions
                                    data
                                }
                            }
                        }
                        `,
                }
            )
console.log('graphql', q)
loup
28 Jul, 2023, 19:50

because in my code ive got this error : "Fields "databasesListDocuments" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional."

Drake
28 Jul, 2023, 19:51
loup
28 Jul, 2023, 19:52

Ive got this error because I use two time databasesListDocumentsbut ive no other choice :/

Drake
28 Jul, 2023, 19:53

did you look at what i linked?

loup
28 Jul, 2023, 19:55

Query batching ? yes but in the example they use method call "accountGet" and "localeGet" but no databaseslistdocuments

loup
28 Jul, 2023, 19:55

I guess there is something I dont understand

Drake
28 Jul, 2023, 19:56

i meant this...

loup
28 Jul, 2023, 19:59

yeah they add custom name for each call like this :

TypeScript
likes: databasesListDocuments(
                                databaseId: "${String(process.env.NEXT_PUBLIC_APPWRITE_DATABASE_USERS)}",
                                collectionId: "${String(process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_MOVIE_LIKED)}",
                                queries: ["equal('userId', ${userId})", "equal('movieId', ${movieId})"]
                            ) {
                                total
                                documents {
                                    _id
                                    _collectionId
                                    _databaseId
                                    _createdAt
                                    _updatedAt
                                    _permissions
                                    data
                                }
                            }
                            watch: databasesListDocuments(
                                databaseId: "${String(process.env.NEXT_PUBLIC_APPWRITE_DATABASE_USERS)}",
                                collectionId: "${String(process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_MOVIE_WATCHED)}",
                                queries: ["equal('userId', ${userId})", "equal('movieId', ${movieId})"]
                            ) {
                                total
                                documents {
                                    _id
                                    _collectionId
                                    _databaseId
                                    _createdAt
                                    _updatedAt
                                    _permissions
                                    data
                                }
                            }

but when i do that im getting Max query complexity should be 250 but got 450 😢 The only way to dont have this error is delete some output result (like _collectionId, _createdAt, etc...)

loup
28 Jul, 2023, 19:59

Well I guess its okay I dont need all these data

loup
28 Jul, 2023, 20:00

But why there is a limit ? ive weed need a lot of result how can we do it ?

Drake
28 Jul, 2023, 20:00

you can also modify the complexity: https://appwrite.io/docs/environment-variables#graphql

loup
28 Jul, 2023, 20:00

ohhh mb ahah

loup
29 Jul, 2023, 20:20

[SOLVED] Starting with GraphQL

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