-
Notifications
You must be signed in to change notification settings - Fork 17
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
Validate that all context types match #29
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a4e62e8
[WIP] Validate the type of context argument
captbaritone fd74e79
Fix broken test
captbaritone 75255ac
Check that all context types reference the same type
captbaritone 03dd662
Allow `unknown` for ctx and prefer `unknown` for args over `never`
captbaritone 8ee1d65
Fix links (and another never -> unknown update)
captbaritone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -358,6 +358,11 @@ export class Extractor { | |
args = this.collectArgs(argsParam); | ||
} | ||
|
||
const context = node.parameters[2]; | ||
if (context != null) { | ||
this.validateContextParameter(context); | ||
} | ||
|
||
const description = this.collectDescription(funcName); | ||
|
||
if (!ts.isSourceFile(node.parent)) { | ||
|
@@ -980,7 +985,7 @@ export class Extractor { | |
} else if (node.kind === ts.SyntaxKind.FalseKeyword) { | ||
return this.gql.boolean(node, false); | ||
} else if (ts.isObjectLiteralExpression(node)) { | ||
return this.cellectObjectLiteral(node); | ||
return this.collectObjectLiteral(node); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated typo fix. |
||
} else if (ts.isArrayLiteralExpression(node)) { | ||
return this.collectArrayLiteral(node); | ||
} | ||
|
@@ -1010,7 +1015,7 @@ export class Extractor { | |
return this.gql.list(node, values); | ||
} | ||
|
||
cellectObjectLiteral( | ||
collectObjectLiteral( | ||
node: ts.ObjectLiteralExpression, | ||
): ConstObjectValueNode | null { | ||
const fields: ConstObjectFieldNode[] = []; | ||
|
@@ -1301,6 +1306,60 @@ export class Extractor { | |
return this.gql.name(id, id.text); | ||
} | ||
|
||
// Ensure the type of the ctx param resolves to the declaration | ||
// annotated with `@gqlContext`. | ||
validateContextParameter(node: ts.ParameterDeclaration) { | ||
if (node.type == null) { | ||
return this.report(node, E.expectedTypeAnnotationOnContext()); | ||
} | ||
|
||
if (!ts.isTypeReferenceNode(node.type)) { | ||
return this.report( | ||
node.type, | ||
E.expectedTypeAnnotationOfReferenceOnContext(), | ||
); | ||
} | ||
|
||
// Check for ... | ||
if (node.dotDotDotToken != null) { | ||
return this.report( | ||
node.dotDotDotToken, | ||
E.unexpectedParamSpreadForContextParam(), | ||
); | ||
} | ||
|
||
const symbol = this.ctx.checker.getSymbolAtLocation(node.type.typeName); | ||
if (symbol == null) { | ||
return this.report( | ||
node.type.typeName, | ||
E.expectedTypeAnnotationOnContextToBeResolvable(), | ||
); | ||
} | ||
|
||
const declaration = this.ctx.findSymbolDeclaration(symbol); | ||
if (declaration == null) { | ||
return this.report( | ||
node.type.typeName, | ||
E.expectedTypeAnnotationOnContextToHaveDeclaration(), | ||
); | ||
} | ||
|
||
if (this.ctx.gqlContext == null) { | ||
// This is the first typed context value we've seen... | ||
this.ctx.gqlContext = { | ||
declaration: declaration, | ||
firstReference: node.type.typeName, | ||
}; | ||
} else if (this.ctx.gqlContext.declaration !== declaration) { | ||
return this.report(node.type.typeName, E.multipleContextTypes(), [ | ||
this.related( | ||
this.ctx.gqlContext.firstReference, | ||
"A different type reference was used here", | ||
), | ||
]); | ||
} | ||
} | ||
|
||
methodDeclaration( | ||
node: ts.MethodDeclaration | ts.MethodSignature, | ||
): FieldDefinitionNode | null { | ||
|
@@ -1325,6 +1384,11 @@ export class Extractor { | |
args = this.collectArgs(argsParam); | ||
} | ||
|
||
const context = node.parameters[1]; | ||
if (context != null) { | ||
this.validateContextParameter(context); | ||
} | ||
|
||
const description = this.collectDescription(node.name); | ||
|
||
const id = this.expectIdentifier(node.name); | ||
|
@@ -1556,7 +1620,7 @@ export class Extractor { | |
if (ts.isIdentifier(node)) { | ||
return node; | ||
} | ||
return this.report(node, E.expectedIdentifer()); | ||
return this.report(node, E.expectedIdentifier()); | ||
} | ||
|
||
findTag(node: ts.Node, tagName: string): ts.JSDocTag | null { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/** @gqlType */ | ||
export default class Query { | ||
/** @gqlField */ | ||
hello(args: never, ctx: any): string { | ||
console.log(ctx); | ||
hello(args: never): string { | ||
console.log("hello"); | ||
return "Hello world!"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/tests/fixtures/resolver_context/ClassMethodWithContextValue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type GratsContext = { | ||
greeting: string; | ||
}; | ||
|
||
/** @gqlType */ | ||
export class Query { | ||
/** @gqlField */ | ||
greeting(args: never, ctx: GratsContext): string { | ||
return ctx.greeting; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/tests/fixtures/resolver_context/ClassMethodWithContextValue.ts.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
----------------- | ||
INPUT | ||
----------------- | ||
type GratsContext = { | ||
greeting: string; | ||
}; | ||
|
||
/** @gqlType */ | ||
export class Query { | ||
/** @gqlField */ | ||
greeting(args: never, ctx: GratsContext): string { | ||
return ctx.greeting; | ||
} | ||
} | ||
|
||
----------------- | ||
OUTPUT | ||
----------------- | ||
schema { | ||
query: Query | ||
} | ||
|
||
directive @methodName(name: String!) on FIELD_DEFINITION | ||
|
||
directive @exported(filename: String!, functionName: String!) on FIELD_DEFINITION | ||
|
||
type Query { | ||
greeting: String | ||
} |
11 changes: 11 additions & 0 deletions
11
src/tests/fixtures/resolver_context/ClassMethodWithContextValueExported.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export type GratsContext = { | ||
greeting: string; | ||
}; | ||
|
||
/** @gqlType */ | ||
export class Query { | ||
/** @gqlField */ | ||
greeting(args: never, ctx: GratsContext): string { | ||
return ctx.greeting; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/tests/fixtures/resolver_context/ClassMethodWithContextValueExported.ts.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
----------------- | ||
INPUT | ||
----------------- | ||
export type GratsContext = { | ||
greeting: string; | ||
}; | ||
|
||
/** @gqlType */ | ||
export class Query { | ||
/** @gqlField */ | ||
greeting(args: never, ctx: GratsContext): string { | ||
return ctx.greeting; | ||
} | ||
} | ||
|
||
----------------- | ||
OUTPUT | ||
----------------- | ||
schema { | ||
query: Query | ||
} | ||
|
||
directive @methodName(name: String!) on FIELD_DEFINITION | ||
|
||
directive @exported(filename: String!, functionName: String!) on FIELD_DEFINITION | ||
|
||
type Query { | ||
greeting: String | ||
} |
11 changes: 11 additions & 0 deletions
11
src/tests/fixtures/resolver_context/ClassMethodWithContextValueInArgsPos.invalid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type GratsContext = { | ||
greeting: string; | ||
}; | ||
|
||
/** @gqlType */ | ||
export class Query { | ||
/** @gqlField */ | ||
greeting(ctx: GratsContext): string { | ||
return ctx.greeting; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/tests/fixtures/resolver_context/ClassMethodWithContextValueInArgsPos.invalid.ts.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
----------------- | ||
INPUT | ||
----------------- | ||
type GratsContext = { | ||
greeting: string; | ||
}; | ||
|
||
/** @gqlType */ | ||
export class Query { | ||
/** @gqlField */ | ||
greeting(ctx: GratsContext): string { | ||
return ctx.greeting; | ||
} | ||
} | ||
|
||
----------------- | ||
OUTPUT | ||
----------------- | ||
src/tests/fixtures/resolver_context/ClassMethodWithContextValueInArgsPos.invalid.ts:8:17 - error: Expected GraphQL field arguments to be typed using a literal object: `{someField: string}`. | ||
|
||
8 greeting(ctx: GratsContext): string { | ||
~~~~~~~~~~~~ |
7 changes: 7 additions & 0 deletions
7
src/tests/fixtures/resolver_context/ContextValueMissingTypeAnnotation.invalid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** @gqlType */ | ||
export class Query { | ||
/** @gqlField */ | ||
greeting(args: never, ctx): string { | ||
return ctx.greeting; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/tests/fixtures/resolver_context/ContextValueMissingTypeAnnotation.invalid.ts.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
----------------- | ||
INPUT | ||
----------------- | ||
/** @gqlType */ | ||
export class Query { | ||
/** @gqlField */ | ||
greeting(args: never, ctx): string { | ||
return ctx.greeting; | ||
} | ||
} | ||
|
||
----------------- | ||
OUTPUT | ||
----------------- | ||
src/tests/fixtures/resolver_context/ContextValueMissingTypeAnnotation.invalid.ts:4:25 - error: Expected context parameter to have a type annotation. Grats validates that your context parameter is type-safe by checking all context values reference the same type declaration. | ||
|
||
4 greeting(args: never, ctx): string { | ||
~~~ |
12 changes: 12 additions & 0 deletions
12
src/tests/fixtures/resolver_context/ContextValueOptional.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** @gqlType */ | ||
export class Query { | ||
/** @gqlField */ | ||
greeting(args: never, ctx?: SomeType): string { | ||
// This is fine since Grats will always pass ctx. It's fine for | ||
// the resolver to _also_ work _without_ ctx, as long as it's | ||
// safe for Grats to pass ctx. | ||
return ctx?.greeting ?? "Hello, World!"; | ||
} | ||
} | ||
|
||
type SomeType = { greeting: string }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe premature to add this since we don't yet typecheck the
info
argument.