forked from n1ru4l/graphql-live-query
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
394 additions
and
4 deletions.
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
131 changes: 131 additions & 0 deletions
131
packages/graphql-live-query/src/getLiveQueryOperationThrottle.spec.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,131 @@ | ||
import { parse, getOperationAST } from "graphql"; | ||
import { getLiveQueryOperationThrottle } from "./getLiveQueryOperationThrottle"; | ||
|
||
test("operation without @live returns undefined", () => { | ||
const node = getOperationAST( | ||
parse(/* GraphQL */ ` | ||
query foo { | ||
foo | ||
} | ||
`) | ||
)!; | ||
|
||
expect(getLiveQueryOperationThrottle(node)).toBe(undefined); | ||
}); | ||
|
||
test("operation with @live but no throttle arg returns undefined", () => { | ||
const node = getOperationAST( | ||
parse(/* GraphQL */ ` | ||
query foo @live { | ||
foo | ||
} | ||
`) | ||
)!; | ||
|
||
expect(getLiveQueryOperationThrottle(node)).toBe(undefined); | ||
}); | ||
|
||
test("operation with @live and 'if' argument set to 'false' returns undefined", () => { | ||
const node = getOperationAST( | ||
parse(/* GraphQL */ ` | ||
query foo @live(if: false, throttle: 1000) { | ||
foo | ||
} | ||
`) | ||
)!; | ||
|
||
expect(getLiveQueryOperationThrottle(node)).toBe(undefined); | ||
}); | ||
|
||
test("operation with @live and 'throttle' set to an int returns the value", () => { | ||
const node = getOperationAST( | ||
parse(/* GraphQL */ ` | ||
query foo @live(throttle: 18395) { | ||
foo | ||
} | ||
`) | ||
)!; | ||
|
||
expect(getLiveQueryOperationThrottle(node)).toBe(18395); | ||
}); | ||
|
||
test("operation with @live and 'throttle' expects its value to be an int", () => { | ||
const node = getOperationAST( | ||
parse(/* GraphQL */ ` | ||
query foo @live(throttle: "12345") { | ||
foo | ||
} | ||
`) | ||
)!; | ||
|
||
expect(() => getLiveQueryOperationThrottle(node)).toThrow(); | ||
}); | ||
|
||
test("operation with @live and 'throttle' range checks its value", () => { | ||
const MAX_INT = 2147483647; | ||
const MIN_INT = -2147483648; | ||
|
||
let node = getOperationAST( | ||
parse(/* GraphQL */ ` | ||
query foo @live(throttle: ${MIN_INT - 1}) { | ||
foo | ||
} | ||
`) | ||
)!; | ||
|
||
expect(() => getLiveQueryOperationThrottle(node)).toThrow(); | ||
|
||
node = getOperationAST( | ||
parse(/* GraphQL */ ` | ||
query foo @live(throttle: ${MAX_INT + 1}) { | ||
foo | ||
} | ||
`) | ||
)!; | ||
|
||
expect(() => getLiveQueryOperationThrottle(node)).toThrow(); | ||
}); | ||
|
||
test("operation with @live and 'throttle' argument set to a variable returns the value", () => { | ||
const node = getOperationAST( | ||
parse(/* GraphQL */ ` | ||
query foo($throttle: Int!) @live(throttle: $throttle) { | ||
foo | ||
} | ||
`) | ||
)!; | ||
|
||
expect(getLiveQueryOperationThrottle(node, { throttle: 12345 })).toBe(12345); | ||
}); | ||
|
||
test("operation with @live and 'throttle' argument set to variable rejects non-int values", () => { | ||
let node = getOperationAST( | ||
parse(/* GraphQL */ ` | ||
query foo($bool: Boolean = false) @live(throttle: $bool) { | ||
foo | ||
} | ||
`) | ||
)!; | ||
|
||
expect(() => getLiveQueryOperationThrottle(node, { bool: true })).toThrow(); | ||
|
||
node = getOperationAST( | ||
parse(/* GraphQL */ ` | ||
query foo($str: String!) @live(throttle: $str) { | ||
foo | ||
} | ||
`) | ||
)!; | ||
|
||
expect(() => getLiveQueryOperationThrottle(node, { str: "invalid" })).toThrow(); | ||
|
||
node = getOperationAST( | ||
parse(/* GraphQL */ ` | ||
query foo($float: Float!) @live(throttle: $float) { | ||
foo | ||
} | ||
`) | ||
)!; | ||
|
||
expect(() => getLiveQueryOperationThrottle(node, { float: 14.5 })).toThrow(); | ||
}); |
78 changes: 78 additions & 0 deletions
78
packages/graphql-live-query/src/getLiveQueryOperationThrottle.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,78 @@ | ||
import type { DefinitionNode } from "graphql"; | ||
import { isSome, isNone, Maybe } from "./Maybe"; | ||
import { GraphQLError, print } from "graphql"; | ||
import { isLiveQueryOperationDefinitionNode } from "./isLiveQueryOperationDefinitionNode"; | ||
|
||
// As per the GraphQL Spec, Integers are only treated as valid when a valid | ||
// 32-bit signed integer, providing the broadest support across platforms. | ||
// | ||
// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because | ||
// they are internally represented as IEEE 754 doubles. | ||
const MAX_INT = 2147483647; | ||
const MIN_INT = -2147483648; | ||
|
||
function checkRange(name: string, value: number): number { | ||
if (value > MAX_INT || value < MIN_INT) { | ||
throw new GraphQLError( | ||
`${name} value cannot represent a non 32-bit signed integer value: ${value}`, | ||
); | ||
} | ||
return value; | ||
} | ||
|
||
function coerceVariable(name: string, inputValue: unknown): number { | ||
if (typeof inputValue !== 'number' || !Number.isInteger(inputValue)) { | ||
throw new GraphQLError( | ||
`${name} value is not an integer : ${inputValue}`, | ||
); | ||
} | ||
return checkRange(name, inputValue); | ||
} | ||
|
||
export const getLiveQueryOperationThrottle = ( | ||
input: DefinitionNode, | ||
variables?: Maybe<{ [key: string]: unknown }> | ||
): Maybe<number> => { | ||
if (!isLiveQueryOperationDefinitionNode(input)) { | ||
return undefined; | ||
} | ||
const liveDirective = input.directives?.find((d) => d.name.value === "live"); | ||
if (isNone(liveDirective)) { | ||
return undefined; | ||
} | ||
const throttleArgument = liveDirective.arguments?.find( | ||
(arg) => arg.name.value === "throttle" | ||
); | ||
if (isNone(throttleArgument)) { | ||
return undefined; | ||
} | ||
const valueNode = throttleArgument.value; | ||
if (valueNode.kind === "IntValue") { | ||
return checkRange('throttle', parseInt(valueNode.value, 10)); | ||
} | ||
if (valueNode.kind !== "Variable") { | ||
throw new GraphQLError( | ||
`Throttle is not an int or a variable: ${print(valueNode)}`, | ||
valueNode, | ||
); | ||
} | ||
|
||
const variableName = valueNode.name.value; | ||
|
||
if (isSome(variables) && isSome(variables[variableName])) { | ||
return coerceVariable(variableName, variables[variableName]); | ||
} | ||
|
||
const variableNode = input.variableDefinitions?.find( | ||
(def) => def.variable.name.value === variableName | ||
); | ||
|
||
if (variableNode?.defaultValue?.kind === "IntValue") { | ||
return coerceVariable(variableName, variableNode.defaultValue.value); | ||
} | ||
|
||
throw new GraphQLError( | ||
`throttle cannot represent non-integer values: ${print(valueNode)}`, | ||
valueNode, | ||
); | ||
}; |
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
Oops, something went wrong.