From c9b713f28d817dfee0643edf3a66e99bd0a06200 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Fri, 16 Jun 2017 16:54:11 -0700 Subject: [PATCH] Replace `=` with `as` --- .../__tests__/schema-kitchen-sink.graphql | 2 +- src/language/__tests__/schema-printer-test.js | 2 +- src/language/parser.js | 22 +++++++++++++--- src/language/printer.js | 2 +- src/type/__tests__/introspection-test.js | 5 ++-- src/type/introspection.js | 6 ++--- src/utilities/__tests__/schemaPrinter-test.js | 26 ++++++------------- src/utilities/schemaPrinter.js | 4 +-- 8 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/language/__tests__/schema-kitchen-sink.graphql b/src/language/__tests__/schema-kitchen-sink.graphql index 80be00b5a8..4ce45bb2ea 100644 --- a/src/language/__tests__/schema-kitchen-sink.graphql +++ b/src/language/__tests__/schema-kitchen-sink.graphql @@ -41,7 +41,7 @@ union AnnotatedUnionTwo @onUnion = | A | B scalar CustomScalar -scalar StringEncodedCustomScalar = String +scalar StringEncodedCustomScalar as String scalar AnnotatedScalar @onScalar diff --git a/src/language/__tests__/schema-printer-test.js b/src/language/__tests__/schema-printer-test.js index 3b4d29a3b2..57993bf331 100644 --- a/src/language/__tests__/schema-printer-test.js +++ b/src/language/__tests__/schema-printer-test.js @@ -87,7 +87,7 @@ union AnnotatedUnionTwo @onUnion = A | B scalar CustomScalar -scalar StringEncodedCustomScalar = String +scalar StringEncodedCustomScalar as String scalar AnnotatedScalar @onScalar diff --git a/src/language/parser.js b/src/language/parser.js index e12370883f..4cf8d53fce 100644 --- a/src/language/parser.js +++ b/src/language/parser.js @@ -784,14 +784,14 @@ function parseOperationTypeDefinition( } /** - * ScalarTypeDefinition : scalar Name ScalarOfType? Directives? - * ScalarOfType : = NamedType + * ScalarTypeDefinition : scalar Name SerializeAsType? Directives? + * SerializeAsType : as NamedType */ function parseScalarTypeDefinition(lexer: Lexer<*>): ScalarTypeDefinitionNode { const start = lexer.token; expectKeyword(lexer, 'scalar'); const name = parseName(lexer); - const type = skip(lexer, TokenKind.EQUALS) ? parseNamedType(lexer) : null; + const type = skipKeyword(lexer, 'as') ? parseNamedType(lexer) : null; const directives = parseDirectives(lexer); return { kind: SCALAR_TYPE_DEFINITION, @@ -1133,10 +1133,24 @@ function expect(lexer: Lexer<*>, kind: string): Token { } /** - * If the next token is a keyword with the given value, return that token after + * If the next token is a keyword with the given value, return true after * advancing the lexer. Otherwise, do not change the parser state and return * false. */ +function skipKeyword(lexer: Lexer<*>, value: string): boolean { + const token = lexer.token; + const match = token.kind === TokenKind.NAME && token.value === value; + if (match) { + lexer.advance(); + } + return match; +} + +/** + * If the next token is a keyword with the given value, return that token after + * advancing the lexer. Otherwise, do not change the parser state and throw + * an error. + */ function expectKeyword(lexer: Lexer<*>, value: string): Token { const token = lexer.token; if (token.kind === TokenKind.NAME && token.value === value) { diff --git a/src/language/printer.js b/src/language/printer.js index 783934cdbc..6045b5ed5a 100644 --- a/src/language/printer.js +++ b/src/language/printer.js @@ -106,7 +106,7 @@ const printDocASTReducer = { operation + ': ' + type, ScalarTypeDefinition: ({ name, type, directives }) => - join([ 'scalar', name, wrap('= ', type), join(directives, ' ') ], ' '), + join([ 'scalar', name, wrap('as ', type), join(directives, ' ') ], ' '), ObjectTypeDefinition: ({ name, interfaces, directives, fields }) => join([ diff --git a/src/type/__tests__/introspection-test.js b/src/type/__tests__/introspection-test.js index a556ecf252..b75c93cf32 100644 --- a/src/type/__tests__/introspection-test.js +++ b/src/type/__tests__/introspection-test.js @@ -1313,8 +1313,9 @@ describe('Introspection', () => { 'An enum describing what kind of type a given `__Type` is.', enumValues: [ { - description: 'Indicates this type is a scalar. ' + - '`ofType` is a valid field.', + description: + 'Indicates this type is a scalar. ' + + '`ofType` may represent how this scalar is serialized.', name: 'SCALAR' }, { diff --git a/src/type/introspection.js b/src/type/introspection.js index 5b5e241b34..3dc32d7b95 100644 --- a/src/type/introspection.js +++ b/src/type/introspection.js @@ -207,8 +207,8 @@ export const __Type = new GraphQLObjectType({ 'The fundamental unit of any GraphQL Schema is the type. There are ' + 'many kinds of types in GraphQL as represented by the `__TypeKind` enum.' + '\n\nDepending on the kind of a type, certain fields describe ' + - 'information about that type. Scalar types provide no information ' + - 'beyond a name and description, while Enum types provide their values. ' + + 'information about that type. Scalar types provide a name, description' + + 'and how they serialize, while Enum types provide their possible values. ' + 'Object and Interface types provide the fields they describe. Abstract ' + 'types, Union and Interface, provide the Object types possible ' + 'at runtime. List and NonNull types compose other types.', @@ -382,7 +382,7 @@ export const __TypeKind = new GraphQLEnumType({ SCALAR: { value: TypeKind.SCALAR, description: 'Indicates this type is a scalar. ' + - '`ofType` is a valid field.' + '`ofType` may represent how this scalar is serialized.' }, OBJECT: { value: TypeKind.OBJECT, diff --git a/src/utilities/__tests__/schemaPrinter-test.js b/src/utilities/__tests__/schemaPrinter-test.js index 52eaf3bb49..4bd22aa318 100644 --- a/src/utilities/__tests__/schemaPrinter-test.js +++ b/src/utilities/__tests__/schemaPrinter-test.js @@ -554,25 +554,15 @@ describe('Type System Printer', () => { query: Root } -<<<<<<< HEAD + scalar Even as Int + scalar Odd type Root { + even: Even odd: Odd } `); -======= -scalar Even = Int - -scalar Odd - -type Root { - even: Even - odd: Odd -} -` - ); ->>>>>>> RFC: Define custom scalars in terms of built-in scalars. }); it('Enum', () => { @@ -775,10 +765,10 @@ type Root { # types in GraphQL as represented by the \`__TypeKind\` enum. # # Depending on the kind of a type, certain fields describe information about that - # type. Scalar types provide no information beyond a name and description, while - # Enum types provide their values. Object and Interface types provide the fields - # they describe. Abstract types, Union and Interface, provide the Object types - # possible at runtime. List and NonNull types compose other types. + # type. Scalar types provide a name, descriptionand how they serialize, while Enum + # types provide their possible values. Object and Interface types provide the + # fields they describe. Abstract types, Union and Interface, provide the Object + # types possible at runtime. List and NonNull types compose other types. type __Type { description: String enumValues(includeDeprecated: Boolean = false): [__EnumValue!] @@ -793,7 +783,7 @@ type Root { # An enum describing what kind of type a given \`__Type\` is. enum __TypeKind { - # Indicates this type is a scalar. \`ofType\` is a valid field. + # Indicates this type is a scalar. \`ofType\` may represent how this scalar is serialized. SCALAR # Indicates this type is an object. \`fields\` and \`interfaces\` are valid fields. diff --git a/src/utilities/schemaPrinter.js b/src/utilities/schemaPrinter.js index 4ae61e4b99..ce2e8b63d8 100644 --- a/src/utilities/schemaPrinter.js +++ b/src/utilities/schemaPrinter.js @@ -153,9 +153,9 @@ export function printType(type: GraphQLType): string { } function printScalar(type: GraphQLScalarType): string { - const ofType = type.ofType ? ` = ${type.ofType.name}` : ''; + const serializeAsType = type.ofType ? ` as ${type.ofType.name}` : ''; return printDescription(type) + - `scalar ${type.name}${ofType}`; + `scalar ${type.name}${serializeAsType}`; } function printObject(type: GraphQLObjectType): string {