-
Notifications
You must be signed in to change notification settings - Fork 4
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
Conversation
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; | ||
} |
There was a problem hiding this comment.
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
it('works with inline fragments on base', () => { | ||
const response: string = runProgram( | ||
schema, | ||
inlineFragmentWithDirectiveOnBaseQuery, | ||
undefined, | ||
{ generateSubTypeInterfaceName } | ||
); | ||
expect(response).toMatchSnapshot(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsolicited feedback 😅
packages/from-query/src/generate.ts
Outdated
@@ -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'] |
There was a problem hiding this comment.
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
😄
There was a problem hiding this comment.
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)
}
There was a problem hiding this comment.
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(); | ||
// }); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
packages/from-query/src/generate.ts
Outdated
); | ||
|
||
const selectionHasDirectives = hasDirectives(selection.directives); | ||
const fragmentsHaveDirectives = !!selection.fragments.some(frag => hasDirectives(frag.directives)) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
packages/from-query/src/generate.ts
Outdated
); | ||
|
||
const selectionHasDirectives = hasDirectives(selection.directives); | ||
const fragmentsHaveDirectives = !!selection.fragments.some(frag => hasDirectives(frag.directives)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
packages/from-query/src/generate.ts
Outdated
@@ -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'] |
There was a problem hiding this comment.
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)
}
packages/from-query/src/generate.ts
Outdated
@@ -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'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!!directives['include'] || !!directives['skip'] | |
!!directives.include || !!directives.skip |
you can use dot-notation
packages/from-query/src/generate.ts
Outdated
@@ -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 => |
There was a problem hiding this comment.
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?
8361abe
to
beaeb8a
Compare
71f1c6d
to
854e639
Compare
This updates fixes directives in the current v2 branch.