Parse type extensions

Signed-off-by: Eugen Wissner <belka@caraus.de>
This commit is contained in:
Eugen Wissner 2020-01-26 11:55:15 +01:00
parent b4a3c98114
commit a6bd2370b6
3 changed files with 96 additions and 6 deletions

View File

@ -9,7 +9,7 @@ and this project adheres to
## [Unreleased] ## [Unreleased]
### Added ### Added
- AST for the GraphQL schema. - AST for the GraphQL schema.
- Parser for the TypeSystemDefinition. - Type system definition parser.
- `Trans.argument`. - `Trans.argument`.
- Schema extension parser. - Schema extension parser.

View File

@ -305,7 +305,7 @@ data TypeExtension
| InterfaceTypeDirectivesExtension Name (NonEmpty Directive) | InterfaceTypeDirectivesExtension Name (NonEmpty Directive)
| UnionTypeUnionMemberTypesExtension | UnionTypeUnionMemberTypesExtension
Name [Directive] (UnionMemberTypes NonEmpty) Name [Directive] (UnionMemberTypes NonEmpty)
| UnionDirectivesExtension Name (NonEmpty Directive) | UnionTypeDirectivesExtension Name (NonEmpty Directive)
| EnumTypeEnumValuesDefinitionExtension | EnumTypeEnumValuesDefinitionExtension
Name [Directive] (NonEmpty EnumValueDefinition) Name [Directive] (NonEmpty EnumValueDefinition)
| EnumTypeDirectivesExtension Name (NonEmpty Directive) | EnumTypeDirectivesExtension Name (NonEmpty Directive)

View File

@ -46,6 +46,7 @@ typeSystemDefinition = schemaDefinition
typeSystemExtension :: Parser TypeSystemExtension typeSystemExtension :: Parser TypeSystemExtension
typeSystemExtension = SchemaExtension <$> schemaExtension typeSystemExtension = SchemaExtension <$> schemaExtension
<|> TypeExtension <$> typeExtension
<?> "TypeSystemExtension" <?> "TypeSystemExtension"
directiveDefinition :: Parser TypeSystemDefinition directiveDefinition :: Parser TypeSystemDefinition
@ -57,6 +58,7 @@ directiveDefinition = DirectiveDefinition
<*> argumentsDefinition <*> argumentsDefinition
<* symbol "on" <* symbol "on"
<*> directiveLocations <*> directiveLocations
<?> "DirectiveDefinition"
directiveLocations :: Parser (NonEmpty DirectiveLocation) directiveLocations :: Parser (NonEmpty DirectiveLocation)
directiveLocations = optional pipe directiveLocations = optional pipe
@ -98,6 +100,15 @@ typeDefinition = scalarTypeDefinition
<|> inputObjectTypeDefinition <|> inputObjectTypeDefinition
<?> "TypeDefinition" <?> "TypeDefinition"
typeExtension :: Parser TypeExtension
typeExtension = scalarTypeExtension
<|> objectTypeExtension
<|> interfaceTypeExtension
<|> unionTypeExtension
<|> enumTypeExtension
<|> inputObjectTypeExtension
<?> "TypeExtension"
scalarTypeDefinition :: Parser TypeDefinition scalarTypeDefinition :: Parser TypeDefinition
scalarTypeDefinition = ScalarTypeDefinition scalarTypeDefinition = ScalarTypeDefinition
<$> description <$> description
@ -106,6 +117,13 @@ scalarTypeDefinition = ScalarTypeDefinition
<*> directives <*> directives
<?> "ScalarTypeDefinition" <?> "ScalarTypeDefinition"
scalarTypeExtension :: Parser TypeExtension
scalarTypeExtension = ScalarTypeExtension
<$ extend "scalar"
<*> name
<*> NonEmpty.some directive
<?> "ScalarTypeExtension"
objectTypeDefinition :: Parser TypeDefinition objectTypeDefinition :: Parser TypeDefinition
objectTypeDefinition = ObjectTypeDefinition objectTypeDefinition = ObjectTypeDefinition
<$> description <$> description
@ -116,6 +134,26 @@ objectTypeDefinition = ObjectTypeDefinition
<*> braces (many fieldDefinition) <*> braces (many fieldDefinition)
<?> "ObjectTypeDefinition" <?> "ObjectTypeDefinition"
objectTypeExtension :: Parser TypeExtension
objectTypeExtension = extend "type"
>> try fieldsDefinitionExtension
<|> try directivesExtension
<|> implementsInterfacesExtension
<?> "ObjectTypeExtension"
where
fieldsDefinitionExtension = ObjectTypeFieldsDefinitionExtension
<$> name
<*> option (ImplementsInterfaces []) (implementsInterfaces sepBy1)
<*> directives
<*> braces (NonEmpty.some fieldDefinition)
directivesExtension = ObjectTypeDirectivesExtension
<$> name
<*> option (ImplementsInterfaces []) (implementsInterfaces sepBy1)
<*> NonEmpty.some directive
implementsInterfacesExtension = ObjectTypeImplementsInterfacesExtension
<$> name
<*> implementsInterfaces NonEmpty.sepBy1
description :: Parser Description description :: Parser Description
description = Description description = Description
<$> optional (string <|> blockString) <$> optional (string <|> blockString)
@ -130,6 +168,20 @@ unionTypeDefinition = UnionTypeDefinition
<*> option (UnionMemberTypes []) (unionMemberTypes sepBy1) <*> option (UnionMemberTypes []) (unionMemberTypes sepBy1)
<?> "UnionTypeDefinition" <?> "UnionTypeDefinition"
unionTypeExtension :: Parser TypeExtension
unionTypeExtension = extend "union"
>> try unionMemberTypesExtension
<|> directivesExtension
<?> "UnionTypeExtension"
where
unionMemberTypesExtension = UnionTypeUnionMemberTypesExtension
<$> name
<*> directives
<*> unionMemberTypes NonEmpty.sepBy1
directivesExtension = UnionTypeDirectivesExtension
<$> name
<*> NonEmpty.some directive
unionMemberTypes :: unionMemberTypes ::
Foldable t => Foldable t =>
(Parser Text -> Parser Text -> Parser (t NamedType)) -> (Parser Text -> Parser Text -> Parser (t NamedType)) ->
@ -149,16 +201,42 @@ interfaceTypeDefinition = InterfaceTypeDefinition
<*> braces (many fieldDefinition) <*> braces (many fieldDefinition)
<?> "InterfaceTypeDefinition" <?> "InterfaceTypeDefinition"
interfaceTypeExtension :: Parser TypeExtension
interfaceTypeExtension = extend "interface"
>> try fieldsDefinitionExtension
<|> directivesExtension
<?> "InterfaceTypeExtension"
where
fieldsDefinitionExtension = InterfaceTypeFieldsDefinitionExtension
<$> name
<*> directives
<*> braces (NonEmpty.some fieldDefinition)
directivesExtension = InterfaceTypeDirectivesExtension
<$> name
<*> NonEmpty.some directive
enumTypeDefinition :: Parser TypeDefinition enumTypeDefinition :: Parser TypeDefinition
enumTypeDefinition = EnumTypeDefinition enumTypeDefinition = EnumTypeDefinition
<$> description <$> description
<* symbol "enum" <* symbol "enum"
<*> name <*> name
<*> directives <*> directives
<*> enumValuesDefinition <*> listOptIn braces enumValueDefinition
<?> "EnumTypeDefinition" <?> "EnumTypeDefinition"
enumTypeExtension :: Parser TypeExtension
enumTypeExtension = extend "enum"
>> try enumValuesDefinitionExtension
<|> directivesExtension
<?> "EnumTypeExtension"
where where
enumValuesDefinition = listOptIn braces enumValueDefinition enumValuesDefinitionExtension = EnumTypeEnumValuesDefinitionExtension
<$> name
<*> directives
<*> braces (NonEmpty.some enumValueDefinition)
directivesExtension = EnumTypeDirectivesExtension
<$> name
<*> NonEmpty.some directive
inputObjectTypeDefinition :: Parser TypeDefinition inputObjectTypeDefinition :: Parser TypeDefinition
inputObjectTypeDefinition = InputObjectTypeDefinition inputObjectTypeDefinition = InputObjectTypeDefinition
@ -166,10 +244,22 @@ inputObjectTypeDefinition = InputObjectTypeDefinition
<* symbol "input" <* symbol "input"
<*> name <*> name
<*> directives <*> directives
<*> inputFieldsDefinition <*> listOptIn braces inputValueDefinition
<?> "InputObjectTypeDefinition" <?> "InputObjectTypeDefinition"
inputObjectTypeExtension :: Parser TypeExtension
inputObjectTypeExtension = extend "input"
>> try inputFieldsDefinitionExtension
<|> directivesExtension
<?> "InputObjectTypeExtension"
where where
inputFieldsDefinition = listOptIn braces inputValueDefinition inputFieldsDefinitionExtension = InputObjectTypeInputFieldsDefinitionExtension
<$> name
<*> directives
<*> braces (NonEmpty.some inputValueDefinition)
directivesExtension = InputObjectTypeDirectivesExtension
<$> name
<*> NonEmpty.some directive
enumValueDefinition :: Parser EnumValueDefinition enumValueDefinition :: Parser EnumValueDefinition
enumValueDefinition = EnumValueDefinition enumValueDefinition = EnumValueDefinition