Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

[Unreleased]

* [FEATURE] Added ability to parse schemas independently of attaching resolvers.
This allows multiple executable schemas to be created from the same parsed
definition, with different schema options if required.

[v1.8.0](https://github.com/graph-gophers/graphql-go/releases/tag/v1.8.0) Release v1.8.0

* [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.
Expand Down
103 changes: 99 additions & 4 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,10 @@ func (s *{{ $enum.Name }}) UnmarshalGraphQL(input interface{}) error {
panic(err)
}

opts := []graphql.SchemaOpt{
opts := []graphql.SchemaDefOpt{
graphql.UseStringDescriptions(),
}
schema := graphql.MustParseSchema(s, nil, opts...)
schema := graphql.MustParseSchemaDef(s, opts...)
ast := schema.AST()
seasons := ast.Enums[0]

Expand Down Expand Up @@ -417,10 +417,10 @@ func ExampleUseStringDescriptions() {
}
`

opts := []graphql.SchemaOpt{
opts := []graphql.SchemaDefOpt{
graphql.UseStringDescriptions(),
}
schema := graphql.MustParseSchema(s, nil, opts...)
schema := graphql.MustParseSchemaDef(s, opts...)
ast := schema.AST()

post := ast.Objects[1]
Expand Down Expand Up @@ -490,3 +490,98 @@ func Example_resolverFieldTag() {
// }
// }
}

func Example_onlyParseSchemaDefinition() {
sdl := `
schema {
query: Query
}

type Query {
hello: String!
}
`

def := graphql.MustParseSchemaDef(sdl)

ast := def.AST()

fmt.Println(ast.Types["Query"].Kind())

// output:
// OBJECT
}

func Example_multipleExecutableSchemas() {
def := graphql.MustParseSchemaDef(starwars.Schema, graphql.UseStringDescriptions())

schema1 := graphql.MustExecutableSchema(def, &starwars.Resolver{}, graphql.MaxDepth(8))
schema2 := graphql.MustExecutableSchema(def, &starwars.Resolver{}, graphql.MaxDepth(4))

query := `
query {
hero(episode:EMPIRE) {
name
friendsConnection(first: 1) {
friends {
name
friendsConnection(first: 1) {
friends {
id
}
}
}
}
}
}`

res1 := schema1.Exec(context.Background(), query, "", nil)
res2 := schema2.Exec(context.Background(), query, "", nil)

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")

if err := enc.Encode(res1); err != nil {
panic(err)
}

if err := enc.Encode(res2); err != nil {
panic(err)
}

// output:
// {
// "data": {
// "hero": {
// "name": "Luke Skywalker",
// "friendsConnection": {
// "friends": [
// {
// "name": "Han Solo",
// "friendsConnection": {
// "friends": [
// {
// "id": "1000"
// }
// ]
// }
// }
// ]
// }
// }
// }
// }
// {
// "errors": [
// {
// "message": "Field \"friends\" has depth 5 that exceeds max depth 4",
// "locations": [
// {
// "line": 9,
// "column": 14
// }
// ]
// }
// ]
// }
}
Loading
Loading