Back

How to build queries and send to the server via api

  • 0
  • Self Hosted
  • Tools
  • Web
  • Databases
Suiii
26 Mar, 2024, 15:21

I have the api route in nextjs. then I connect via client app. what is best way to send the quries to the api route. I want to build the query string using the client sdk. is this possible

TL;DR
Developers want to learn how to send queries to the server via API using the node-appwrite SDK. Solution provided: you can create a function to generate a query list in the client and send it to the server's API route.
Kenny
26 Mar, 2024, 15:25

So you're wanting to make a rest call, and add queries to it?

Kenny
26 Mar, 2024, 15:26

You can do something like this if that is the case

TypeScript
export function generateQueryList(queries: Query[]) {
  const queryList: string[] = [];

  queries.forEach((x) => {
    queryList.push(`queries[]=${x}`);
  });

  return queryList.join("&");
}

And you'd use it like this.

TypeScript
  /**
   * Retrieves a list of documents from a specific collection.
   *
   * @template {T} - The type of the documents to retrieve.
   * @param {string} collectionId - The ID of the collection to retrieve documents from.
   * @returns A promise that resolves to an array of documents of type T.
   */
  async list<T extends Models.Document>(
    collectionId: string,
    queries: string[] = [],
  ) {
    const queryList = generateQueryList(queries);

    const url = `${ENDPOINT}/databases/${DATABASE_ID}/collections/${collectionId}/documents${queries.length > 0 ? `?${queryList.toString()}` : ""}`;

    const response = await fetch(url, {
      headers: {
        "x-appwrite-project": PROJECT_ID,
      },
      cache: "no-store",
    });

    const result: T = await response.json();

    return result;
  },
};
Suiii
26 Mar, 2024, 15:27

@Kenny yes something like that. then in server, how to parse it back

Kenny
26 Mar, 2024, 15:28

Not sure I understand what you mean by parse it back /:

Suiii
26 Mar, 2024, 15:30

i'm not using the rest to appwrite directly. i will use node-appwrite sdk in server as normal. jusst i need to send rest from client via api

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