Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking a function that inserts data to AppSync result in Error: Must provide Source. Received: undefined #10573

Closed
5 tasks done
mstoyanovv opened this issue Jun 13, 2022 · 8 comments
Assignees
Labels
documentation Add or update documentation functions Issues tied to the functions category mock Issues tied to the mock functionality

Comments

@mstoyanovv
Copy link

Before opening, please confirm:

  • I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
  • I have searched for duplicate or closed issues.
  • I have read the guide for submitting bug reports.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

v14.19.3

Amplify CLI Version

8.0.2

What operating system are you using?

Mac

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

I have not

Amplify Categories

storage, function

Amplify Commands

Not applicable

Describe the bug

I am following the documentation for Calling GraphQL API from a Lambda function and everything works when deployed but I cannot mock it locally. First issue was that amplify mock does not support ES modules and therefore the examples in the documentation are not working with it. I fixed that by using node-fetch v2 which works with commonjs and switched to requiring modules so mocking would work.
Next issue is that the example code provided for mutation works when deployed in aws but does not work with amplify mock api and amplify mock function. The error returned by graphql local endpoint is "Must provide Source. Received: undefined".

Expected behavior

The examples provided in the documentation should work as expected when mocking locally and data should be inserted in the local graphql.

Reproduction steps

  1. Create function for graphql mutation following the example here)
  2. Modify code to use commonjs modules instead of ES ones so amplify mock function would work
  3. Execute amplify mock function with sample event

GraphQL schema(s)

# Put schemas below this line

Log output

# Put your logs below this line


Additional information

No response

@josefaidt josefaidt added functions Issues tied to the functions category pending-triage Issue is pending triage labels Jun 13, 2022
@josefaidt
Copy link
Contributor

Hey @mstoyanovv 👋 thanks for raising this, and apologies for the confusion with the ESM example & mock (support for this is tracked here #10432). Regardless of the outcome here I think we should include a CommonJS example on our docs as well. Would you be open to contributing to our docs examples with your CommonJS port?

Can you confirm the API call is sending a valid body with the query/mutation and inputs when mocking? And to clarify, are the environment variables such as the GraphQL API endpoint being populated?

@josefaidt josefaidt added pending-response Issue is pending response from the issue author mock Issues tied to the mock functionality labels Jun 13, 2022
@mstoyanovv
Copy link
Author

Hey @josefaidt I will open a pr with commonjs samples. Regarding the body value I am sure that it is valid as it is the same when uploaded to aws via amplify function push. There it connects to appsync api and successfully inserts data.

I also thought about the env variables and upon checking them they seem to be good. There is a graphql endpoint that points to localhost:20002/graphql and test api key variable that is not empty.

@myartsev
Copy link

Hey @josefaidt, I am also running into this.
Unlike @mstoyanovv we do use ESM modules since we have typscript + webpack, but that's beside the point here.

Running the local amplify mock, here's a sample payload for requestToBeSigned:

{
  requestToBeSigned: '{"method":"POST","hostname":"localhost:20002","query":{},"headers":{"host":"localhost:20002"},"body":"{\\"query\\":\\"\\\\n  query MyQuery {\\\\n    listFunds {\\\\n      items {\\\\n        id\\\\n        name\\\\n      }\\\\n    }\\\\n  }\\\\n\\"}","protocol":"https:","path":"/graphql"}'
}

image

And the same payload after it was signed:

{
  signed: HttpRequest {
    method: 'POST',
    hostname: 'localhost:20002',
    port: undefined,
    query: {},
    headers: {
      host: 'localhost:20002',
      'x-amz-date': '20220622T002112Z',
      'x-amz-content-sha256': '1716517f4fc8f5723c327f424378dc408a1667b8ad09239580a8e820ebd22813',
      authorization: 'AWS4-HMAC-SHA256 Credential=AKIA3NJ75NEZWAU5I3HK/20220622/us-west-2/appsync/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=6acc008b6f82a5b525f4c45d0314305a25428d6a7450500bd84bfb9e92d2b334'
    },
    body: '{"query":"\\n  query MyQuery {\\n    listFunds {\\n      items {\\n        id\\n        name\\n      }\\n    }\\n  }\\n"}',
    protocol: 'https:',
    path: '/graphql'
  }
}

image

As described in the original issue, deploying this exact same code and executing in the cloud from AppSync works just fine.

Executing the same code locally with amplify mock gives me the "must provide source" error.
Here is the full stack-trace if helpful:

[1] Error while executing GraphQL statement Error: Must provide Source. Received: undefined
[1]     at devAssert (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/graphql/jsutils/devAssert.js:12:11)
[1]     at new Parser (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/graphql/language/parser.js:82:67)
[1]     at parse (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/graphql/language/parser.js:35:16)
[1]     at OperationServer.handleRequest (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/amplify-appsync-simulator/src/server/operations.ts:58:24)
[1]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/layer.js:95:5)
[1]     at next (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/route.js:144:13)
[1]     at Route.dispatch (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/route.js:114:3)
[1]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/layer.js:95:5)
[1]     at /usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:284:15
[1]     at Function.process_params (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:346:12)
[1]     at next (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:280:10)
[1]     at cors (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/cors/lib/index.js:188:7)
[1]     at /usr/local/lib/node_modules/@aws-amplify/cli/node_modules/cors/lib/index.js:224:17
[1]     at originCallback (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/cors/lib/index.js:214:15)
[1]     at /usr/local/lib/node_modules/@aws-amplify/cli/node_modules/cors/lib/index.js:219:13
[1]     at optionsCallback (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/cors/lib/index.js:199:9)
[1]     at corsMiddleware (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/cors/lib/index.js:204:7)
[1]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/layer.js:95:5)
[1]     at trim_prefix (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:328:13)
[1]     at /usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:286:9
[1]     at Function.process_params (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:346:12)
[1]     at next (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:280:10)
[1]     at jsonParser (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/body-parser/lib/types/json.js:119:7)
[1]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/layer.js:95:5)
[1]     at trim_prefix (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:328:13)
[1]     at /usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:286:9
[1]     at Function.process_params (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:346:12)
[1]     at next (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:280:10)
[1]     at expressInit (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/middleware/init.js:40:5)
[1]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/layer.js:95:5)
[1]     at trim_prefix (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:328:13)
[1]     at /usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:286:9
[1]     at Function.process_params (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:346:12)
[1]     at next (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:280:10)
[1]     at query (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/middleware/query.js:45:5)
[1]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/layer.js:95:5)
[1]     at trim_prefix (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:328:13)
[1]     at /usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:286:9
[1]     at Function.process_params (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:346:12)
[1]     at next (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:280:10)
[1]     at Function.handle (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/router/index.js:175:3)
[1]     at Function.handle (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/application.js:181:10)
[1]     at Server.app (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/express/lib/express.js:39:9)
[1]     at Server.emit (events.js:400:28)
[1]     at Server.emit (domain.js:470:12)
[1]     at parserOnIncoming (_http_server.js:897:12)
[1]     at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)

Thank you so much & let me know if you need any further info!

@myartsev
Copy link

^ Verified that the above is still the case with the latest CLI version: @aws-amplify/[email protected]

@josefaidt josefaidt self-assigned this Jun 24, 2022
@josefaidt
Copy link
Contributor

Hey @myartsev and @mstoyanovv 👋 I was able to successfully reproduce this issue using a simple query to the mocked GraphQL API, and was able to mitigate it by adding the following header to the request, 'Content-Type': 'application/json',. Marking as a documentation issue 🙂

@josefaidt josefaidt added documentation Add or update documentation and removed pending-response Issue is pending response from the issue author pending-triage Issue is pending triage labels Jun 24, 2022
@josefaidt
Copy link
Contributor

Adding pending-close per docs PR merge

@josefaidt
Copy link
Contributor

Closing, @mstoyanovv if you continue to experience issues after this small adjustment to the template please reply back to this thread and we can re-open to investigate further 🙂

@mstoyanovv
Copy link
Author

Closing, @mstoyanovv if you continue to experience issues after this small adjustment to the template please reply back to this thread and we can re-open to investigate further 🙂

Pr to update doc - aws-amplify/docs#4441

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Add or update documentation functions Issues tied to the functions category mock Issues tied to the mock functionality
Projects
None yet
Development

No branches or pull requests

3 participants