-
-
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.
feat(parses.ts): add
parseGraphQLParameters
that parse value as Gra…
…phQL parameters
- Loading branch information
1 parent
3675fc3
commit be9128b
Showing
3 changed files
with
93 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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { has, isNull, isPlainObject, isString } from "./deps.ts"; | ||
import { GraphQLParameters } from "./types.ts"; | ||
|
||
/** Parse value as {@link GraphQLParameters}. */ | ||
export function parseGraphQLParameters( | ||
value: unknown, | ||
): [data: GraphQLParameters] | [data: undefined, error: TypeError] { | ||
if (!isPlainObject(value)) { | ||
return [ | ||
, | ||
TypeError( | ||
`Invalid field. "payload" must be plain object.`, | ||
), | ||
]; | ||
} | ||
|
||
if (!has(value, "query")) { | ||
return [ | ||
, | ||
TypeError( | ||
`Missing field. "query"`, | ||
), | ||
]; | ||
} | ||
|
||
if (!isString(value.query)) { | ||
return [ | ||
, | ||
TypeError( | ||
`Invalid field. "query" must be string.`, | ||
), | ||
]; | ||
} | ||
|
||
if ( | ||
has(value, "variables") && | ||
(!isNull(value.variables) && !isPlainObject(value.variables)) | ||
) { | ||
return [ | ||
, | ||
TypeError( | ||
`Invalid field. "variables" must be plain object or null`, | ||
), | ||
]; | ||
} | ||
if ( | ||
has(value, "operationName") && | ||
(!isNull(value.operationName) && !isString(value.operationName)) | ||
) { | ||
return [ | ||
, | ||
TypeError( | ||
`Invalid field. "operationName" must be string or null.`, | ||
), | ||
]; | ||
} | ||
if ( | ||
has(value, "extensions") && | ||
(!isNull(value.extensions) && !isPlainObject(value.extensions)) | ||
) { | ||
return [ | ||
, | ||
TypeError( | ||
`Invalid field. "extensions" must be plain object or null`, | ||
), | ||
]; | ||
} | ||
|
||
const { query, ...rest } = value; | ||
|
||
return [{ | ||
operationName: null, | ||
variableValues: null, | ||
extensions: null, | ||
query, | ||
...rest, | ||
}]; | ||
} |