Skip to content

Commit c7f94f3

Browse files
committed
Allow schemas to be used with multiple resolvers
Enables multiple executable schemas to be created from a single SDL parse. This allows the execution of each to differ as defined by the resolvers and schema options for each case. The change in type for `SchemaOpt` isn't strictly a 100% backward compatible change. It should however be non-breaking for typical cases. Breakages may exist if usages define their own equivalent type from `func(*graphql.Schema)`.
1 parent afe0cf4 commit c7f94f3

File tree

4 files changed

+253
-57
lines changed

4 files changed

+253
-57
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
[Unreleased]
4+
5+
* [FEATURE] Added ability to apply a resolver to an already created schema. This allows multiple executable schemas to be created from the same parsed definition, with different resolvable schema options if required.
6+
37
[v1.8.0](https://github.com/graph-gophers/graphql-go/releases/tag/v1.8.0) Release v1.8.0
48

59
* [FEATURE] Added `DecodeSelectedFieldArgs` helper function to decode argument values for any (nested) selected field path directly from a resolver context, enabling efficient multi-level prefetching without per-resolver argument reflection. This enables selective, multi‑level batching (Category → Products → Reviews) by loading only requested fields, mitigating N+1 issues despite complex filters or pagination.

examples_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,75 @@ func Example_resolverFieldTag() {
490490
// }
491491
// }
492492
}
493+
494+
func Example_multipleExecutableSchemas() {
495+
schema1 := graphql.MustParseSchema(starwars.Schema, &starwars.Resolver{}, graphql.MaxDepth(8))
496+
schema2 := graphql.MustWithResolver(schema1, &starwars.Resolver{}, graphql.MaxDepth(4))
497+
498+
query := `
499+
query {
500+
hero(episode:EMPIRE) {
501+
name
502+
friendsConnection(first: 1) {
503+
friends {
504+
name
505+
friendsConnection(first: 1) {
506+
friends {
507+
id
508+
}
509+
}
510+
}
511+
}
512+
}
513+
}`
514+
515+
res1 := schema1.Exec(context.Background(), query, "", nil)
516+
res2 := schema2.Exec(context.Background(), query, "", nil)
517+
518+
enc := json.NewEncoder(os.Stdout)
519+
enc.SetIndent("", " ")
520+
521+
if err := enc.Encode(res1); err != nil {
522+
panic(err)
523+
}
524+
525+
if err := enc.Encode(res2); err != nil {
526+
panic(err)
527+
}
528+
529+
// output:
530+
// {
531+
// "data": {
532+
// "hero": {
533+
// "name": "Luke Skywalker",
534+
// "friendsConnection": {
535+
// "friends": [
536+
// {
537+
// "name": "Han Solo",
538+
// "friendsConnection": {
539+
// "friends": [
540+
// {
541+
// "id": "1000"
542+
// }
543+
// ]
544+
// }
545+
// }
546+
// ]
547+
// }
548+
// }
549+
// }
550+
// }
551+
// {
552+
// "errors": [
553+
// {
554+
// "message": "Field \"friends\" has depth 5 that exceeds max depth 4",
555+
// "locations": [
556+
// {
557+
// "line": 9,
558+
// "column": 14
559+
// }
560+
// ]
561+
// }
562+
// ]
563+
// }
564+
}

0 commit comments

Comments
 (0)