Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { GraphQLScalarType, Kind } from 'graphql';
const parseValue = value => value; // value from the client
const serialize = value => {
// value sent to the client
value.buffer = value.buffer.toString('base64');
return value;
};
const parseLiteral = ast => ast.value; // ast is always string format
const ImageScalarType = new GraphQLScalarType({
name: 'ImageScalar',
description: 'Image manipulating scalar',
parseValue,
serialize,
parseLiteral
});
export default ImageScalarType;
const { GraphQLScalarType } = require('graphql')
const phone = require('phone')
const TOLL_FREE_PREFIX = ['800', '844', '855', '866', '877', '888']
const parse = (value) => {
const num = String(value).replace(/\D/g, '').slice(-10)
if (num.length === 10 && TOLL_FREE_PREFIX.some(v => v === num.substr(0, 3))) {
return `+1${num}`
}
return phone(String(value))[0]
}
// Index 0 is phone, index 1 is country code
module.exports = new GraphQLScalarType({
name: 'PhoneNumber',
serialize: parse,
parseValue: parse,
parseLiteral (ast) {
return parse(ast.value)
}
})
import contentResolvers from './content';
import contentRemoteResolvers from './content_remote';
import { GraphQLScalarType } from 'graphql';
import { Kind } from 'graphql/language';
import userResolvers from './user';
import userRemoteResolvers from './user_remote';
const exampleResolvers = {
Date: new GraphQLScalarType({
name: 'Date',
description: 'Date custom scalar type',
parseValue(value) {
return new Date(value); // value from the client
},
serialize(value) {
return value.getTime(); // value sent to the client
},
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
return parseInt(ast.value, 10); // ast value is always in string format
}
return null;
},
}),
import { BigNumber } from "bignumber.js";
import { ASTNode, GraphQLScalarType } from "graphql";
import * as language from "graphql/language/kinds";
export const BigNumberScalar = new GraphQLScalarType({
name: "BigNumber",
description:
"The BigNumber scalar type represents numeric values with precision as in https://github.com/MikeMcl/bignumber.js/",
serialize(value: BigNumber): string {
return new BigNumber(value).toString();
},
parseValue(value: string): string {
return new BigNumber(value).toString(10);
},
// value received from the client
parseLiteral(ast: ASTNode): string | null {
switch (ast.kind) {
case language.Kind.INT:
case language.Kind.FLOAT:
return new BigNumber(ast.value).toString(10);
default:
}
function parseLiteral(
ast:
| { kind: typeof Kind.INT; value: number }
| { kind: typeof Kind.STRING; value: string }
| any
): null | Date {
if (ast.kind === Kind.INT || ast.kind === Kind.STRING) {
return parseValue(ast.value);
}
return null;
}
export const GraphQLTimestamp = new GraphQLScalarType({
name: "Timestamp",
description:
"The javascript `Date` as integer. Type represents date and time as number of milliseconds from start of UNIX epoch.",
serialize,
parseValue,
parseLiteral
});
const parseObjectFields = fields => {
if (Array.isArray(fields)) {
return fields.reduce(
(object, field) => ({
...object,
[field.name.value]: parseValue(field.value),
}),
{}
);
}
throw new TypeValidationError(fields, 'Object');
};
const ANY = new GraphQLScalarType({
name: 'Any',
description:
'The Any scalar type is used in operations and types that involve any type of value.',
parseValue: value => value,
serialize: value => value,
parseLiteral: ast => parseValue(ast),
});
const OBJECT = new GraphQLScalarType({
name: 'Object',
description:
'The Object scalar type is used in operations and types that involve objects.',
parseValue(value) {
if (typeof value === 'object') {
return value;
}
throw new TSError(`${keyName}: ${keyValue} is not a valid string`);
}
}
if (keyName === 'array') {
if (keyValue != null && typeof keyValue !== 'boolean') {
throw new TSError(`${keyName}: ${keyValue} is not a valid boolean`);
}
}
accum[fieldName] = { [keyName]: keyValue };
return accum;
}, {});
}
export const GraphQLDataType = new GraphQLScalarType({
name: 'GraphQLDataType',
description: 'Scalar type for data type configs',
serialize,
parseValue,
parseLiteral,
});
export function formatSchema(schemaStr: string) {
const schema = buildSchema(schemaStr, { commentDescriptions: true });
return printSchema(schema, {
commentDescriptions: true
});
}
parseLiteral: function parseLiteral(ast) {
switch (ast.kind) {
case _language.Kind.INT:
return ast.value;
case _language.Kind.LIST:
return ast.values.map(timeRange.parseLiteral);
default:
return null;
}
}
};
exports.default = {
schema: '\n scalar TimeRange\n ',
resolvers: {
TimeRange: new _graphql.GraphQLScalarType(timeRange)
}
};
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNtcy9ncmFwaHF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IHBhcnNlTGl0ZXJhbDogKGFzdCkgPT4ge1xuICAgIHN3aXRjaCAoYXN0LmtpbmQpIHtcbiAgICAgIGNhc2UgS2luZC5JTlQ6XG4gICAgICAgIHJldHVybiBhc3QudmFsdWU7XG4gICAgICBjYXNlIEtpbmQuTElTVDpcbiAgICAgICAgcmV0dXJuIGFzdC52YWx1ZXMubWFwKHRpbWVSYW5nZS5wYXJzZUxpdGVyYWwpO1xuICAgICAgZGVmYXVsdDpcbiAgICAgICAgcmV0dXJuIG51bGw7XG4gICAgfVxuICB9LFxufTtcblxuXG5leHBvcnQgZGVmYXVsdCB7XG4gIHNjaGVtYTogYFxuICAgIHNjYWxhciBUaW1lUmFuZ2VcbiAgYCxcbiAgcmVzb2x2ZXJzOiB7XG4gICAgVGltZVJhbmdlOiBuZXcgR3JhcGhRTFNjYWxhclR5cGUodGltZVJhbmdlKSxcbiAgfSxcbn07XG4iXX0=
});
return value;
}
case _language.Kind.LIST:
return ast.values.map(json.parseLiteral);
default:
return null;
}
}
};
exports.default = {
schema: '\n scalar Json\n ',
resolvers: {
Json: new _graphql.GraphQLScalarType(json)
}
};
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNtcy9ncmFwaHFsL3NlcnZlci9zY2FsYXJzL2pzb24uZXM2Il0sIm5hbWVzIjpbImpzb24iLCJuYW1lIiwiZGVzY3JpcHRpb24iLCJzZXJpYWxpemUiLCJ2YWx1ZSIsInBhcnNlVmFsdWUiLCJwYXJzZUxpdGVyYWwiLCJhc3QiLCJraW5kIiwiU1RSSU5HIiwiQk9PTEVBTiIsIklOVCIsIkZMT0FUIiwicGFyc2VGbG9hdCIsIk9CSkVDVCIsIk9iamVjdCIsImNyZWF0ZSIsImZpZWxkcyIsImZvckVhY2giLCJmaWVsZCIsIkxJU1QiLCJ2YWx1ZXMiLCJtYXAiLCJzY2hlbWEiLCJyZXNvbHZlcnMiLCJKc29uIl0sIm1hcHBpbm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AgICAgICAgcmV0dXJuIHZhbHVlO1xuICAgICAgfVxuICAgICAgY2FzZSBLaW5kLkxJU1Q6XG4gICAgICAgIHJldHVybiBhc3QudmFsdWVzLm1hcChqc29uLnBhcnNlTGl0ZXJhbCk7XG4gICAgICBkZWZhdWx0OlxuICAgICAgICByZXR1cm4gbnVsbDtcbiAgICB9XG4gIH0sXG59O1xuXG5leHBvcnQgZGVmYXVsdCB7XG4gIHNjaGVtYTogYFxuICAgIHNjYWxhciBKc29uXG4gIGAsXG4gIHJlc29sdmVyczoge1xuICAgIEpzb246IG5ldyBHcmFwaFFMU2NhbGFyVHlwZShqc29uKSxcbiAgfSxcbn07XG4iXX0=
deprecationReason: value.deprecationReason,
description: value.description,
astNode: value.astNode,
};
});
return new GraphQLEnumType({
name: type.name,
description: type.description,
astNode: type.astNode,
values: newValues,
});
} else if (type instanceof GraphQLScalarType) {
if (keepResolvers || isSpecifiedScalarType(type)) {
return type;
} else {
return new GraphQLScalarType({
name: type.name,
description: type.description,
astNode: type.astNode,
serialize(value: any) {
return value;
},
parseValue(value: any) {
return value;
},
parseLiteral(ast: ValueNode) {
return parseLiteral(ast);
},
});
}
} else {
throw new Error(`Invalid type ${type}`);