Skip to content

Commit

Permalink
Replace = with as
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed Jun 19, 2017
1 parent 9771485 commit c9b713f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/language/__tests__/schema-kitchen-sink.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ union AnnotatedUnionTwo @onUnion = | A | B

scalar CustomScalar

scalar StringEncodedCustomScalar = String
scalar StringEncodedCustomScalar as String

scalar AnnotatedScalar @onScalar

Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/schema-printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ union AnnotatedUnionTwo @onUnion = A | B
scalar CustomScalar
scalar StringEncodedCustomScalar = String
scalar StringEncodedCustomScalar as String
scalar AnnotatedScalar @onScalar
Expand Down
22 changes: 18 additions & 4 deletions src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/language/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
5 changes: 3 additions & 2 deletions src/type/__tests__/introspection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
},
{
Expand Down
6 changes: 3 additions & 3 deletions src/type/introspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 8 additions & 18 deletions src/utilities/__tests__/schemaPrinter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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!]
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/utilities/schemaPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c9b713f

Please sign in to comment.