Skip to content

[legacy-framework] Fix page generator with custom context#669

Merged
flybayer merged 8 commits intocanaryfrom
fix-replace-template-context
Jun 19, 2020
Merged

[legacy-framework] Fix page generator with custom context#669
flybayer merged 8 commits intocanaryfrom
fix-replace-template-context

Conversation

@peaonunes
Copy link
Collaborator

Closes: blitz-js/legacy-framework#910

What are the changes and their implications?

Fixes the page templates and the generations.
The paths on templates should be different from the modelNames since the generated queries and mutations may have additional path of context included.
Added a new template value in the pages to refer to import path of the model considering the context.

Checklist

  • Tests added for changes
  • PR submitted to blitzjs.com for any user facing changes

@peaonunes peaonunes requested a review from aem as a code owner June 12, 2020 10:23
@peaonunes peaonunes self-assigned this Jun 12, 2020
@peaonunes peaonunes changed the title Fix replace template context Fix page generator replace of context Jun 12, 2020
modelNames: this.options.modelNames,
ModelName: this.options.ModelName,
ModelNames: this.options.ModelNames,
importModelNames: this.getPathWithContext(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Happy to change to a better name could not think about any other atm.

Copy link
Member

Choose a reason for hiding this comment

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

What about modelNamesPath?

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 for modelNamesPath

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed here 783027e

}

getModelNameAndContext(modelName: string, context?: string): {model: string; context: string} {
getModelNameAndContext(modelName: string, context?: string): {model: string; context?: string} {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Found a bug here while testing the different input values.
Passing not setting the context was leading to contextSegments.= '' here. However, if we call path with empty string we gotta a wrong path:

console.log(path.join(''))
> .

So I noticed a wrong it would generate the wrong import on the newly __importModelNames I've added. Since the options.context passed down to generators would be .. That potentially would affect other parts of the code as well.
Screen Shot 2020-06-12 at 20 49 15

@peaonunes peaonunes requested a review from flybayer June 12, 2020 13:03
@flybayer
Copy link
Member

Awesome, thank you so much!! :) I'll let @aem do a full review

Copy link
Collaborator

@aem aem left a comment

Choose a reason for hiding this comment

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

wow, good find here! thanks for jumping on a fix. just a couple of small comments, overall looks great though!

}
}

if (!!context && context !== '') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

super minor: can we use Boolean(context) instead of !!? it's just a little more explicit about the intent

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TS seems to think Boolean constructor is not equivalent to use !!. It's complaining.
image
So I changed to be != null and != undefined explicitly here 4f97355

modelNames: this.options.modelNames,
ModelName: this.options.ModelName,
ModelNames: this.options.ModelNames,
importModelNames: this.getPathWithContext(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 for modelNamesPath

@peaonunes peaonunes force-pushed the fix-replace-template-context branch from e72f4ac to 783027e Compare June 15, 2020 07:52
@peaonunes peaonunes requested a review from aem June 15, 2020 07:59
}
}

if (context !== undefined && context !== null && context !== '') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is good if you're happy with this solution! Two other options that are a little shorter:

  1. if (context != null && context !== '')
  2. if (Boolean(context)) (context as string).split(/[\\/]/)

We can either take advantage of != null checking for both null and undefined but no other falsy values, or the fact that we're a little smarter than the TS compiler and just do a manual cast to string since we know the value can't be undefined at that point

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point! Happy to go with 2. Done here a8ca8fd

@peaonunes peaonunes requested a review from aem June 16, 2020 12:22
@flybayer flybayer changed the title Fix page generator replace of context Fix page generator with custom context Jun 19, 2020
@flybayer flybayer merged commit 09fc2e1 into canary Jun 19, 2020
@flybayer flybayer deleted the fix-replace-template-context branch June 19, 2020 02:11
@itsdillon itsdillon changed the title Fix page generator with custom context [legacy-framework] Fix page generator with custom context Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG on generated routes with --context

3 participants