Skip to content

Commit

Permalink
chore: move duplicated code to function
Browse files Browse the repository at this point in the history
  • Loading branch information
icazevedo committed May 18, 2022
1 parent fb39540 commit 4134943
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions packages/api/src/platforms/vtex/resolvers/objectOrString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ export const ObjectOrString = new GraphQLScalarType({
serialize: stringify,
parseLiteral(ast) {
if (ast.kind === Kind.STRING) {
try {
return JSON.parse(ast.value)
} catch (e) {
return ast.value
}
return getValueAsObjectOrString(ast.value)
}

return null
Expand All @@ -23,16 +19,20 @@ export const ObjectOrString = new GraphQLScalarType({

function toObjectOrString(value: GraphQLScalarSerializer<any>) {
if (typeof value === 'string') {
try {
return JSON.parse(value)
} catch (e) {
return value
}
return getValueAsObjectOrString(value)
}

return null
}

function getValueAsObjectOrString(value: string) {
try {
return JSON.parse(value)
} catch (e) {
return value
}
}

function stringify(value: GraphQLScalarSerializer<any>) {
if (typeof value === 'object') {
return JSON.stringify(value)
Expand Down

0 comments on commit 4134943

Please sign in to comment.