From b0885a038ec0e654962d69fb910ac86659279579 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Tue, 22 Mar 2016 15:35:37 -0700 Subject: [PATCH] Updating schema parser to more closely match current state of RFC https://github.com/facebook/graphql/pull/90 --- src/language/ast.js | 26 ++++++------ src/language/kinds.js | 4 +- src/language/parser.js | 87 +++++++++++++++++++++-------------------- src/language/printer.js | 8 ++-- 4 files changed, 65 insertions(+), 60 deletions(-) diff --git a/src/language/ast.js b/src/language/ast.js index f56da99462..20d4271737 100644 --- a/src/language/ast.js +++ b/src/language/ast.js @@ -77,9 +77,7 @@ export type Document = { export type Definition = OperationDefinition | FragmentDefinition - | TypeDefinition - | TypeExtensionDefinition - | DirectiveDefinition + | TypeSystemDefinition export type OperationDefinition = { kind: 'OperationDefinition'; @@ -256,15 +254,25 @@ export type NonNullType = { type: NamedType | ListType; } -// Type Definition +// Type System Definition -export type TypeDefinition = ObjectTypeDefinition +export type TypeSystemDefinition = TypeDefinition + | TypeExtensionDefinition + | DirectiveDefinition + +export type TypeDefinition = ScalarTypeDefinition + | ObjectTypeDefinition | InterfaceTypeDefinition | UnionTypeDefinition - | ScalarTypeDefinition | EnumTypeDefinition | InputObjectTypeDefinition +export type ScalarTypeDefinition = { + kind: 'ScalarTypeDefinition'; + loc?: ?Location; + name: Name; +} + export type ObjectTypeDefinition = { kind: 'ObjectTypeDefinition'; loc?: ?Location; @@ -303,12 +311,6 @@ export type UnionTypeDefinition = { types: Array; } -export type ScalarTypeDefinition = { - kind: 'ScalarTypeDefinition'; - loc?: ?Location; - name: Name; -} - export type EnumTypeDefinition = { kind: 'EnumTypeDefinition'; loc?: ?Location; diff --git a/src/language/kinds.js b/src/language/kinds.js index d586d89088..d27681425b 100644 --- a/src/language/kinds.js +++ b/src/language/kinds.js @@ -48,14 +48,16 @@ export const NAMED_TYPE = 'NamedType'; export const LIST_TYPE = 'ListType'; export const NON_NULL_TYPE = 'NonNullType'; +// Type System Definitions + // Type Definitions +export const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition'; export const OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition'; export const FIELD_DEFINITION = 'FieldDefinition'; export const INPUT_VALUE_DEFINITION = 'InputValueDefinition'; export const INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition'; export const UNION_TYPE_DEFINITION = 'UnionTypeDefinition'; -export const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition'; export const ENUM_TYPE_DEFINITION = 'EnumTypeDefinition'; export const ENUM_VALUE_DEFINITION = 'EnumValueDefinition'; export const INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition'; diff --git a/src/language/parser.js b/src/language/parser.js index 2c406c4e4c..2c726c40cf 100644 --- a/src/language/parser.js +++ b/src/language/parser.js @@ -39,13 +39,14 @@ import type { Type, NamedType, - TypeDefinition, + TypeSystemDefinition, + + ScalarTypeDefinition, ObjectTypeDefinition, FieldDefinition, InputValueDefinition, InterfaceTypeDefinition, UnionTypeDefinition, - ScalarTypeDefinition, EnumTypeDefinition, EnumValueDefinition, InputObjectTypeDefinition, @@ -85,12 +86,12 @@ import { LIST_TYPE, NON_NULL_TYPE, + SCALAR_TYPE_DEFINITION, OBJECT_TYPE_DEFINITION, FIELD_DEFINITION, INPUT_VALUE_DEFINITION, INTERFACE_TYPE_DEFINITION, UNION_TYPE_DEFINITION, - SCALAR_TYPE_DEFINITION, ENUM_TYPE_DEFINITION, ENUM_VALUE_DEFINITION, INPUT_OBJECT_TYPE_DEFINITION, @@ -185,8 +186,7 @@ function parseDocument(parser: Parser): Document { * Definition : * - OperationDefinition * - FragmentDefinition - * - TypeDefinition - * - TypeExtensionDefinition + * - TypeSystemDefinition */ function parseDefinition(parser: Parser): Definition { if (peek(parser, TokenKind.BRACE_L)) { @@ -202,14 +202,15 @@ function parseDefinition(parser: Parser): Definition { case 'fragment': return parseFragmentDefinition(parser); + // Note: the Type System IDL is an experimental non-spec addition. + case 'scalar': case 'type': case 'interface': case 'union': - case 'scalar': case 'enum': - case 'input': return parseTypeDefinition(parser); - case 'extend': return parseTypeExtensionDefinition(parser); - case 'directive': return parseDirectiveDefinition(parser); + case 'input': + case 'extend': + case 'directive': return parseTypeSystemDefinition(parser); } } @@ -649,34 +650,48 @@ export function parseNamedType(parser: Parser): NamedType { // Implements the parsing rules in the Type Definition section. /** + * TypeSystemDefinition : + * - TypeDefinition + * - TypeExtensionDefinition + * - DirectiveDefinition + * * TypeDefinition : + * - ScalarTypeDefinition * - ObjectTypeDefinition * - InterfaceTypeDefinition * - UnionTypeDefinition - * - ScalarTypeDefinition * - EnumTypeDefinition * - InputObjectTypeDefinition */ -function parseTypeDefinition(parser: Parser): TypeDefinition { - if (!peek(parser, TokenKind.NAME)) { - throw unexpected(parser); - } - switch (parser.token.value) { - case 'type': - return parseObjectTypeDefinition(parser); - case 'interface': - return parseInterfaceTypeDefinition(parser); - case 'union': - return parseUnionTypeDefinition(parser); - case 'scalar': - return parseScalarTypeDefinition(parser); - case 'enum': - return parseEnumTypeDefinition(parser); - case 'input': - return parseInputObjectTypeDefinition(parser); - default: - throw unexpected(parser); +function parseTypeSystemDefinition(parser: Parser): TypeSystemDefinition { + if (peek(parser, TokenKind.NAME)) { + switch (parser.token.value) { + case 'scalar': return parseScalarTypeDefinition(parser); + case 'type': return parseObjectTypeDefinition(parser); + case 'interface': return parseInterfaceTypeDefinition(parser); + case 'union': return parseUnionTypeDefinition(parser); + case 'enum': return parseEnumTypeDefinition(parser); + case 'input': return parseInputObjectTypeDefinition(parser); + case 'extend': return parseTypeExtensionDefinition(parser); + case 'directive': return parseDirectiveDefinition(parser); + } } + + throw unexpected(parser); +} + +/** + * ScalarTypeDefinition : scalar Name + */ +function parseScalarTypeDefinition(parser: Parser): ScalarTypeDefinition { + const start = parser.token.start; + expectKeyword(parser, 'scalar'); + const name = parseName(parser); + return { + kind: SCALAR_TYPE_DEFINITION, + name, + loc: loc(parser, start), + }; } /** @@ -816,20 +831,6 @@ function parseUnionMembers(parser: Parser): Array { return members; } -/** - * ScalarTypeDefinition : scalar Name - */ -function parseScalarTypeDefinition(parser: Parser): ScalarTypeDefinition { - const start = parser.token.start; - expectKeyword(parser, 'scalar'); - const name = parseName(parser); - return { - kind: SCALAR_TYPE_DEFINITION, - name, - loc: loc(parser, start), - }; -} - /** * EnumTypeDefinition : enum Name { EnumValueDefinition+ } */ diff --git a/src/language/printer.js b/src/language/printer.js index 7edc5d7c6d..c8a4c0053c 100644 --- a/src/language/printer.js +++ b/src/language/printer.js @@ -92,7 +92,10 @@ const printDocASTReducer = { ListType: ({ type }) => '[' + type + ']', NonNullType: ({ type }) => type + '!', - // Type Definitions + // Type System Definitions + + ScalarTypeDefinition: ({ name }) => + `scalar ${name}`, ObjectTypeDefinition: ({ name, interfaces, fields }) => 'type ' + name + ' ' + @@ -111,9 +114,6 @@ const printDocASTReducer = { UnionTypeDefinition: ({ name, types }) => `union ${name} = ${join(types, ' | ')}`, - ScalarTypeDefinition: ({ name }) => - `scalar ${name}`, - EnumTypeDefinition: ({ name, values }) => `enum ${name} ${block(values)}`,