Skip to content

Commit

Permalink
feat(graphql): add ignore prefixes config option
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Jul 27, 2022
1 parent 2754f42 commit 224f50b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type GraphQLConfig struct {
// `GraphQLDefaultPaginator` is used.
Paginator GraphQLPaginator

// IgnorePrefixes defines path prefixes which should be ignored by the
// GraphQL model generator.
IgnorePrefixes []string

// known keeps track of known structs since they can only be defined once
// per GraphQL endpoint. If used by multiple HTTP operations, they must
// reference the same struct converted to GraphQL schema.
Expand Down Expand Up @@ -374,7 +378,13 @@ func (r *Router) EnableGraphQL(config *GraphQLConfig) {
config.costMap = gqlcost.CostMap{}
config.paginatorType = reflect.TypeOf(config.Paginator).Elem()

outer:
for _, resource := range resources {
for _, prefix := range config.IgnorePrefixes {
if strings.HasPrefix(resource.path, prefix) {
continue outer
}
}
r.handleResource(config, "Query", fields, resource, map[string]bool{})
}

Expand Down
25 changes: 25 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,15 @@ func TestGraphQL(t *testing.T) {
ctx.WriteModel(http.StatusOK, categories[input.CategoryID].products[input.ProductID].stores[input.StoreID])
})

app.Resource("/ignored").Get("get-ignored", "doc",
NewResponse(http.StatusOK, "").Model(struct{ ID string }{}),
).Run(func(ctx Context) {
ctx.WriteHeader(http.StatusOK)
})

app.EnableGraphQL(&GraphQLConfig{
ComplexityLimit: 250,
IgnorePrefixes: []string{"/ignored"},
})

query := strings.Replace(strings.Replace(`{
Expand Down Expand Up @@ -304,4 +311,22 @@ data:
id: target
url: https://www.target.com/
`, "\t", " ", -1), w.Body.String())

// Confirm expected top-level fields in the query API.
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodGet, "/graphql?query={__schema{queryType{fields{name}}}}", nil)
app.ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
assert.YAMLEq(t, strings.Replace(`data:
__schema:
queryType:
fields:
- name: categories
- name: categoriesItem
- name: products
- name: productsItem
- name: stores
- name: storesItem
`, "\t", " ", -1), w.Body.String())
}

0 comments on commit 224f50b

Please sign in to comment.