Skip to content

Commit

Permalink
fix(cli): Fix apiId not found (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
bboure authored Dec 23, 2022
1 parent d0bdc85 commit 11a99c8
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class ServerlessAppsyncPlugin {
public readonly commands?: CommandsDefinition;
public readonly configurationVariablesSources?: VariablesSourcesDefinition;
private api?: Api;
private naming?: Naming;

constructor(
public serverless: Serverless,
Expand Down Expand Up @@ -344,18 +345,25 @@ class ServerlessAppsyncPlugin {
}

async getApiId() {
this.loadConfig();

if (!this.naming) {
throw new this.serverless.classes.Error(
'Could not find the naming service. This should not happen.',
);
}

const logicalIdGraphQLApi = this.naming.getApiLogicalId();

const { StackResources } = await this.provider.request<
DescribeStackResourcesInput,
DescribeStackResourcesOutput
>('CloudFormation', 'describeStackResources', {
StackName: this.provider.naming.getStackName(),
LogicalResourceId: logicalIdGraphQLApi,
});

const apiId = last(
StackResources?.find(
(resource) => resource.ResourceType === 'AWS::AppSync::GraphQLApi',
)?.PhysicalResourceId?.split('/'),
);
const apiId = last(StackResources?.[0]?.PhysicalResourceId?.split('/'));

if (!apiId) {
throw new this.serverless.classes.Error(
Expand Down Expand Up @@ -950,6 +958,7 @@ class ServerlessAppsyncPlugin {
}
}
const config = getAppSyncConfig(appSync);
this.naming = new Naming(appSync.name);
this.api = new Api(config, this);
}

Expand Down Expand Up @@ -992,31 +1001,37 @@ class ServerlessAppsyncPlugin {
}

public resolveVariable: VariableSourceResolver = ({ address }) => {
const naming = new Naming(this.serverless.configurationInput.appSync.name);
this.loadConfig();

if (!this.naming) {
throw new this.serverless.classes.Error(
'Could not find the naming service. This should not happen.',
);
}

if (address === 'id') {
return {
value: {
'Fn::GetAtt': [naming.getApiLogicalId(), 'ApiId'],
'Fn::GetAtt': [this.naming.getApiLogicalId(), 'ApiId'],
},
};
} else if (address === 'arn') {
return {
value: {
'Fn::GetAtt': [naming.getApiLogicalId(), 'Arn'],
'Fn::GetAtt': [this.naming.getApiLogicalId(), 'Arn'],
},
};
} else if (address === 'url') {
return {
value: {
'Fn::GetAtt': [naming.getApiLogicalId(), 'GraphQLUrl'],
'Fn::GetAtt': [this.naming.getApiLogicalId(), 'GraphQLUrl'],
},
};
} else if (address.startsWith('apiKey.')) {
const [, name] = address.split('.');
return {
value: {
'Fn::GetAtt': [naming.getApiKeyLogicalId(name), 'ApiKey'],
'Fn::GetAtt': [this.naming.getApiKeyLogicalId(name), 'ApiKey'],
},
};
} else {
Expand Down

0 comments on commit 11a99c8

Please sign in to comment.