From b259c2c5b8fc0297a102600fc015f05488485d39 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 29 Mar 2019 14:32:31 -0400 Subject: [PATCH] Parse "subscription", also prepare for type system syntax --- graphql_parser/lib/src/language/ast/definition.dart | 6 ++++++ .../lib/src/language/ast/fragment_definition.dart | 2 +- .../lib/src/language/ast/operation_definition.dart | 9 +++++++-- graphql_parser/lib/src/language/lexer.dart | 1 + graphql_parser/lib/src/language/parser.dart | 4 +++- graphql_parser/lib/src/language/token_type.dart | 1 + graphql_server/test/mirrors_test.dart | 1 + 7 files changed, 20 insertions(+), 4 deletions(-) diff --git a/graphql_parser/lib/src/language/ast/definition.dart b/graphql_parser/lib/src/language/ast/definition.dart index d982eef2..efc72ee0 100644 --- a/graphql_parser/lib/src/language/ast/definition.dart +++ b/graphql_parser/lib/src/language/ast/definition.dart @@ -1,3 +1,9 @@ import 'node.dart'; abstract class DefinitionContext extends Node {} + +abstract class ExecutableDefinitionContext extends DefinitionContext {} + +abstract class TypeSystemDefinitionContext extends DefinitionContext {} + +abstract class TypeSystemExtensionContext extends DefinitionContext {} diff --git a/graphql_parser/lib/src/language/ast/fragment_definition.dart b/graphql_parser/lib/src/language/ast/fragment_definition.dart index 0fb9892a..23787e07 100644 --- a/graphql_parser/lib/src/language/ast/fragment_definition.dart +++ b/graphql_parser/lib/src/language/ast/fragment_definition.dart @@ -5,7 +5,7 @@ import 'package:source_span/source_span.dart'; import 'selection_set.dart'; import 'type_condition.dart'; -class FragmentDefinitionContext extends DefinitionContext { +class FragmentDefinitionContext extends ExecutableDefinitionContext { final Token FRAGMENT, NAME, ON; final TypeConditionContext typeCondition; final List directives = []; diff --git a/graphql_parser/lib/src/language/ast/operation_definition.dart b/graphql_parser/lib/src/language/ast/operation_definition.dart index 8334fb8b..06967ac7 100644 --- a/graphql_parser/lib/src/language/ast/operation_definition.dart +++ b/graphql_parser/lib/src/language/ast/operation_definition.dart @@ -6,7 +6,7 @@ import 'directive.dart'; import 'selection_set.dart'; import 'variable_definitions.dart'; -class OperationDefinitionContext extends DefinitionContext { +class OperationDefinitionContext extends ExecutableDefinitionContext { final Token TYPE, NAME; final VariableDefinitionsContext variableDefinitions; final List directives = []; @@ -14,13 +14,18 @@ class OperationDefinitionContext extends DefinitionContext { bool get isMutation => TYPE?.text == 'mutation'; + bool get isSubscription => TYPE?.text == 'subscription'; + bool get isQuery => TYPE?.text == 'query' || TYPE == null; String get name => NAME?.text; OperationDefinitionContext( this.TYPE, this.NAME, this.variableDefinitions, this.selectionSet) { - assert(TYPE == null || TYPE.text == 'query' || TYPE.text == 'mutation'); + assert(TYPE == null || + TYPE.text == 'query' || + TYPE.text == 'mutation' || + TYPE.text == 'subscription'); } @override diff --git a/graphql_parser/lib/src/language/lexer.dart b/graphql_parser/lib/src/language/lexer.dart index adb925ed..1859fb8d 100644 --- a/graphql_parser/lib/src/language/lexer.dart +++ b/graphql_parser/lib/src/language/lexer.dart @@ -29,6 +29,7 @@ final Map _patterns = { ')': TokenType.RPAREN, 'fragment': TokenType.FRAGMENT, 'mutation': TokenType.MUTATION, + 'subscription': TokenType.SUBSCRIPTION, 'on': TokenType.ON, 'query': TokenType.QUERY, 'null': TokenType.NULL, diff --git a/graphql_parser/lib/src/language/parser.dart b/graphql_parser/lib/src/language/parser.dart index 0e894c55..db1ef323 100644 --- a/graphql_parser/lib/src/language/parser.dart +++ b/graphql_parser/lib/src/language/parser.dart @@ -61,7 +61,9 @@ class Parser { if (selectionSet != null) return new OperationDefinitionContext(null, null, null, selectionSet); else { - if (next(TokenType.MUTATION) || next(TokenType.QUERY)) { + if (next(TokenType.MUTATION) || + next(TokenType.QUERY) || + next(TokenType.SUBSCRIPTION)) { var TYPE = current; Token NAME = next(TokenType.NAME) ? current : null; var variables = parseVariableDefinitions(); diff --git a/graphql_parser/lib/src/language/token_type.dart b/graphql_parser/lib/src/language/token_type.dart index a3d6ed0f..4f42d33a 100644 --- a/graphql_parser/lib/src/language/token_type.dart +++ b/graphql_parser/lib/src/language/token_type.dart @@ -15,6 +15,7 @@ enum TokenType { FRAGMENT, MUTATION, + SUBSCRIPTION, ON, QUERY, diff --git a/graphql_server/test/mirrors_test.dart b/graphql_server/test/mirrors_test.dart index 28c81aba..131300a3 100644 --- a/graphql_server/test/mirrors_test.dart +++ b/graphql_server/test/mirrors_test.dart @@ -43,6 +43,7 @@ void main() { }); } +@graphQLClass enum RomanceLanguage { SPANISH, FRANCE,