sid88in / serverless-appsync-plugin

serverless plugin for appsync
MIT License
951 stars 189 forks source link

Multiple GraphQlApi / GraphQlUrl reference #467

Open NevRA opened 2 years ago

NevRA commented 2 years ago

Hey For the single API I can reference it with

ENDPOINT: !GetAtt 
  - GraphQlApi
  - GraphQLUrl

For the multiple APIs we could use https://github.com/sid88in/serverless-appsync-plugin/issues/275 But I can't understand how to make it work without hardcode and with stages support

E.g. I have API with a name ${self:provider.stackName}-public (project-stage-rc-2-public), it will create a resource with logical id Projectstagerc2publicGraphQlApi (without dashes)

How to reference it and have the ability to work with multiple staging environments (without hardcode rc-2 in the YAML file)?

Thanks!

bboure commented 2 years ago

@NevRA

I understand this is an issue. I do not see an easy fix to that now.

A workaround would be to use js macros to achieve that.

Something like this:

ENDPOINT: ${file(templates/macros.js):getPulblicAppSyncEndpoint()}

in templates/macros.js

const getStage = (serverless) => {
  const { stage } = serverless.options
    ? serverless.options
    : serverless.service.provider;

  return stage;
};

module.exports = {
  getPulblicAppSyncEndpoint(serverless) {
    const name = removeDashes(getStage(serverless)) + 'publicGraphQlApi';
    return { '!GetAtt', [ name, 'GraphQLUrl' ] };
  },
};

On.a side note, version 2 introduces variables which should simplify accessing id, ARN and URL of the API.

However, it also drops support for multiple APIs, which I understand you use.

I'd like to hear your feedback and use case about why using more than one API in a single stack. Feel free to contact me about it

NevRA commented 2 years ago

@bboure Thank you for the reply! Looks promising The problem is that I have already renamed all stagings without the dash :) Thanks for mentioning v2; I didn't see that. I will write to you about multi API and think about how best to do it because it looks like this multi-API approach is a path to nowhere.