-
-
Notifications
You must be signed in to change notification settings - Fork 2
Type complexity algorithm #34
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e0e540d
laid out the framework for the type complxity analysis algorithm
evanmcneely 610d7b5
commiting changes. considering overall strategy
evanmcneely cb183c0
started to refactor the complexity analysis to use seperate functions…
evanmcneely 92a0a6a
refactored the type complexity analysis to use several functions, eac…
evanmcneely 8f5f86e
debugging comlpexity algorithm
evanmcneely 57a8cd8
type complexity analysis working for first test. changed i + 1 to i += 1
evanmcneely 9beb1ce
passing all test up to and including aliases
evanmcneely 6923c53
resolved merge conflicts with dev
evanmcneely 8d09a5c
added a chart to describe the AST node connections
evanmcneely c464162
Merge branch 'dev' of github.com:oslabs-beta/GraphQL-Gate into em/typ…
shalarewicz ece955b
Merge branch 'dev' into em/typeComplexityAlgo
shalarewicz 4ea037f
exported types for succesful typescript build.
shalarewicz 9ef5624
lint fix
shalarewicz 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 hidden or 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 |
|---|---|---|
|
|
@@ -26,5 +26,5 @@ | |
| "error" | ||
| ] | ||
| }, | ||
| "ignorePatterns": ["jest.*"] | ||
| "ignorePatterns": ["jest.*", "build/*"] | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -102,3 +102,5 @@ dist | |
|
|
||
| # TernJS port file | ||
| .tern-port | ||
|
|
||
| build/* | ||
This file contains hidden or 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,21 @@ | ||
| { | ||
| // Use IntelliSense to learn about possible attributes. | ||
| // Hover to view descriptions of existing attributes. | ||
| // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
| "version": "0.2.0", | ||
| "configurations": [{ | ||
| "type": "node", | ||
| "request": "launch", | ||
| "name": "Jest Tests", | ||
| "program": "${workspaceRoot}/node_modules/jest/bin/jest.js", | ||
| "args": [ | ||
| "-i", "--verbose", "--no-cache" | ||
| ], | ||
| // "preLaunchTask": "build", | ||
| // "internalConsoleOptions": "openOnSessionStart", | ||
| // "outFiles": [ | ||
| // "${workspaceRoot}/dist/**/*" | ||
| // ], | ||
| // "envFile": "${workspaceRoot}/.env" | ||
| }] | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,157 @@ | ||
| /* eslint-disable @typescript-eslint/no-use-before-define */ | ||
| import { | ||
| DocumentNode, | ||
| FieldNode, | ||
| SelectionSetNode, | ||
| DefinitionNode, | ||
| Kind, | ||
| SelectionNode, | ||
| ArgumentNode, | ||
| } from 'graphql'; | ||
| import { FieldWeight, TypeWeightObject } from '../@types/buildTypeWeights'; | ||
|
|
||
| // TODO: handle variables and arguments | ||
| // ! this is not functional | ||
| const getArgObj = (args: ArgumentNode[]): { [index: string]: any } => { | ||
shalarewicz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const argObj: { [index: string]: any } = {}; | ||
| for (let i = 0; i < args.length; i + 1) { | ||
| const node = args[i]; | ||
| if (args[i].value.kind !== Kind.VARIABLE) { | ||
| if (args[i].value.kind === Kind.INT) { | ||
| // FIXME: this does not work | ||
| argObj[args[i].name.value] = args[i].value; | ||
| } | ||
| } | ||
| } | ||
| return argObj; | ||
| }; | ||
| /** | ||
| * The AST node functions call each other following the nested structure below | ||
| * Each function handles a specific GraphQL AST node type | ||
| * | ||
| * AST nodes call each other in the following way | ||
| * | ||
| * Document Node | ||
| * | | ||
| * Definiton Node | ||
| * (operation and fragment definitons) | ||
| * / \ | ||
| * |-----> Selection Set Node not done | ||
| * | / | ||
| * | Selection Node | ||
| * | (Field, Inline fragment and fragment spread) | ||
| * | | \ \ | ||
| * |--Field Node not done not done | ||
| * | ||
| */ | ||
|
|
||
| export function fieldNode( | ||
| node: FieldNode, | ||
| typeWeights: TypeWeightObject, | ||
| variables: any | undefined, | ||
| parentName: string | ||
| ): number { | ||
| let complexity = 0; | ||
| // console.log('fieldNode', node, parentName); | ||
| // check if the field name is in the type weight object. | ||
| if (node.name.value.toLocaleLowerCase() in typeWeights) { | ||
| // if it is, than the field is an object type, add itss type weight to the total | ||
| complexity += typeWeights[node.name.value].weight; | ||
| // call the function to handle selection set node with selectionSet property if it is not undefined | ||
| if (node.selectionSet) { | ||
| complexity += selectionSetNode( | ||
| node.selectionSet, | ||
| typeWeights, | ||
| variables, | ||
| node.name.value | ||
| ); | ||
| } | ||
| } else { | ||
| // otherwise the field is a scalar or a list. | ||
| const fieldWeight: FieldWeight = typeWeights[parentName].fields[node.name.value]; | ||
| if (typeof fieldWeight === 'number') { | ||
| // if the feild weight is a number, add the number to the total complexity | ||
| complexity += fieldWeight; | ||
| } else if (node.arguments) { | ||
| // otherwise the the feild weight is a list, invoke the function with variables | ||
| // TODO: calculate the complexity for lists with arguments and varibales | ||
| // ! this is not functional | ||
| // iterate through the arguments to build the object to | ||
| complexity += fieldWeight([...node.arguments]); | ||
| } | ||
| } | ||
| return complexity; | ||
| } | ||
|
|
||
| export function selectionNode( | ||
| node: SelectionNode, | ||
| typeWeights: TypeWeightObject, | ||
| variables: any | undefined, | ||
| parentName: string | ||
| ): number { | ||
| let complexity = 0; | ||
| // console.log('selectionNode', node, parentName); | ||
| // check the kind property against the set of selection nodes that are possible | ||
| if (node.kind === Kind.FIELD) { | ||
| // call the function that handle field nodes | ||
| complexity += fieldNode(node, typeWeights, variables, parentName); | ||
| } | ||
| // TODO: add checks for Kind.FRAGMENT_SPREAD and Kind.INLINE_FRAGMENT here | ||
| return complexity; | ||
| } | ||
|
|
||
| export function selectionSetNode( | ||
| node: SelectionSetNode, | ||
| typeWeights: TypeWeightObject, | ||
| variables: any | undefined, | ||
| parentName: string | ||
| ): number { | ||
| let complexity = 0; | ||
| // iterate shrough the 'selections' array on the seletion set node | ||
| for (let i = 0; i < node.selections.length; i += 1) { | ||
| // call the function to handle seletion nodes | ||
| // pass the current parent through because selection sets act only as intermediaries | ||
| complexity += selectionNode(node.selections[i], typeWeights, variables, parentName); | ||
| } | ||
| return complexity; | ||
| } | ||
|
|
||
| export function definitionNode( | ||
| node: DefinitionNode, | ||
| typeWeights: TypeWeightObject, | ||
| variables: any | undefined | ||
| ): number { | ||
| let complexity = 0; | ||
| // check the kind property against the set of definiton nodes that are possible | ||
| if (node.kind === Kind.OPERATION_DEFINITION) { | ||
| // check if the operation is in the type weights object. | ||
| if (node.operation.toLocaleLowerCase() in typeWeights) { | ||
| // if it is, it is an object type, add it's type weight to the total | ||
| complexity += typeWeights[node.operation].weight; | ||
| // call the function to handle selection set node with selectionSet property if it is not undefined | ||
| if (node.selectionSet) | ||
| complexity += selectionSetNode( | ||
| node.selectionSet, | ||
| typeWeights, | ||
| variables, | ||
| node.operation | ||
| ); | ||
| } | ||
| } | ||
| // TODO: add checks for Kind.FRAGMENT_DEFINITION here (there are other type definition nodes that i think we can ignore. see ast.d.ts in 'graphql') | ||
| return complexity; | ||
| } | ||
|
|
||
| export function documentNode( | ||
| node: DocumentNode, | ||
| typeWeights: TypeWeightObject, | ||
| variables: any | undefined | ||
| ): number { | ||
| let complexity = 0; | ||
| // iterate through 'definitions' array on the document node | ||
| for (let i = 0; i < node.definitions.length; i += 1) { | ||
| // call the function to handle the various types of definition nodes | ||
| complexity += definitionNode(node.definitions[i], typeWeights, variables); | ||
| } | ||
| return complexity; | ||
| } | ||
This file contains hidden or 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 hidden or 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,28 +1,25 @@ | ||
| import { DocumentNode } from 'graphql'; | ||
| import { TypeWeightObject } from '../@types/buildTypeWeights'; | ||
| import { documentNode } from './ASTnodefunctions'; | ||
|
|
||
| /** | ||
| * This function should | ||
| * 1. validate the query using graphql methods | ||
| * 2. parse the query string using the graphql parse method | ||
| * 3. itreate through the query AST and | ||
| * - cross reference the type weight object to check type weight | ||
| * - total all the eweights of all types in the query | ||
| * 4. return the total as the query complexity | ||
| * Calculate the complexity for the query by recursivly traversing through the query AST, | ||
| * checking the query fields against the type weight object and totaling the weights of every field. | ||
| * | ||
| * TO DO: extend the functionality to work for mutations and subscriptions | ||
| * TO DO: extend the functionality to work for mutations and subscriptions and directives | ||
| * | ||
| * @param {string} queryString | ||
| * @param {TypeWeightObject} typeWeights | ||
| * @param {string} queryAST | ||
| * @param {any | undefined} varibales | ||
| * @param {string} complexityOption | ||
| * @param {TypeWeightObject} typeWeights | ||
| */ | ||
| // TODO add queryVaribables parameter | ||
| function getQueryTypeComplexity( | ||
| queryString: DocumentNode, | ||
| varibales: any | undefined, | ||
| queryAST: DocumentNode, | ||
| variables: any | undefined, | ||
| typeWeights: TypeWeightObject | ||
| ): number { | ||
| throw Error('getQueryComplexity is not implemented.'); | ||
| let complexity = 0; | ||
| complexity += documentNode(queryAST, typeWeights, variables); | ||
| return complexity; | ||
| } | ||
|
|
||
| export default getQueryTypeComplexity; |
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.
Uh oh!
There was an error while loading. Please reload this page.