UNPKG

3.83 kBJavaScriptView Raw
1// @flow
2
3import { GraphQLScalarType } from 'graphql'
4import { GQLBase } from './GQLBase'
5
6/**
7 * GQLScalars are how one might construct their own types for use within
8 * GraphQL with Lattice. The descriptions below should be sufficient to get
9 * you started with your own types. The SDL for a Scalar looks like this:
10 *
11 * ```
12 * scalar MyType
13 * ```
14 *
15 * @class GQLScalar
16 */
17export class GQLScalar extends GQLBase {
18 /**
19 * Determines the default type targeted by this GQLBase class. Any
20 * type will technically be valid but only will trigger special behavior
21 *
22 * @memberof GQLScalar
23 * @method ⬇︎⠀GQL_TYPE
24 * @static
25 * @const
26 *
27 * @return {Function} a type, such as `GraphQLObjectType` or
28 * `GraphQLInterfaceType`
29 */
30 static get GQL_TYPE(): Function {
31 return GraphQLScalarType;
32 }
33
34 /**
35 * The `serialize` method is called by GraphQL when the type is going to
36 * be sent to the client. Since values on the client are in the form of
37 * JSON, the return value of `serialize` can be any valid JSON value;
38 * String, Number, Array, Object, etc...
39 *
40 * @memberof GQLScalar
41 * @method serialize
42 * @static
43 *
44 * @param {mixed} value the value that needs to be converted for the
45 * downstream JSON client side result.
46 * @return {mixed} any valid JSON value
47 */
48 static serialize(value: mixed): mixed {
49 return value;
50 }
51
52 /**
53 * Parse value handles input from the client. In this form, the value is
54 * taken directly from the sent query. The type of the value can be nearly
55 * anything, but the `parseValue` function's job is to interpret the
56 * input and return the understood value.
57 *
58 * You could have a ColorBlind scalar type that took in a hexadecimal
59 * color string and converted it to a color scheme as seen by those with
60 * some form of color blindness. The value supplied to `parseValue` would
61 * be the input color. The modified color would be the output value.
62 *
63 * ```
64 * query {
65 * showMe(colorBlind: '#ff0000') {
66 * color
67 * }
68 * }
69 *
70 * // this might convert to #c65100
71 * ```
72 *
73 * This can also cover input sent in the form of variables. The variable
74 * can be of any valid JSON type.
75 *
76 * @memberof GQLScalar
77 * @method parseValue
78 * @static
79 *
80 * @param {mixed} value the input sent from a query that needs to be
81 * converted to an internal value for GraphQL to proceed
82 * @return {mixed} the converted output given the input; this will be purely
83 * how you want your scalars to function.
84 */
85 static parseValue(value: ?mixed): ?mixed {
86 return value;
87 }
88
89 /**
90 * Similar to `parseValue`, but rather than receiving the input values from
91 * a query or from a query variable, the data comes in the form of a parsed
92 * abstract syntax/source tree (AST). It is the job of `parseLiteral` to
93 * convert from an AST type to the desired output value.
94 *
95 * An example that converts all Strings to Numbers and vice versa
96 *
97 * ```javascript
98 * static parseLiteral(ast) {
99 * const { Kind } = require('graphql/language')
100 *
101 * switch (ast.kind) {
102 * case Kind.INT:
103 * case Kind.FLOAT:
104 * return String(ast.value)
105 * case Kind.STRING:
106 * return parseFloat(ast.value)
107 * default:
108 * return null;
109 * }
110 * }
111 * ```
112 *
113 * @memberof GQLScalar
114 * @method parseLiteral
115 * @static
116 *
117 * @param {Object} ast the parse value of the type given some literal SDL
118 * syntax. Presumably this is where you can choose to take a String, for
119 * example, and convert it to an integer when Kind.STRING is supplied.
120 * @return {mixed} the value of the conversion, given input.
121 */
122 static parseLiteral(ast: Object): ?mixed {
123 }
124}
125
126export default GQLScalar
\No newline at end of file