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

feat: add types to graphql in TS outputs #620

Closed

Conversation

svidgen
Copy link
Member

@svidgen svidgen commented Jul 12, 2023

Description of changes

Let's discuss the general approach before we worry about the nits.

In order to add types to the generated graphql for TypeScript, we need to know the underlying operation type (Query | Mutation | Subscription) and specific operation name (CreateBlog, ListBlogs, etc.) at the time graphql operations are embeded/formatted for the language as variable assignments. This proposal infers the specific operation name from the key value in the statements map.

The alternative would be to weave original type names through the chain — this seems more robust. But, if the type name is effectively captured in the statements map anyway, perhaps this is unnecessary.

I'm looking for high level guidance from codegen folks before proceeding.

Issue #, if available

Description of how you validated changes

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)
  • Breaking changes to existing customers are released behind a feature flag or major version update
  • Changes are tested using sample applications for all relevant platforms (iOS/android/flutter/Javascript) that use the feature added/modified

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

get typeDefs() {
if (this.language === 'typescript' && this.opTypeName) {
return [
`import * as APITypes from '../API';`,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the location of API.ts stable relative to the generated graphql path? I'm guessing not. How should I calculate the correct relative path here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it is not, customers can choose any custom path relative to the project root when they do "amplify add codegen".

${variablesTypeName},
${resultTypeName}
>;`;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was initially thinking this was very gross. But, now I'm wondering if the name I receive here (from the statements map) is actually all the info I need, minus the casing.

@codecov-commenter
Copy link

Codecov Report

Merging #620 (0314257) into main (8fb4331) will increase coverage by 0.02%.
The diff coverage is 92.30%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##             main     #620      +/-   ##
==========================================
+ Coverage   85.93%   85.96%   +0.02%     
==========================================
  Files         152      152              
  Lines        7489     7502      +13     
  Branches     1956     1959       +3     
==========================================
+ Hits         6436     6449      +13     
  Misses        960      960              
  Partials       93       93              
Impacted Files Coverage Δ
packages/graphql-docs-generator/src/index.ts 70.90% <50.00%> (ø)
...ackages/amplify-codegen/src/commands/statements.js 85.91% <80.00%> (ø)
...fy-codegen/src/utils/GraphQLStatementsFormatter.js 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Comment on lines +81 to +82
const variablesTypeName = `APITypes.${titleCasedName}${this.opTypeName}Variables`;
const resultTypeName = `APITypes.${titleCasedName}${this.opTypeName}`;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have a rev by standup tomorrow that uses interfaceNameFromOperation + a new interfaceVariablesNameFromOperation instead of re-deriving those names here. If I'm reading things correctly, that should guard against future mismatches due to drift.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as another PR to compare/contrast: https://github.com/aws-amplify/amplify-codegen/pull/623/files

if (!this.opTypeName) return '';
if (this.language !== 'typescript') return '';

const titleCasedName = `${name[0].toUpperCase()}${name.slice(1)}`;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm reading things right, what I'm deriving here is actually name from the ops arrays earlier in the chain. It looks safe to use this name and just fix casing, but in case I'm wrong and to prevent drift, I'm looking at adding an optional flag to generateGraphQLDocuments to return something like Map<string, {query: string, meta: { ...}}> when needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as another PR to compare/contrast: https://github.com/aws-amplify/amplify-codegen/pull/623/files

@svidgen
Copy link
Member Author

svidgen commented Aug 8, 2023

Using this one instead: #623

@svidgen svidgen closed this Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants