-
Notifications
You must be signed in to change notification settings - Fork 2k
Add 'getDefinitionsMap' to 'ASTValidationContext' #1476
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,11 +47,9 @@ export function KnownDirectives( | |
locationsMap[directive.name] = directive.locations; | ||
} | ||
|
||
const astDefinitions = context.getDocument().definitions; | ||
for (const def of astDefinitions) { | ||
if (def.kind === Kind.DIRECTIVE_DEFINITION) { | ||
locationsMap[def.name.value] = def.locations.map(name => name.value); | ||
} | ||
const defNodes = context.getDefinitionsMap().DirectiveDefinition || []; | ||
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. I think it makes more sense to have both contexts have a One nice thing with not exposing it: we could later easily add a My big worry with |
||
for (const node of defNodes) { | ||
locationsMap[node.name.value] = node.locations.map(name => name.value); | ||
} | ||
|
||
return { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,38 +22,25 @@ export function unusedFragMessage(fragName: string): string { | |
* within operations, or spread within other fragments spread within operations. | ||
*/ | ||
export function NoUnusedFragments(context: ASTValidationContext): ASTVisitor { | ||
const operationDefs = []; | ||
const fragmentDefs = []; | ||
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. In the context of GQL documents 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. This is very clever. I like the idea behind it a lot. I'm not sure it makes sense to expose publicly yet, but for the internal rules it makes a lot of sense to use something like |
||
const fragmentNameUsed = Object.create(null); | ||
const operationDefs = context.getDefinitionsMap().OperationDefinition || []; | ||
for (const operation of operationDefs) { | ||
for (const fragment of context.getRecursivelyReferencedFragments( | ||
operation, | ||
)) { | ||
fragmentNameUsed[fragment.name.value] = true; | ||
} | ||
} | ||
|
||
return { | ||
OperationDefinition(node) { | ||
operationDefs.push(node); | ||
return false; | ||
}, | ||
FragmentDefinition(node) { | ||
fragmentDefs.push(node); | ||
const fragName = node.name.value; | ||
if (fragmentNameUsed[fragName] !== true) { | ||
context.reportError( | ||
new GraphQLError(unusedFragMessage(fragName), [node]), | ||
); | ||
} | ||
return false; | ||
}, | ||
Document: { | ||
leave() { | ||
const fragmentNameUsed = Object.create(null); | ||
for (const operation of operationDefs) { | ||
for (const fragment of context.getRecursivelyReferencedFragments( | ||
operation, | ||
)) { | ||
fragmentNameUsed[fragment.name.value] = true; | ||
} | ||
} | ||
|
||
for (const fragmentDef of fragmentDefs) { | ||
const fragName = fragmentDef.name.value; | ||
if (fragmentNameUsed[fragName] !== true) { | ||
context.reportError( | ||
new GraphQLError(unusedFragMessage(fragName), [fragmentDef]), | ||
); | ||
} | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
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.
We could still move the
this._fragments
instantiation to here:No point incurring the cost of object-creation until someone actually asks for it.