-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: must sent stringified json to pg_graphql
- Loading branch information
1 parent
adc29d9
commit 8ad9895
Showing
5 changed files
with
79 additions
and
29 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
14 changes: 0 additions & 14 deletions
14
...rver/src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars/json.scalar.ts
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
.../src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars/raw-json.scalar.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,74 @@ | ||
import { GraphQLScalarType } from 'graphql'; | ||
import { Maybe } from 'graphql-yoga'; | ||
import { ObjMap } from 'graphql/jsutils/ObjMap'; | ||
import { ASTNode, Kind, ValueNode } from 'graphql/language'; | ||
|
||
const parseLiteral = ( | ||
ast: ValueNode, | ||
variables?: Maybe<ObjMap<unknown>>, | ||
): any => { | ||
switch (ast.kind) { | ||
case Kind.STRING: | ||
case Kind.BOOLEAN: | ||
return ast.value; | ||
case Kind.INT: | ||
case Kind.FLOAT: | ||
return parseFloat(ast.value); | ||
case Kind.OBJECT: | ||
return parseObject(ast as any, variables); | ||
case Kind.LIST: | ||
return (ast as any).values.map((n: ValueNode) => | ||
parseLiteral(n, variables), | ||
); | ||
case Kind.NULL: | ||
return null; | ||
case Kind.VARIABLE: | ||
return variables ? variables[ast.name.value] : undefined; | ||
default: | ||
throw new TypeError( | ||
`JSONStringify cannot represent value: ${JSON.stringify(ast)}`, | ||
); | ||
} | ||
}; | ||
|
||
const parseObject = ( | ||
ast: ASTNode, | ||
variables?: Maybe<ObjMap<unknown>>, | ||
): object => { | ||
const value = Object.create(null); | ||
|
||
if ('fields' in ast) { | ||
ast.fields?.forEach((field: any) => { | ||
value[field.name.value] = parseLiteral(field.value, variables); | ||
}); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
const stringify = (value: any): string => { | ||
return JSON.stringify(value); | ||
}; | ||
|
||
const parseJSON = (value: string): object => { | ||
try { | ||
return JSON.parse(value); | ||
} catch (e) { | ||
throw new TypeError(`Value is not valid JSON: ${value}`); | ||
} | ||
}; | ||
|
||
export const RawJSONScalar = new GraphQLScalarType({ | ||
name: 'RawJSONScalar', | ||
description: | ||
'The `RawJSONScalar` scalar type represents JSON values, but stringifies inputs and parses outputs.', | ||
serialize: parseJSON, | ||
parseValue: stringify, | ||
parseLiteral: (ast, variables) => { | ||
if (ast.kind === Kind.STRING) { | ||
return stringify(ast.value); | ||
} else { | ||
return stringify(parseLiteral(ast, variables)); | ||
} | ||
}, | ||
}); |
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