Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
INFO_TAG,
DIRECTIVE_TAG,
} from "./Extractor.js";
import { DirectiveLocation } from "graphql";

export const ISSUE_URL = "https://github.com/captbaritone/grats/issues";

Expand Down Expand Up @@ -654,6 +655,10 @@ export function directiveTagNoComment() {
return "Expected `@gqlDirective` tag to specify at least one location.";
}

export function invalidDirectiveLocation(name: string) {
return `"${name}" is not a valid directive location. Valid locations are: ${Object.values(DirectiveLocation).join(", ")}.`;
}

export function directiveFunctionNotNamed() {
return "Expected `@gqlDirective` function to be named.";
}
Expand Down
18 changes: 15 additions & 3 deletions src/Extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
DefinitionNode,
version as graphqlJSVersion,
TokenKind,
GraphQLError,
DirectiveLocation,
} from "graphql";
import { gte as semverGte } from "semver";
import {
Expand Down Expand Up @@ -536,13 +536,21 @@ class Extractor {
}

const locations = parser
.delimitedMany(TokenKind.PIPE, () => parser.parseDirectiveLocation())
.delimitedMany(TokenKind.PIPE, () => parser.parseName())
.map((location) => ({ ...location, loc: loc(tag) }));
return { name, repeatable, locations };
});

if (tagData == null) return;

const validLocations = Object.values(DirectiveLocation) as string[];
for (const location of tagData.locations) {
if (!validLocations.includes(location.value)) {
this.report(tag, E.invalidDirectiveLocation(location.value));
return;
}
}

let name = tagData.name;

// If there wasn't a name in the directive tag, we expect the function
Expand Down Expand Up @@ -909,7 +917,11 @@ class Extractor {
parser.expectToken(TokenKind.EOF);
return result;
} catch (err) {
if (err instanceof GraphQLError) {
if (err instanceof Error && err.name === "GraphQLError") {
// Note: We use a name check instead of `instanceof GraphQLError`
// because in bundled environments (e.g. the playground), the
// GraphQLError class from the parser may be a different instance
// than the one we imported, causing `instanceof` to fail.
this.report(node, err.message);
} else {
throw err;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function customDirective() {}
### Error Report

```text
src/tests/fixtures/directives/defineCustomDirectiveLocationInvalid.invalid.ts:3:4 - error: Syntax Error: Unexpected Name "WHOOPS".
src/tests/fixtures/directives/defineCustomDirectiveLocationInvalid.invalid.ts:3:4 - error: "WHOOPS" is not a valid directive location. Valid locations are: QUERY, MUTATION, SUBSCRIPTION, FIELD, FRAGMENT_DEFINITION, FRAGMENT_SPREAD, INLINE_FRAGMENT, VARIABLE_DEFINITION, SCHEMA, SCALAR, OBJECT, FIELD_DEFINITION, ARGUMENT_DEFINITION, INTERFACE, UNION, ENUM, ENUM_VALUE, INPUT_OBJECT, INPUT_FIELD_DEFINITION.

3 * @gqlDirective on WHOOPS
~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Int } from "../../../Types";
/**
* @gqlDirective on DIRECTIVE_DEFINITION
* @gqlAnnotate
*/
export function myDirective(args: { credits: Int }) {
// ...
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# directives/directiveOnDirectiveDefinitionLocation.invalid.ts

## Input

```ts title="directives/directiveOnDirectiveDefinitionLocation.invalid.ts"
import { Int } from "../../../Types";
/**
* @gqlDirective on DIRECTIVE_DEFINITION
* @gqlAnnotate
*/
export function myDirective(args: { credits: Int }) {
// ...
}
```

## Output

### Error Report

```text
src/tests/fixtures/directives/directiveOnDirectiveDefinitionLocation.invalid.ts:3:4 - error: "DIRECTIVE_DEFINITION" is not a valid directive location. Valid locations are: QUERY, MUTATION, SUBSCRIPTION, FIELD, FRAGMENT_DEFINITION, FRAGMENT_SPREAD, INLINE_FRAGMENT, VARIABLE_DEFINITION, SCHEMA, SCALAR, OBJECT, FIELD_DEFINITION, ARGUMENT_DEFINITION, INTERFACE, UNION, ENUM, ENUM_VALUE, INPUT_OBJECT, INPUT_FIELD_DEFINITION.

3 * @gqlDirective on DIRECTIVE_DEFINITION
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 * @gqlAnnotate
~~~
```
Loading