Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow compatibility when using introspection derived client schema's #145

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 36 additions & 0 deletions docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,42 @@ var respAgain MyQueryResponse
err := json.Unmarshal(b, &resp)
```

### … let me use introspection to fetch my client schema

This is currently not supported by default. You can however use tool such as [gqlfetch](https://github.com/suessflorian/gqlfetch) to build your client schema using introspection and then let `genqlient` continue from there. Moreover, you can define yourself what happens when `go:generate` is run via managing your own _go runnable_ progam.

For example - suppose the file `generate/main.go`;

```go
package main

import (
"context"
"fmt"
"os"

"github.com/Khan/genqlient/generate"
"github.com/suessflorian/gqlfetch"
)

func main() {
schema, err := gqlfetch.BuildClientSchema(context.Background(), "http://localhost:8080/query")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err = os.WriteFile("schema.graphql", []byte(schema), 0644); err != nil {
fmt.Println(err)
os.Exit(1)
}

generate.Main()
}
```

This can now be invoked upon `go generate` via `//go:generate your_package/generate`.

## How do I make a query with …

### … a specific name for a field?
Expand Down
15 changes: 13 additions & 2 deletions generate/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
"github.com/vektah/gqlparser/v2/parser"
"github.com/vektah/gqlparser/v2/validator"
)
Expand All @@ -31,9 +32,19 @@ func getSchema(globs StringList) (*ast.Schema, error) {
sources[i] = &ast.Source{Name: filename, Input: string(text)}
}

schema, graphqlError := gqlparser.LoadSchema(sources...)
// Multi step schema validation
// Step 1 assume schema implicitly declares types that are required by the graphql spec
// Step 2 assume schema explicitly declares types that are required by the graphql spec
var (
schema *ast.Schema
graphqlError *gqlerror.Error
)
schema, graphqlError = gqlparser.LoadSchema(sources...)
if graphqlError != nil {
return nil, errorf(nil, "invalid schema: %v", graphqlError)
schema, graphqlError = validator.LoadSchema(sources...)
if graphqlError != nil {
return nil, errorf(nil, "invalid schema: %v", graphqlError)
}
}

return schema, nil
Expand Down