MCPcopy Create free account
hub / github.com/vektah/gqlparser / TestCustomRuleSetWithRules

Function TestCustomRuleSetWithRules

validator/validator_test.go:295–339  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

293}
294
295func TestCustomRuleSetWithRules(t *testing.T) {
296 someRule := validator.Rule{
297 Name: "SomeRule",
298 RuleFunc: func(observers *validator.Events, addError validator.AddErrFunc) {
299 addError(validator.Message("%s", "some error message"))
300 },
301 }
302
303 someOtherRule := validator.Rule{
304 Name: "SomeOtherRule",
305 RuleFunc: func(observers *validator.Events, addError validator.AddErrFunc) {
306 addError(validator.Message("%s", "some other error message"))
307 },
308 }
309
310 s := gqlparser.MustLoadSchema(
311 &ast.Source{
312 Name: "graph/schema.graphqls",
313 Input: `
314 type Query {
315 bar: String!
316 }
317 `, BuiltIn: false,
318 },
319 )
320
321 q, err := parser.ParseQuery(&ast.Source{
322 Name: "SomeQuery",
323 Input: `
324 query Foo($flag: Boolean!) {
325 ...Bar
326 }
327 `,
328 })
329 require.NoError(t, err)
330 errList := validator.ValidateWithRules(s, q, rules.NewRules(someRule, someOtherRule))
331 require.Len(t, errList, 2)
332
333 // because we hold rules in a map, the order is not guaranteed
334 // this is fine because we used to add the rule in the init function, so it didn't need to be
335 // specified as a requirement for the order.
336 messages := []string{errList[0].Message, errList[1].Message}
337 require.Contains(t, messages, "some error message")
338 require.Contains(t, messages, "some other error message")
339}
340
341func TestRemoveRule(t *testing.T) {
342 // no error

Callers

nothing calls this directly

Calls 4

ParseQueryFunction · 0.92
ValidateWithRulesFunction · 0.92
NewRulesFunction · 0.92
LenMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…