docs icon indicating copy to clipboard operation
docs copied to clipboard

There is no guide anywhere supporting Filtered Queries

Open qwikag opened this issue 9 months ago • 1 comments

I have searched all over the internet, especially in AWS Docs. I cannot for the life of me find the proper syntax for doing filtered queries.

I finally asked Chat GPT and it gave me a non-working answer I then offered some similar to this and CHATGPT corrected it for me and now it is working.

But the problem is that there is no documentation. of proper node.js or JS extensive examples of queries and mutations. There is a need for an examples page with nothing but examples of code.

Here is my working code: which I could not find a good working example of.

const { Sha256 } = require("@aws-crypto/sha256-js");
const { defaultProvider } = require("@aws-sdk/credential-provider-node");
const { SignatureV4 } = require("@aws-sdk/signature-v4");
const { HttpRequest } = require("@aws-sdk/protocol-http");
const { default: fetch, Request } = require("node-fetch");

const GRAPHQL_ENDPOINT = process.env.API_MYAPP_GRAPHQLAPIENDPOINTOUTPUT;
const AWS_REGION = process.env.AWS_REGION || 'ap-southeast-2';

exports.handler = async (event) => {

  // Constants
  const currentDateTime = new Date().toISOString();
  const user = event.request.userAttributes;
  const endpoint = new URL(GRAPHQL_ENDPOINT);
  const signer = new SignatureV4({
    credentials: defaultProvider(),
    region: AWS_REGION,
    service: 'appsync',
    sha256: Sha256
  });
  const queryPeople = /* GraphQL */ `query LIST_PEOPLE($Email: String!) {
    listPeople(filter: { Email: { eq: $Email } }) {
      items {
        id Email Username
      }
    }
  }`;
    const queryPeopleHttpRequest = new HttpRequest({
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        host: endpoint.host
      },
      hostname: endpoint.host,
      path: endpoint.pathname,
      body: JSON.stringify({ query: queryPeople, variables: { Email: user.email } })
    });

    const signedQueryPeopleHttpRequest = await signer.sign(queryPeopleHttpRequest);
    const queryPeopleRequest = new Request(GRAPHQL_ENDPOINT, signedQueryPeopleHttpRequest);
    const queryPeopleResponse = await fetch(queryPeopleRequest);
    const queryPeopleBody = await queryPeopleResponse.json();

And I still do not know if it is correct syntax.

This particular syntax was not present anywhere, there is nothing that looks like this with LIST_PEOPLE and listPeople, with 2 sets of variables. And this could be my knowledge gap but regardless this is a document gap.:

  const queryPeople = /* GraphQL */ `query LIST_PEOPLE($Email: String!) {
    listPeople(filter: { Email: { eq: $Email } }) {
      items {
        id Email Username
      }
    }
  }`;

https://docs.amplify.aws/guides/functions/graphql-from-lambda/q/platform/js/#commonjs

The Items of syntax presented need to show where they are found, in what file. because there is the schema mutations and queries files there is javascript REACT code there is Node.JS. It is all jumbled up. this section does not help me: https://docs.amplify.aws/lib/graphqlapi/query-data/q/platform/js/#filtered-and-paginated-queries

Also...it is far different from the way other platforms use/write GraphQL high level language. So it is very hard to get the right setup.

when I go to this page: The syntax on this page is Javascript but it does not reflect anything close to what I needed. https://docs.amplify.aws/lib/graphqlapi/query-data/q/platform/js/#simple-query

qwikag avatar Sep 22 '23 00:09 qwikag

Hey @qwikag, Thanks for raising this. This issue has been address here https://docs.amplify.aws/react/build-a-backend/graphqlapi/query-data/#filter-list-queries. Please review and let us know.

AnilMaktala avatar Jan 30 '24 16:01 AnilMaktala