Skip to content

Commit 023af5a

Browse files
authored
Merge pull request #66 from haikusw/add_description_support
Add support for optional Description for those objects which allow it
2 parents 7fbca26 + 75cb871 commit 023af5a

File tree

5 files changed

+511
-39
lines changed

5 files changed

+511
-39
lines changed

Sources/GraphQL/Language/AST.swift

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -811,16 +811,18 @@ public final class StringValue {
811811
public let kind: Kind = .stringValue
812812
public let loc: Location?
813813
public let value: String
814+
public let block: Bool?
814815

815-
init(loc: Location? = nil, value: String) {
816+
init(loc: Location? = nil, value: String, block: Bool? = nil) {
816817
self.loc = loc
817818
self.value = value
819+
self.block = block
818820
}
819821
}
820822

821823
extension StringValue : Equatable {
822824
public static func == (lhs: StringValue, rhs: StringValue) -> Bool {
823-
return lhs.value == rhs.value
825+
return lhs.value == rhs.value && lhs.block == rhs.block
824826
}
825827
}
826828

@@ -1076,20 +1078,23 @@ public func == (lhs: TypeSystemDefinition, rhs: TypeSystemDefinition) -> Bool {
10761078
public final class SchemaDefinition {
10771079
public let kind: Kind = .schemaDefinition
10781080
public let loc: Location?
1081+
public let description: StringValue?
10791082
public let directives: [Directive]
10801083
public let operationTypes: [OperationTypeDefinition]
10811084

1082-
init(loc: Location? = nil, directives: [Directive], operationTypes: [OperationTypeDefinition]) {
1085+
init(loc: Location? = nil, description: StringValue? = nil, directives: [Directive], operationTypes: [OperationTypeDefinition]) {
10831086
self.loc = loc
1087+
self.description = description
10841088
self.directives = directives
10851089
self.operationTypes = operationTypes
10861090
}
10871091
}
10881092

10891093
extension SchemaDefinition : Equatable {
10901094
public static func == (lhs: SchemaDefinition, rhs: SchemaDefinition) -> Bool {
1091-
return lhs.directives == rhs.directives &&
1092-
lhs.operationTypes == rhs.operationTypes
1095+
return lhs.description == rhs.description &&
1096+
lhs.directives == rhs.directives &&
1097+
lhs.operationTypes == rhs.operationTypes
10931098
}
10941099
}
10951100

@@ -1157,33 +1162,38 @@ public func == (lhs: TypeDefinition, rhs: TypeDefinition) -> Bool {
11571162
public final class ScalarTypeDefinition {
11581163
public let kind: Kind = .scalarTypeDefinition
11591164
public let loc: Location?
1165+
public let description: StringValue?
11601166
public let name: Name
11611167
public let directives: [Directive]
11621168

1163-
init(loc: Location? = nil, name: Name, directives: [Directive] = []) {
1169+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, directives: [Directive] = []) {
11641170
self.loc = loc
1171+
self.description = description
11651172
self.name = name
11661173
self.directives = directives
11671174
}
11681175
}
11691176

11701177
extension ScalarTypeDefinition : Equatable {
11711178
public static func == (lhs: ScalarTypeDefinition, rhs: ScalarTypeDefinition) -> Bool {
1172-
return lhs.name == rhs.name &&
1179+
return lhs.description == rhs.description &&
1180+
lhs.name == rhs.name &&
11731181
lhs.directives == rhs.directives
11741182
}
11751183
}
11761184

11771185
public final class ObjectTypeDefinition {
11781186
public let kind: Kind = .objectTypeDefinition
11791187
public let loc: Location?
1188+
public let description: StringValue?
11801189
public let name: Name
11811190
public let interfaces: [NamedType]
11821191
public let directives: [Directive]
11831192
public let fields: [FieldDefinition]
11841193

1185-
init(loc: Location? = nil, name: Name, interfaces: [NamedType] = [], directives: [Directive] = [], fields: [FieldDefinition] = []) {
1194+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, interfaces: [NamedType] = [], directives: [Directive] = [], fields: [FieldDefinition] = []) {
11861195
self.loc = loc
1196+
self.description = description
11871197
self.name = name
11881198
self.interfaces = interfaces
11891199
self.directives = directives
@@ -1193,7 +1203,8 @@ public final class ObjectTypeDefinition {
11931203

11941204
extension ObjectTypeDefinition : Equatable {
11951205
public static func == (lhs: ObjectTypeDefinition, rhs: ObjectTypeDefinition) -> Bool {
1196-
return lhs.name == rhs.name &&
1206+
return lhs.description == rhs.description &&
1207+
lhs.name == rhs.name &&
11971208
lhs.interfaces == rhs.interfaces &&
11981209
lhs.directives == rhs.directives &&
11991210
lhs.fields == rhs.fields
@@ -1203,13 +1214,15 @@ extension ObjectTypeDefinition : Equatable {
12031214
public final class FieldDefinition {
12041215
public let kind: Kind = .fieldDefinition
12051216
public let loc: Location?
1217+
public let description: StringValue?
12061218
public let name: Name
12071219
public let arguments: [InputValueDefinition]
12081220
public let type: Type
12091221
public let directives: [Directive]
12101222

1211-
init(loc: Location? = nil, name: Name, arguments: [InputValueDefinition] = [], type: Type, directives: [Directive] = []) {
1223+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, arguments: [InputValueDefinition] = [], type: Type, directives: [Directive] = []) {
12121224
self.loc = loc
1225+
self.description = description
12131226
self.name = name
12141227
self.arguments = arguments
12151228
self.type = type
@@ -1219,7 +1232,8 @@ public final class FieldDefinition {
12191232

12201233
extension FieldDefinition : Equatable {
12211234
public static func == (lhs: FieldDefinition, rhs: FieldDefinition) -> Bool {
1222-
return lhs.name == rhs.name &&
1235+
return lhs.description == rhs.description &&
1236+
lhs.name == rhs.name &&
12231237
lhs.arguments == rhs.arguments &&
12241238
lhs.type == rhs.type &&
12251239
lhs.directives == rhs.directives
@@ -1229,13 +1243,15 @@ extension FieldDefinition : Equatable {
12291243
public final class InputValueDefinition {
12301244
public let kind: Kind = .inputValueDefinition
12311245
public let loc: Location?
1246+
public let description: StringValue?
12321247
public let name: Name
12331248
public let type: Type
12341249
public let defaultValue: Value?
12351250
public let directives: [Directive]
12361251

1237-
init(loc: Location? = nil, name: Name, type: Type, defaultValue: Value? = nil, directives: [Directive] = []) {
1252+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, type: Type, defaultValue: Value? = nil, directives: [Directive] = []) {
12381253
self.loc = loc
1254+
self.description = description
12391255
self.name = name
12401256
self.type = type
12411257
self.defaultValue = defaultValue
@@ -1272,19 +1288,22 @@ extension InputValueDefinition : Equatable {
12721288
public final class InterfaceTypeDefinition {
12731289
public let kind: Kind = .interfaceTypeDefinition
12741290
public let loc: Location?
1291+
public let description: StringValue?
12751292
public let name: Name
12761293
public let interfaces: [NamedType]
12771294
public let directives: [Directive]
12781295
public let fields: [FieldDefinition]
12791296

12801297
init(
12811298
loc: Location? = nil,
1299+
description: StringValue? = nil,
12821300
name: Name,
12831301
interfaces: [NamedType] = [],
12841302
directives: [Directive] = [],
12851303
fields: [FieldDefinition]
12861304
) {
12871305
self.loc = loc
1306+
self.description = description
12881307
self.name = name
12891308
self.interfaces = interfaces
12901309
self.directives = directives
@@ -1294,7 +1313,8 @@ public final class InterfaceTypeDefinition {
12941313

12951314
extension InterfaceTypeDefinition : Equatable {
12961315
public static func == (lhs: InterfaceTypeDefinition, rhs: InterfaceTypeDefinition) -> Bool {
1297-
return lhs.name == rhs.name &&
1316+
return lhs.description == rhs.description &&
1317+
lhs.name == rhs.name &&
12981318
lhs.directives == rhs.directives &&
12991319
lhs.fields == rhs.fields
13001320
}
@@ -1303,12 +1323,14 @@ extension InterfaceTypeDefinition : Equatable {
13031323
public final class UnionTypeDefinition {
13041324
public let kind: Kind = .unionTypeDefinition
13051325
public let loc: Location?
1326+
public let description: StringValue?
13061327
public let name: Name
13071328
public let directives: [Directive]
13081329
public let types: [NamedType]
13091330

1310-
init(loc: Location? = nil, name: Name, directives: [Directive] = [], types: [NamedType]) {
1331+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, directives: [Directive] = [], types: [NamedType]) {
13111332
self.loc = loc
1333+
self.description = description
13121334
self.name = name
13131335
self.directives = directives
13141336
self.types = types
@@ -1317,7 +1339,8 @@ public final class UnionTypeDefinition {
13171339

13181340
extension UnionTypeDefinition : Equatable {
13191341
public static func == (lhs: UnionTypeDefinition, rhs: UnionTypeDefinition) -> Bool {
1320-
return lhs.name == rhs.name &&
1342+
return lhs.description == rhs.description &&
1343+
lhs.name == rhs.name &&
13211344
lhs.directives == rhs.directives &&
13221345
lhs.types == rhs.types
13231346
}
@@ -1326,12 +1349,14 @@ extension UnionTypeDefinition : Equatable {
13261349
public final class EnumTypeDefinition {
13271350
public let kind: Kind = .enumTypeDefinition
13281351
public let loc: Location?
1352+
public let description: StringValue?
13291353
public let name: Name
13301354
public let directives: [Directive]
13311355
public let values: [EnumValueDefinition]
13321356

1333-
init(loc: Location? = nil, name: Name, directives: [Directive] = [], values: [EnumValueDefinition]) {
1357+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, directives: [Directive] = [], values: [EnumValueDefinition]) {
13341358
self.loc = loc
1359+
self.description = description
13351360
self.name = name
13361361
self.directives = directives
13371362
self.values = values
@@ -1340,7 +1365,8 @@ public final class EnumTypeDefinition {
13401365

13411366
extension EnumTypeDefinition : Equatable {
13421367
public static func == (lhs: EnumTypeDefinition, rhs: EnumTypeDefinition) -> Bool {
1343-
return lhs.name == rhs.name &&
1368+
return lhs.description == rhs.description &&
1369+
lhs.name == rhs.name &&
13441370
lhs.directives == rhs.directives &&
13451371
lhs.values == rhs.values
13461372
}
@@ -1349,32 +1375,37 @@ extension EnumTypeDefinition : Equatable {
13491375
public final class EnumValueDefinition {
13501376
public let kind: Kind = .enumValueDefinition
13511377
public let loc: Location?
1378+
public let description: StringValue?
13521379
public let name: Name
13531380
public let directives: [Directive]
13541381

1355-
init(loc: Location? = nil, name: Name, directives: [Directive] = []) {
1382+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, directives: [Directive] = []) {
13561383
self.loc = loc
1384+
self.description = description
13571385
self.name = name
13581386
self.directives = directives
13591387
}
13601388
}
13611389

13621390
extension EnumValueDefinition : Equatable {
13631391
public static func == (lhs: EnumValueDefinition, rhs: EnumValueDefinition) -> Bool {
1364-
return lhs.name == rhs.name &&
1392+
return lhs.description == rhs.description &&
1393+
lhs.name == rhs.name &&
13651394
lhs.directives == rhs.directives
13661395
}
13671396
}
13681397

13691398
public final class InputObjectTypeDefinition {
13701399
public let kind: Kind = .inputObjectTypeDefinition
13711400
public let loc: Location?
1401+
public let description: StringValue?
13721402
public let name: Name
13731403
public let directives: [Directive]
13741404
public let fields: [InputValueDefinition]
13751405

1376-
init(loc: Location? = nil, name: Name, directives: [Directive] = [], fields: [InputValueDefinition]) {
1406+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, directives: [Directive] = [], fields: [InputValueDefinition]) {
13771407
self.loc = loc
1408+
self.description = description
13781409
self.name = name
13791410
self.directives = directives
13801411
self.fields = fields
@@ -1383,9 +1414,10 @@ public final class InputObjectTypeDefinition {
13831414

13841415
extension InputObjectTypeDefinition : Equatable {
13851416
public static func == (lhs: InputObjectTypeDefinition, rhs: InputObjectTypeDefinition) -> Bool {
1386-
return lhs.name == rhs.name &&
1387-
lhs.directives == rhs.directives &&
1388-
lhs.fields == rhs.fields
1417+
return lhs.description == rhs.description &&
1418+
lhs.name == rhs.name &&
1419+
lhs.directives == rhs.directives &&
1420+
lhs.fields == rhs.fields
13891421
}
13901422
}
13911423

@@ -1409,22 +1441,25 @@ extension TypeExtensionDefinition : Equatable {
14091441
public final class DirectiveDefinition {
14101442
public let kind: Kind = .directiveDefinition
14111443
public let loc: Location?
1444+
public let description: StringValue?
14121445
public let name: Name
14131446
public let arguments: [InputValueDefinition]
14141447
public let locations: [Name]
14151448

1416-
init(loc: Location? = nil, name: Name, arguments: [InputValueDefinition] = [], locations: [Name]) {
1449+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, arguments: [InputValueDefinition] = [], locations: [Name]) {
14171450
self.loc = loc
14181451
self.name = name
1452+
self.description = description
14191453
self.arguments = arguments
14201454
self.locations = locations
14211455
}
14221456
}
14231457

14241458
extension DirectiveDefinition : Equatable {
14251459
public static func == (lhs: DirectiveDefinition, rhs: DirectiveDefinition) -> Bool {
1426-
return lhs.name == rhs.name &&
1427-
lhs.arguments == rhs.arguments &&
1428-
lhs.locations == rhs.locations
1460+
return lhs.description == rhs.description &&
1461+
lhs.name == rhs.name &&
1462+
lhs.arguments == rhs.arguments &&
1463+
lhs.locations == rhs.locations
14291464
}
14301465
}

Sources/GraphQL/Language/Lexer.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ final class Lexer {
9191
func advance() throws -> Token {
9292
return try advanceFunction(self)
9393
}
94+
95+
/**
96+
* Looks ahead and returns the next non-ignored token, but does not change
97+
* the state of Lexer.
98+
*/
99+
func lookahead() throws -> Token {
100+
var startToken = token
101+
let savedLine = self.line
102+
let savedLineStart = self.lineStart
103+
104+
guard startToken.kind != .eof else { return startToken }
105+
repeat {
106+
startToken = try startToken.next ??
107+
{
108+
startToken.next = try readToken(lexer: self, prev: startToken)
109+
return startToken.next!
110+
}()
111+
} while startToken.kind == .comment
112+
113+
// restore these since both `positionAfterWhitespace` & `readBlockString`
114+
// can potentially modify them and commment for `lookahead` says no lexer modification.
115+
// (the latter is true in the canonical js lexer also and is likely a bug)
116+
self.line = savedLine
117+
self.lineStart = savedLineStart
118+
119+
return startToken
120+
}
94121
}
95122

96123
/**

0 commit comments

Comments
 (0)