serverless-appsync-simulator icon indicating copy to clipboard operation
serverless-appsync-simulator copied to clipboard

Error while executing GraphQL statement Error: Must provide Source. Received: undefined

Open pkubat opened this issue 3 years ago • 2 comments

Describe the bug This is a direct lambda query/datasource. It works fine deployed to AWS but dies not to work locally. Not sure how to debug/trace as SLS_DEBUG does not generate traces or output from the queries.

To Reproduce npx sls offline start and query the service

sample query

curl -X "POST" "http://localhost:20002/graphql" \
     -H 'x-api-key: 0123456789' \
     -H 'Content-Type: application/graphql' \
     -d $'{
	"query": "query MyQuery{listBooks{title}}",
	"variables": {}
}'

Expected behavior

Screenshots

Additional context

  plugins: [
    'serverless-appsync-plugin',
    'serverless-appsync-simulator',
    'serverless-offline',
    'serverless-esbuild',
  ],
    appSync: {
      name: 'service-members-ledgers',
      authenticationType: 'API_KEY',
      apiKeys: [ {
        name: 'key 1',
        description: 'asdasd',
        expiresAfter: '30d',
      }],
      mappingTemplates: [{
        type: 'Query',
        request: false,
        response: false,
        dataSource: '_ds_appSyncTest',
        field: 'listBooks',
      }],
      dataSources: [{
        type: 'AWS_LAMBDA',
        name: '_ds_appSyncTest',
        config: {
          functionName: 'appSyncTest'
        }
      }],
    },

SLS+DEBUG=*

AppSync Simulator: AppSync Config service-members-ledgers
AppSync Simulator: {
  appSync: {
    name: 'service-members-ledgers',
    apiKey: '0123456789',
    defaultAuthenticationType: { authenticationType: 'API_KEY' },
    additionalAuthenticationProviders: []
  },
  schema: {
    path: 'schema.graphql',
    content: 'schema {\n' +
      '  query: Query\n' +
      '}\n' +
      '\n' +
      'type Book {\n' +
      '  title: String\n' +
      '}\n' +
      '\n' +
      'type Query {\n' +
      '  listBooks: [Book]\n' +
      '}'
  },
  resolvers: [
    {
      kind: 'UNIT',
      fieldName: 'listBooks',
      typeName: 'Query',
      dataSourceName: '_ds_appSyncTest',
      functions: undefined,
      requestMappingTemplate: '## Direct lambda request\n' +
        '{\n' +
        '    "version": "2018-05-29",\n' +
        '    "operation": "Invoke",\n' +
        '    "payload": $utils.toJson($context)\n' +
        '}\n',
      responseMappingTemplate: '## Direct lambda response\n' +
        '#if($ctx.error)\n' +
        '    $util.error($ctx.error.message, $ctx.error.type, $ctx.result)\n' +
        '#end\n' +
        '$util.toJson($ctx.result)\n'
    }
  ],
  dataSources: [
    {
      name: '_ds_appSyncTest',
      type: 'AWS_LAMBDA',
      invoke: [AsyncFunction: invoke]
    }
  ],
  functions: []
}

pkubat avatar Jan 18 '22 21:01 pkubat

This is caused by the use of an HTTP header Content-Type:application/graphql which is used in AWS's examples at https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html#api-key-authorization. It seems that the request ends up as any empty object {}

The workaround for this is to drop the header Content-Type:application/graphql or use application/json

pkubat avatar Jan 19 '22 21:01 pkubat

Thank you @pkubat I think this issue is probably on https://github.com/aws-amplify/amplify-cli/ it might be worth opening an issue there

bboure avatar Feb 17 '22 20:02 bboure