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

CF-897: Fix directives #267

Merged
merged 46 commits into from
Mar 13, 2020
Merged

CF-897: Fix directives #267

merged 46 commits into from
Mar 13, 2020

Conversation

BrandonHermanAvant
Copy link
Contributor

@BrandonHermanAvant BrandonHermanAvant commented Mar 11, 2020

This updates fixes directives in the current v2 branch.

Comment on lines 67 to 117
exports[`directives fragments works with inline fragments on base 1`] = `
"export interface FragmentSelectionOnHuman {}

export interface FragmentSelectionOnDroid {
primaryFunction: string | null;
primaryFunctionNonNull: string;
}
",
],
"interface": "export interface FragmentTest {
heroNoParam: Partial<IFragmentSpreadOnDroid> | null;

export interface FragmentTest {
heroNoParam?: FragmentSelectionOnHuman | FragmentSelectionOnDroid | null;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the one new test I added

Comment on lines +665 to +673
it('works with inline fragments on base', () => {
const response: string = runProgram(
schema,
inlineFragmentWithDirectiveOnBaseQuery,
undefined,
{ generateSubTypeInterfaceName }
);
expect(response).toMatchSnapshot();
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

New test

Copy link
Contributor

@brettjurgens brettjurgens left a comment

Choose a reason for hiding this comment

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

unsolicited feedback 😅

@@ -110,8 +123,15 @@ export default (OPTIONS: IFromQueryOptions): (ir: IOperation) => string => {
private buildDeclaration (selection: Selection): string {
switch (selection.kind) {
case 'Field':
const foo = !!selection.directives['include'] || !!selection.directives['skip']
Copy link
Contributor

Choose a reason for hiding this comment

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

can we find a better name than foo 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

can we reuse the hasDirectives above? we can then do it inline below

{
  // ...
  optional: hasDirectives(selection.directives)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ooh good catch, got excited that I got it working and missed this cleanup spot lol

// }
// }`)
// ).toMatchSnapshot();
// });
Copy link
Contributor

Choose a reason for hiding this comment

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

why are these tests commented out?

Copy link
Contributor Author

@BrandonHermanAvant BrandonHermanAvant Mar 12, 2020

Choose a reason for hiding this comment

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

They were failing before our changes. Maybe I should leave them in since this is just going into v2.0.0-3 branch?

The error is:

    InlineFragment Must Be Inlined!

);
expect(response).toMatchSnapshot();
});
// it('supports unions', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

do unions no longer work? why are these commented out?

Copy link
Contributor Author

@BrandonHermanAvant BrandonHermanAvant Mar 12, 2020

Choose a reason for hiding this comment

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

Currently getting the following error:

TypeError: Cannot read property 'type' of null

);

const selectionHasDirectives = hasDirectives(selection.directives);
const fragmentsHaveDirectives = !!selection.fragments.some(frag => hasDirectives(frag.directives))
Copy link
Contributor

Choose a reason for hiding this comment

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

An interesting thing to bring up is the defer directive. what should happen there? Should it transform a field from T -> T | null? Should it generate two types one for the deferred and one for the final result and then OR them?

What about custom directives? This was something I gave a little thought to but didn't come up with a consensus

Copy link
Contributor

Choose a reason for hiding this comment

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

I think re: custom directives it was an idea to allow the user to provide a "transform" function of sorts. but I don't exactly know what that looks like

);

const selectionHasDirectives = hasDirectives(selection.directives);
const fragmentsHaveDirectives = !!selection.fragments.some(frag => hasDirectives(frag.directives))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const fragmentsHaveDirectives = !!selection.fragments.some(frag => hasDirectives(frag.directives))
const fragmentsHaveDirectives = selection.fragments.some(frag => hasDirectives(frag.directives))

!! is unnecessary here as Array.prototype.some will always return a bool

@@ -110,8 +123,15 @@ export default (OPTIONS: IFromQueryOptions): (ir: IOperation) => string => {
private buildDeclaration (selection: Selection): string {
switch (selection.kind) {
case 'Field':
const foo = !!selection.directives['include'] || !!selection.directives['skip']
Copy link
Contributor

Choose a reason for hiding this comment

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

can we reuse the hasDirectives above? we can then do it inline below

{
  // ...
  optional: hasDirectives(selection.directives)
}

@@ -66,7 +66,20 @@ export default (OPTIONS: IFromQueryOptions): (ir: IOperation) => string => {
}
};

const printField: (name: string, type: TypeDefinition | string, node: Selection, nameOverride?: string) => string = (name, type, node, nameOverride) => `${name}: ${printType(type, node, nameOverride)};`;
const hasDirectives: (directives: IDirectiveMap) => boolean = directives =>
!!directives['include'] || !!directives['skip']
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
!!directives['include'] || !!directives['skip']
!!directives.include || !!directives.skip

you can use dot-notation

@@ -66,7 +66,20 @@ export default (OPTIONS: IFromQueryOptions): (ir: IOperation) => string => {
}
};

const printField: (name: string, type: TypeDefinition | string, node: Selection, nameOverride?: string) => string = (name, type, node, nameOverride) => `${name}: ${printType(type, node, nameOverride)};`;
const hasDirectives: (directives: IDirectiveMap) => boolean = directives =>
Copy link
Contributor

Choose a reason for hiding this comment

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

can we rename this to convey that it checks for specific directives instead of any directive as the name would suggest?

something like hasOptionalDirective or so?

@BrandonHermanAvant BrandonHermanAvant changed the title Cf 897 fix directives CF-897: Fix directives in v2.0.0-3 Mar 13, 2020
@BrandonHermanAvant BrandonHermanAvant force-pushed the CF-897_fix_directives branch 2 times, most recently from 8361abe to beaeb8a Compare March 13, 2020 17:24
@BrandonHermanAvant BrandonHermanAvant changed the base branch from v2.0.0-3 to experiment_with_fragments March 13, 2020 18:32
@BrandonHermanAvant BrandonHermanAvant changed the base branch from experiment_with_fragments to 2.0.0 March 13, 2020 20:51
@BrandonHermanAvant BrandonHermanAvant linked an issue Mar 13, 2020 that may be closed by this pull request
17 tasks
@BrandonHermanAvant BrandonHermanAvant changed the title CF-897: Fix directives in v2.0.0-3 CF-897: Fix directives Mar 13, 2020
@BrandonHermanAvant BrandonHermanAvant merged commit 75edcc9 into 2.0.0 Mar 13, 2020
@BrandonHermanAvant BrandonHermanAvant deleted the CF-897_fix_directives branch March 13, 2020 22:53
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.

Roadmap to 2.0
2 participants