/
graphql_helpers.go
66 lines (61 loc) · 2.17 KB
/
graphql_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// Copyright 2023 The GUAC Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package backend
// // getPreloads get the specific graphQL query fields that are requested.
// // graphql.CollectAllFields only provides the top level fields and none of the nested fields below it.
// // getPreloads recursively goes through the fields and retrieves each nested field below it.
// // for example:
// /*
// type
// namespaces {
// namespace
// names {
// name
// tag
// commit
// }
// }
// will return:
// fields: [type namespaces namespaces.namespace namespaces.names namespaces.names.name namespaces.names.tag namespaces.names.commit]
// */
// func getPreloads(ctx context.Context) ([]string, bool) {
// if !graphql.HasOperationContext(ctx) {
// return []string{}, false
// }
// visited := make(map[string]bool)
// return getNestedPreloads(
// graphql.GetOperationContext(ctx),
// graphql.CollectFieldsCtx(ctx, nil),
// "", visited), true
// }
// func getNestedPreloads(ctx *graphql.OperationContext, fields []graphql.CollectedField, prefix string, visited map[string]bool) []string {
// preloads := []string{}
// for _, column := range fields {
// prefixColumn := getPreloadString(prefix, column.Name)
// if visited[prefixColumn] {
// continue
// }
// visited[prefixColumn] = true
// preloads = append(preloads, prefixColumn)
// preloads = append(preloads, getNestedPreloads(ctx, graphql.CollectFields(ctx, column.Selections, nil), prefixColumn, visited)...)
// }
// return preloads
// }
// func getPreloadString(prefix, name string) string {
// if len(prefix) > 0 {
// return prefix + "." + name
// }
// return name
// }