Skip to content

Commit a3911d7

Browse files
committed
Made Schema optional params as structs
1 parent a5ce8d2 commit a3911d7

6 files changed

Lines changed: 36 additions & 37 deletions

File tree

‎src/FSharp.Data.GraphQL.Server/Execution.fs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,13 @@ let internal executeOperation (ctx : ExecutionContext) : AsyncVal<GQLExecutionRe
754754
| Query -> executeQueryOrMutation resultSet ctx ctx.Schema.Query ctx.RootValue
755755
| Mutation ->
756756
match ctx.Schema.Mutation with
757-
| Some m -> executeQueryOrMutation resultSet ctx m ctx.RootValue
758-
| None -> raise(InvalidOperationException("Attempted to make a mutation but no mutation schema was present!"))
757+
| ValueSome m -> executeQueryOrMutation resultSet ctx m ctx.RootValue
758+
| ValueNone -> raise(InvalidOperationException("Attempted to make a mutation but no mutation schema was present!"))
759759
| Subscription ->
760760
match ctx.Schema.Subscription with
761-
| Some s ->
761+
| ValueSome s ->
762762
match executeSubscription resultSet ctx.GetInputContext ctx s ctx.RootValue with
763763
| Ok data -> AsyncVal.wrap(GQLExecutionResult.Stream(ctx.ExecutionPlan.DocumentId, data, ctx.Metadata))
764764
| Error errs -> asyncVal { return GQLExecutionResult.Error(ctx.ExecutionPlan.DocumentId, errs, ctx.Metadata) }
765765

766-
| None -> raise(InvalidOperationException("Attempted to make a subscription but no subscription schema was present!"))
766+
| ValueNone -> raise(InvalidOperationException("Attempted to make a subscription but no subscription schema was present!"))

‎src/FSharp.Data.GraphQL.Server/Executor.fs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ type Executor<'Root>(schema: ISchema<'Root>, middlewares : IExecutorMiddleware s
146146
| Query -> Ok schema.Query
147147
| Mutation ->
148148
match schema.Mutation with
149-
| Some m -> Ok m
150-
| None -> Error <| [ GQLProblemDetails.CreateWithKind (
149+
| ValueSome m -> Ok m
150+
| ValueNone -> Error <| [ GQLProblemDetails.CreateWithKind (
151151
"Operation to be executed is of type mutation, but no mutation root object was defined in current schema",
152152
ErrorKind.Validation
153153
)]
154154
| Subscription ->
155155
match schema.Subscription with
156-
| Some s -> Ok <| upcast s
157-
| None -> Error <| [ GQLProblemDetails.CreateWithKind (
156+
| ValueSome s -> Ok <| upcast s
157+
| ValueNone -> Error <| [ GQLProblemDetails.CreateWithKind (
158158
"Operation to be executed is of type subscription, but no subscription root object was defined in the current schema",
159159
ErrorKind.Validation
160160
)]

‎src/FSharp.Data.GraphQL.Server/Planning.fs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,31 +403,31 @@ let internal planOperation (ctx: PlanningContext) : ExecutionPlan =
403403
Metadata = ctx.Metadata }
404404
| Mutation ->
405405
match ctx.Schema.Mutation with
406-
| Some mutationDef ->
406+
| ValueSome mutationDef ->
407407
{ DocumentId = ctx.DocumentId
408408
Operation = ctx.Operation
409409
RootDef = mutationDef
410410
Fields = fields
411411
Variables = variables
412412
Strategy = Sequential
413413
Metadata = ctx.Metadata }
414-
| None ->
414+
| ValueNone ->
415415
Debug.Fail "Must be prevented by validation"
416416
raise (
417417
MalformedGQLQueryException
418418
"Operation to be executed is of type mutation, but no mutation root object was defined in current schema"
419419
)
420420
| Subscription ->
421421
match ctx.Schema.Subscription with
422-
| Some subscriptionDef ->
422+
| ValueSome subscriptionDef ->
423423
{ DocumentId = ctx.DocumentId
424424
Operation = ctx.Operation
425425
RootDef = subscriptionDef
426426
Fields = fields
427427
Variables = variables
428428
Strategy = Sequential
429429
Metadata = ctx.Metadata }
430-
| None ->
430+
| ValueNone ->
431431
Debug.Fail "Must be prevented by validation"
432432
raise (
433433
MalformedGQLQueryException

‎src/FSharp.Data.GraphQL.Server/Schema.fs‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ type SchemaConfig =
169169
Directives = [ IncludeDirective; SkipDirective; DeferDirective; streamDirective; LiveDirective ] }
170170

171171
/// GraphQL server schema. Defines the complete type system to be used by GraphQL queries.
172-
type Schema<'Root> (query: ObjectDef<'Root>, ?mutation: ObjectDef<'Root>, ?subscription: SubscriptionObjectDef<'Root>, ?config: SchemaConfig) =
172+
type Schema<'Root> (query: ObjectDef<'Root>, [<Struct>] ?mutation: ObjectDef<'Root>, [<Struct>] ?subscription: SubscriptionObjectDef<'Root>, ?config: SchemaConfig) =
173173

174174
let schemaConfig =
175175
match config with
@@ -191,8 +191,8 @@ type Schema<'Root> (query: ObjectDef<'Root>, ?mutation: ObjectDef<'Root>, ?subsc
191191
__Schema
192192
query ]
193193

194-
let m = mutation |> function Some (Named n) -> [n] | _ -> []
195-
let s = subscription |> function Some (Named n) -> [n] | _ -> []
194+
let m = mutation |> function ValueSome (Named n) -> [n] | _ -> []
195+
let s = subscription |> function ValueSome (Named n) -> [n] | _ -> []
196196
seq { initialTypes; s; m; schemaConfig.Types } |> Seq.collect id |> TypeMap.FromSeq
197197

198198
let getImplementations (typeMap : TypeMap) =
@@ -349,9 +349,8 @@ type Schema<'Root> (query: ObjectDef<'Root>, ?mutation: ObjectDef<'Root>, ?subsc
349349
|> List.map (introspectDirective inamed)
350350
|> List.toArray
351351
{ QueryType = Map.find query.Name inamed
352-
// TODO: `mutation`/`subscription` are still `'T option` (Schema's constructor is out of scope for this change) - convert here until that follow-up moves Schema to [<Struct>] ?.
353-
MutationType = mutation |> ValueOption.ofOption |> ValueOption.map (fun m -> Map.find m.Name inamed)
354-
SubscriptionType = subscription |> ValueOption.ofOption |> ValueOption.map(fun s -> Map.find s.Name inamed)
352+
MutationType = mutation |> ValueOption.map (fun m -> Map.find m.Name inamed)
353+
SubscriptionType = subscription |> ValueOption.map (fun s -> Map.find s.Name inamed)
355354
Types = itypes
356355
Directives = idirectives }
357356

@@ -382,8 +381,8 @@ type Schema<'Root> (query: ObjectDef<'Root>, ?mutation: ObjectDef<'Root>, ?subsc
382381
member _.Directives = schemaConfig.Directives |> List.toArray
383382
member _.Introspected = introspected.Force()
384383
member _.Query = upcast query
385-
member _.Mutation = mutation |> Option.map (fun x -> upcast x)
386-
member _.Subscription = subscription |> Option.map (fun x -> upcast x)
384+
member _.Mutation = mutation |> ValueOption.map (fun x -> upcast x)
385+
member _.Subscription = subscription |> ValueOption.map (fun x -> upcast x)
387386
member _.TryFindType typeName = typeMap.TryFind(typeName, includeDefaultTypes = true)
388387
member _.GetPossibleTypes typedef = getPossibleTypes typedef
389388
member _.ParseError path exn = schemaConfig.ParseError path exn

‎src/FSharp.Data.GraphQL.Shared/TypeSystem.fs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,11 @@ and ISchema =
428428

429429
/// A mutation root object. Defines all top level operations,
430430
/// that can be performed from GraphQL mutations.
431-
abstract Mutation : ObjectDef option
431+
abstract Mutation : ObjectDef voption
432432

433433
// A subscription root object. Defines all top level operations,
434434
// that can be performed from GraphQL subscriptions.
435-
abstract Subscription : SubscriptionObjectDef option
435+
abstract Subscription : SubscriptionObjectDef voption
436436

437437
/// List of all directives supported by the current schema.
438438
abstract Directives : DirectiveDef[]
@@ -472,8 +472,8 @@ and ISchema<'Root> =
472472
interface
473473
inherit ISchema
474474
abstract Query : ObjectDef<'Root>
475-
abstract Mutation : ObjectDef<'Root> option
476-
abstract Subscription : SubscriptionObjectDef<'Root> option
475+
abstract Mutation : ObjectDef<'Root> voption
476+
abstract Subscription : SubscriptionObjectDef<'Root> voption
477477
end
478478

479479
/// A type alias for a field execute compiler function.

‎src/FSharp.Data.GraphQL.Shared/Validation.fs‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -904,8 +904,8 @@ module Ast =
904904
=
905905
let visitCount =
906906
visited
907-
|> List.filter (fun x -> x = fragName)
908-
|> List.length
907+
|> Seq.filter (fun x -> x = fragName)
908+
|> Seq.length
909909
if visitCount > 1 then
910910
AstError.AsResult $"Fragment '%s{fragName}' is making a cyclic reference."
911911
else
@@ -961,7 +961,7 @@ module Ast =
961961
|> getFragmentAndParentTypes
962962
|> ValidationResult.collect (checkFragmentSpreadIsPossibleInSelection))
963963

964-
let private checkInputValue (schemaInfo : SchemaInfo) (variables : VariableDefinition list option) (selection : SelectionInfo) =
964+
let private checkInputValue (schemaInfo : SchemaInfo) (variables : VariableDefinition list voption) (selection : SelectionInfo) =
965965
let rec checkIsCoercible (tref : IntrospectionTypeRef) (argName : string) (value : InputValue) =
966966
let canNotCoerce =
967967
AstError.AsResult (
@@ -1045,15 +1045,15 @@ module Ast =
10451045
| VariableName varName ->
10461046
let variableDefinition =
10471047
variables
1048-
|> Option.defaultValue []
1049-
|> List.tryPick (fun v ->
1048+
|> ValueOption.defaultValue []
1049+
|> List.vtryPick (fun v ->
10501050
if v.VariableName = varName then
1051-
Some (v, schemaInfo.TryGetInputType (v.Type))
1051+
ValueSome (v, schemaInfo.TryGetInputType (v.Type))
10521052
else
1053-
None)
1053+
ValueNone)
10541054
match variableDefinition with
1055-
| Some (vdef, Some vtype) when vdef.DefaultValue.IsSome -> checkIsCoercible vtype argName vdef.DefaultValue.Value
1056-
| Some (vdef, None) when vdef.DefaultValue.IsSome -> canNotCoerce
1055+
| ValueSome (vdef, Some vtype) when vdef.DefaultValue.IsSome -> checkIsCoercible vtype argName vdef.DefaultValue.Value
1056+
| ValueSome (vdef, None) when vdef.DefaultValue.IsSome -> canNotCoerce
10571057
| _ -> Success
10581058
selection.Field.Arguments
10591059
|> ValidationResult.collect (fun arg ->
@@ -1067,10 +1067,10 @@ module Ast =
10671067
let internal validateInputValues (ctx : ValidationContext) =
10681068
ctx.Definitions
10691069
|> ValidationResult.collect (fun def ->
1070-
let (vars, selectionSet) =
1070+
let struct (vars, selectionSet) =
10711071
match def with
1072-
| OperationDefinitionInfo odef -> (Some odef.Definition.VariableDefinitions, odef.SelectionSet)
1073-
| FragmentDefinitionInfo fdef -> (None, fdef.SelectionSet)
1072+
| OperationDefinitionInfo odef -> struct (ValueSome odef.Definition.VariableDefinitions, odef.SelectionSet)
1073+
| FragmentDefinitionInfo fdef -> struct (ValueNone, fdef.SelectionSet)
10741074
selectionSet
10751075
|> ValidationResult.collect (checkInputValue ctx.Schema vars))
10761076

@@ -1096,7 +1096,7 @@ module Ast =
10961096
let selectionSetDirectives =
10971097
frag.SelectionSet
10981098
|> List.collect (getDistinctDirectiveNamesInSelection path)
1099-
fragDirectives |> List.append selectionSetDirectives
1099+
fragDirectives @ selectionSetDirectives
11001100

11011101
let internal validateDirectivesDefined (ctx : ValidationContext) =
11021102
ctx.Definitions

0 commit comments

Comments
 (0)