Skip to content

Commit 933307a

Browse files
committed
support: parse schema directive
1 parent fb8fd75 commit 933307a

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

schema/directive.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ type Location struct {
1212
}
1313

1414
type Directive struct {
15-
Name []byte
16-
Arguments []*DirectiveArgument
17-
Locations []*Location
15+
Name []byte
16+
Arguments []*DirectiveArgument
17+
Locations []*Location
18+
Extentions []*Directive
1819
}
1920

2021
type Directives []*Directive
@@ -39,6 +40,7 @@ type DirectiveDefinition struct {
3940
Arguments []*ArgumentDefinition
4041
Repeatable bool
4142
Locations []*Location
43+
Extentions []*DirectiveDefinition
4244
}
4345

4446
func (d *DirectiveDefinition) IsAllowedApplySchema() bool {

schema/parser.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,28 @@ func (p *Parser) parseExtendDefinition(schema *Schema, tokens Tokens, cur int) (
272272

273273
t.Extentions = append(t.Extentions, unionDefinition)
274274
case Scalar:
275-
// TODO: Support
275+
scalarDefinition, newCur, err := p.parseScalarDefinition(tokens, cur)
276+
if err != nil {
277+
return nil, 0, err
278+
}
279+
cur = newCur
280+
t := get(schema.Indexes, string(scalarDefinition.Name), scalarDefinition)
281+
if t == nil {
282+
return nil, 0, fmt.Errorf("%s is not defined", scalarDefinition.Name)
283+
}
284+
285+
t.Extentions = append(t.Extentions, scalarDefinition)
286+
case At:
287+
directive, newCur, err := p.parseDirectiveDefinition(tokens, cur)
288+
if err != nil {
289+
return nil, 0, err
290+
}
291+
cur = newCur
292+
t := get(schema.Indexes, string(directive.Name), directive)
293+
if t == nil {
294+
return nil, 0, fmt.Errorf("%s is not defined", directive.Name)
295+
}
296+
t.Extentions = append(t.Extentions, directive)
276297
}
277298

278299
return schema, cur, nil

schema/schema.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (o *OperationDefinition) GetFieldByName(name []byte) *FieldDefinition {
170170
}
171171

172172
type DefinitionType interface {
173-
*TypeDefinition | *OperationDefinition | *EnumDefinition | *UnionDefinition | *InterfaceDefinition | *InputDefinition | *ScalarDefinition
173+
*TypeDefinition | *OperationDefinition | *EnumDefinition | *UnionDefinition | *InterfaceDefinition | *InputDefinition | *ScalarDefinition | *DirectiveDefinition
174174
}
175175

176176
type Indexes struct {
@@ -181,6 +181,7 @@ type Indexes struct {
181181
InputIndex map[string]*InputDefinition
182182
OperationIndexes map[OperationType]map[string]*OperationDefinition
183183
ScalarIndex map[string]*ScalarDefinition
184+
DirectiveIndex map[string]*DirectiveDefinition
184185
}
185186

186187
func (i *Indexes) GetTypeDefinition(name string) *TypeDefinition {
@@ -675,6 +676,10 @@ func get[T DefinitionType](indexes *Indexes, key string, t T) T {
675676
return any(indexes.InterfaceIndex[key]).(T)
676677
case *InputDefinition:
677678
return any(indexes.InputIndex[key]).(T)
679+
case *ScalarDefinition:
680+
return any(indexes.ScalarIndex[key]).(T)
681+
case *DirectiveDefinition:
682+
return any(indexes.DirectiveIndex[key]).(T)
678683
}
679684

680685
return nil
@@ -683,6 +688,7 @@ func get[T DefinitionType](indexes *Indexes, key string, t T) T {
683688
type ScalarDefinition struct {
684689
Name []byte
685690
Directives []*Directive
691+
Extentions []*ScalarDefinition
686692
}
687693

688694
type SchemaDefinition struct {

0 commit comments

Comments
 (0)