MCPcopy Create free account
hub / github.com/cli/cli / scaffoldGraphQL

Function scaffoldGraphQL

script/ghmock/scaffold.go:208–316  ·  view source on GitHub ↗

scaffoldGraphQL adds a response built from gh's query, or adds the fields gh asked for to stored responses that lack them. explicit is true when the caller named this request, so it reports when there's nothing to do.

(chk *checker, name string, responses responseSet, entries []loggedRequest, explicit bool)

Source from the content-addressed store, hash-verified

206// gh asked for to stored responses that lack them. explicit is true when the
207// caller named this request, so it reports when there's nothing to do.
208func scaffoldGraphQL(chk *checker, name string, responses responseSet, entries []loggedRequest, explicit bool) ([]string, error) {
209 schema, err := chk.graphQL()
210 if err != nil {
211 return nil, err
212 }
213 queries := queriesFor(entries, name)
214 if len(queries) == 0 {
215 return nil, fmt.Errorf("gh hasn't sent %s, so there's no query to build it from; run gh first", name)
216 }
217 type parsed struct {
218 sentQuery
219 op *ast.OperationDefinition
220 }
221 var ops []parsed
222 for _, q := range queries {
223 op, _ := checkGraphQLQuery(schema, q.query, q.request(name).GraphQL.Operation)
224 if op == nil {
225 return nil, fmt.Errorf("gh's query couldn't be read; ghmock log --full shows it")
226 }
227 if rootType(schema, op.Operation) == nil {
228 return nil, fmt.Errorf("GitHub's schema has no %s type", op.Operation)
229 }
230 ops = append(ops, parsed{q, op})
231 }
232
233 list := responses[name]
234 if len(list) == 0 {
235 s := newGQLScaffolder(schema)
236 data := map[string]any{}
237 for _, p := range ops {
238 if err := s.useVariables(p.op, p.vars); err != nil {
239 return nil, err
240 }
241 s.fillObject("data", data, rootType(schema, p.op.Operation), p.op.SelectionSet, false)
242 }
243 body, err := json.Marshal(map[string]any{"data": data})
244 if err != nil {
245 return nil, err
246 }
247 resp := response{Body: body}
248 for _, p := range ops {
249 if res := chk.checkRequest(p.request(name), &resp); len(res.responseProblems) > 0 {
250 return nil, fmt.Errorf("the placeholder response doesn't match GitHub's schema, which is a ghmock bug:\n%s",
251 indentProblems(res.responseProblems, " ", maxShownProblems))
252 }
253 }
254 responses[name] = []response{resp}
255 detail := "with placeholder values"
256 if !s.placeholders {
257 detail = "from GitHub's schema"
258 }
259 return []string{fmt.Sprintf("%s: added response 1 of 1 %s", name, detail)}, nil
260 }
261
262 var lines []string
263 for i := range list {
264 r := list[i]
265 if r.status() != http.StatusOK {

Callers 1

cmdScaffoldFunction · 0.85

Calls 15

queriesForFunction · 0.85
checkGraphQLQueryFunction · 0.85
rootTypeFunction · 0.85
newGQLScaffolderFunction · 0.85
indentProblemsFunction · 0.85
decodeBodyFunction · 0.85
addedPathsFunction · 0.85
dedupeFunction · 0.85
graphQLMethod · 0.80
requestMethod · 0.80
useVariablesMethod · 0.80
fillObjectMethod · 0.80

Tested by

no test coverage detected