-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: receiving and returning StorePropertyValue now works correctly
- Loading branch information
Showing
9 changed files
with
89 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
46 changes: 46 additions & 0 deletions
46
packages/api/src/platforms/vtex/resolvers/objectOrString.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,46 @@ | ||
import type { GraphQLScalarSerializer } from 'graphql' | ||
import { GraphQLScalarType } from 'graphql' | ||
import { Kind } from 'graphql/language' | ||
|
||
export const ObjectOrString = new GraphQLScalarType({ | ||
name: 'ObjectOrString', | ||
description: | ||
'A string or the string representation of an object (a stringified object).', | ||
parseValue: toObjectOrString, | ||
serialize: stringify, | ||
parseLiteral(ast) { | ||
if (ast.kind === Kind.STRING) { | ||
try { | ||
return JSON.parse(ast.value) | ||
} catch (e) { | ||
return ast.value | ||
} | ||
} | ||
|
||
return null | ||
}, | ||
}) | ||
|
||
function toObjectOrString(value: GraphQLScalarSerializer<any>) { | ||
if (typeof value === 'string') { | ||
try { | ||
return JSON.parse(value) | ||
} catch (e) { | ||
return value | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
function stringify(value: GraphQLScalarSerializer<any>) { | ||
if (typeof value === 'object') { | ||
return JSON.stringify(value) | ||
} | ||
|
||
if (typeof value === 'string') { | ||
return value | ||
} | ||
|
||
return null | ||
} |
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
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 @@ | ||
scalar ObjectOrString |
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