func TestValidate_NoUnusedFragments_ContainsUnknownFragmentsWithRefCycle(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.NoUnusedFragmentsRule, `
      query Foo {
        human(id: 4) {
          ...HumanFields1
        }
      }
      query Bar {
        human(id: 4) {
          ...HumanFields2
        }
      }
      fragment HumanFields1 on Human {
        name
        ...HumanFields3
      }
      fragment HumanFields2 on Human {
        name
      }
      fragment HumanFields3 on Human {
        name
      }
      fragment Unused1 on Human {
        name
        ...Unused2
      }
      fragment Unused2 on Human {
        name
        ...Unused1
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Fragment "Unused1" is never used.`, 22, 7),
		testutil.RuleError(`Fragment "Unused2" is never used.`, 26, 7),
	})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfDeeply(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
      fragment fragA on Dog { ...fragB }
      fragment fragB on Dog { ...fragC }
      fragment fragC on Dog { ...fragO }
      fragment fragX on Dog { ...fragY }
      fragment fragY on Dog { ...fragZ }
      fragment fragZ on Dog { ...fragO }
      fragment fragO on Dog { ...fragP }
      fragment fragP on Dog { ...fragA, ...fragX }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Cannot spread fragment "fragA" within itself via fragB, fragC, fragO, fragP.`,
			2, 31,
			3, 31,
			4, 31,
			8, 31,
			9, 31),
		testutil.RuleError(`Cannot spread fragment "fragO" within itself via fragP, fragX, fragY, fragZ.`,
			8, 31,
			9, 41,
			5, 31,
			6, 31,
			7, 31),
	})
}
func TestValidate_OverlappingFieldsCanBeMerged_ReportsEachConflictOnce(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.OverlappingFieldsCanBeMergedRule, `
      {
        f1 {
          ...A
          ...B
        }
        f2 {
          ...B
          ...A
        }
        f3 {
          ...A
          ...B
          x: c
        }
      }
      fragment A on Type {
        x: a
      }
      fragment B on Type {
        x: b
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Fields "x" conflict because a and b are different fields.`, 18, 9, 21, 9),
		testutil.RuleError(`Fields "x" conflict because a and c are different fields.`, 18, 9, 14, 11),
		testutil.RuleError(`Fields "x" conflict because b and c are different fields.`, 21, 9, 14, 11),
	})
}
func TestValidate_NoUndefinedVariables_MultipleVariablesNotDefined(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.NoUndefinedVariablesRule, `
      query Foo($b: String) {
        field(a: $a, b: $b, c: $c)
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Variable "$a" is not defined by operation "Foo".`, 3, 18, 2, 7),
		testutil.RuleError(`Variable "$c" is not defined by operation "Foo".`, 3, 32, 2, 7),
	})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfDeeplyTwoPaths(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
      fragment fragA on Dog { ...fragB, ...fragC }
      fragment fragB on Dog { ...fragA }
      fragment fragC on Dog { ...fragA }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Cannot spread fragment "fragA" within itself via fragB.`, 2, 31, 3, 31),
		testutil.RuleError(`Cannot spread fragment "fragA" within itself via fragC.`, 2, 41, 4, 31),
	})
}
func TestValidate_NoUnusedVariables_MultipleVariablesNotUsed(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.NoUnusedVariablesRule, `
      query Foo($a: String, $b: String, $c: String) {
        field(b: $b)
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Variable "$a" is never used in operation "Foo".`, 2, 17),
		testutil.RuleError(`Variable "$c" is never used in operation "Foo".`, 2, 41),
	})
}
func TestValidate_KnownArgumentNames_UnknownArgsAmongstKnownArgs(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.KnownArgumentNamesRule, `
      fragment oneGoodArgOneInvalidArg on Dog {
        doesKnowCommand(whoknows: 1, dogCommand: SIT, unknown: true)
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Unknown argument "whoknows" on field "doesKnowCommand" of type "Dog".`, 3, 25),
		testutil.RuleError(`Unknown argument "unknown" on field "doesKnowCommand" of type "Dog".`, 3, 55),
	})
}
func TestValidate_UniqueArgumentNames_ManyDuplicateFieldArguments(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.UniqueArgumentNamesRule, `
      {
        field(arg1: "value", arg1: "value", arg1: "value")
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`There can be only one argument named "arg1".`, 3, 15, 3, 30),
		testutil.RuleError(`There can be only one argument named "arg1".`, 3, 15, 3, 45),
	})
}
func TestValidate_UniqueInputFieldNames_ManyDuplicateInputObjectFields(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.UniqueInputFieldNamesRule, `
      {
        field(arg: { f1: "value", f1: "value", f1: "value" })
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`There can be only one input field named "f1".`, 3, 22, 3, 35),
		testutil.RuleError(`There can be only one input field named "f1".`, 3, 22, 3, 48),
	})
}
func TestValidate_VariablesAreInputTypes_1(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.VariablesAreInputTypesRule, `
      query Foo($a: Dog, $b: [[CatOrDog!]]!, $c: Pet) {
        field(a: $a, b: $b, c: $c)
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Variable "$a" cannot be non-input type "Dog".`, 2, 21),
		testutil.RuleError(`Variable "$b" cannot be non-input type "[[CatOrDog!]]!".`, 2, 30),
		testutil.RuleError(`Variable "$c" cannot be non-input type "Pet".`, 2, 50),
	})
}
func TestValidate_ProvidedNonNullArguments_InvalidNonNullableValue_MissingMultipleNonNullableArguments(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.ProvidedNonNullArgumentsRule, `
        {
          complicatedArgs {
            multipleReqs
          }
        }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Field "multipleReqs" argument "req1" of type "Int!" is required but not provided.`, 4, 13),
		testutil.RuleError(`Field "multipleReqs" argument "req2" of type "Int!" is required but not provided.`, 4, 13),
	})
}
func TestValidate_ProvidedNonNullArguments_DirectiveArguments_WithDirectiveWithMissingTypes(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.ProvidedNonNullArgumentsRule, `
        {
          dog @include {
            name @skip
          }
        }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Directive "@include" argument "if" of type "Boolean!" is required but not provided.`, 3, 15),
		testutil.RuleError(`Directive "@skip" argument "if" of type "Boolean!" is required but not provided.`, 4, 18),
	})
}
func TestValidate_KnownDirectives_WithMisplacedDirectives(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.KnownDirectivesRule, `
      query Foo @include(if: true) {
        name @operationOnly
        ...Frag @operationOnly
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Directive "include" may not be used on QUERY.`, 2, 17),
		testutil.RuleError(`Directive "operationOnly" may not be used on FIELD.`, 3, 14),
		testutil.RuleError(`Directive "operationOnly" may not be used on FRAGMENT_SPREAD.`, 4, 17),
	})
}
func TestValidate_UniqueVariableNames_DuplicateVariableNames(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.UniqueVariableNamesRule, `
      query A($x: Int, $x: Int, $x: String) { __typename }
      query B($x: String, $x: Int) { __typename }
      query C($x: Int, $x: Int) { __typename }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`There can only be one variable named "x".`, 2, 16, 2, 25),
		testutil.RuleError(`There can only be one variable named "x".`, 2, 16, 2, 34),
		testutil.RuleError(`There can only be one variable named "x".`, 3, 16, 3, 28),
		testutil.RuleError(`There can only be one variable named "x".`, 4, 16, 4, 25),
	})
}
func TestValidate_AnonymousOperationMustBeAlone_MultipleAnonOperations(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.LoneAnonymousOperationRule, `
      {
        fieldA
      }
      {
        fieldB
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`This anonymous operation must be the only defined operation.`, 2, 7),
		testutil.RuleError(`This anonymous operation must be the only defined operation.`, 5, 7),
	})
}
func TestValidate_FieldsOnCorrectType_ReportErrorsWhenTheTypeIsKnownAgain(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
      fragment typeKnownAgain on Pet {
        unknown_pet_field {
          ... on Cat {
            unknown_cat_field
          }
        }
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Cannot query field "unknown_pet_field" on type "Pet".`, 3, 9),
		testutil.RuleError(`Cannot query field "unknown_cat_field" on type "Cat".`, 5, 13),
	})
}
func TestValidate_OverlappingFieldsCanBeMerged_VeryDeepConflict(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.OverlappingFieldsCanBeMergedRule, `
      {
        field {
          deepField {
            x: a
          }
        },
        field {
          deepField {
            x: b
          }
        }
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(
			`Fields "field" conflict because subfields "deepField" conflict because subfields "x" conflict because `+
				`a and b are different fields.`,
			3, 9,
			4, 11,
			5, 13,
			8, 9,
			9, 11,
			10, 13),
	})
}
func TestValidate_OverlappingFieldsCanBeMerged_ReportsDeepConflictToNearestCommonAncestor(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.OverlappingFieldsCanBeMergedRule, `
      {
        field {
          deepField {
            x: a
          }
          deepField {
            x: b
          }
        },
        field {
          deepField {
            y
          }
        }
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(
			`Fields "deepField" conflict because subfields "x" conflict because `+
				`a and b are different fields.`,
			4, 11,
			5, 13,
			7, 11,
			8, 13),
	})
}
func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_ComparesDeepTypesIncludingList(t *testing.T) {
	testutil.ExpectFailsRuleWithSchema(t, &schema, graphql.OverlappingFieldsCanBeMergedRule, `
        {
          connection {
            ...edgeID
            edges {
              node {
                id: name
              }
            }
          }
        }

        fragment edgeID on Connection {
          edges {
            node {
              id
            }
          }
        }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(
			`Fields "edges" conflict because subfields "node" conflict because subfields "id" conflict because `+
				`id and name are different fields.`,
			14, 11,
			15, 13,
			16, 15,
			5, 13,
			6, 15,
			7, 17),
	})
}
func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_DisallowsDifferingDeepReturnTypesDespiteNoOverlap(t *testing.T) {
	testutil.ExpectFailsRuleWithSchema(t, &schema, graphql.OverlappingFieldsCanBeMergedRule, `
        {
          someBox {
            ... on IntBox {
              box: stringBox {
                scalar
              }
            }
            ... on StringBox {
              box: intBox {
                scalar
              }
            }
          }
        }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(
			`Fields "box" conflict because subfields "scalar" conflict because they return conflicting types String and Int.`,
			5, 15,
			6, 17,
			10, 15,
			11, 17),
	})
}
func TestValidate_VariableDefaultValuesOfCorrectType_VariablesWithInvalidDefaultValues(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.DefaultValuesOfCorrectTypeRule, `
      query InvalidDefaultValues(
        $a: Int = "one",
        $b: String = 4,
        $c: ComplexInput = "notverycomplex"
      ) {
        dog { name }
      }
    `,
		[]gqlerrors.FormattedError{
			testutil.RuleError(`Variable "$a" of type "Int" has invalid default value: "one".`, 3, 19),
			testutil.RuleError(`Variable "$b" of type "String" has invalid default value: 4.`, 4, 22),
			testutil.RuleError(`Variable "$c" of type "ComplexInput" has invalid default value: "notverycomplex".`, 5, 28),
		})
}
func TestValidate_NoCircularFragmentSpreads_SpreadingRecursivelyWithinFieldFails(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
      fragment fragA on Human { relatives { ...fragA } },
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Cannot spread fragment "fragA" within itself.`, 2, 45),
	})
}
func TestValidate_NoUndefinedVariables_VariablesInFragmentNotDefinedByMultipleOperations(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.NoUndefinedVariablesRule, `
      query Foo($b: String) {
        ...FragAB
      }
      query Bar($a: String) {
        ...FragAB
      }
      fragment FragAB on Type {
        field(a: $a, b: $b)
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Variable "$a" is not defined by operation "Foo".`, 9, 18, 2, 7),
		testutil.RuleError(`Variable "$b" is not defined by operation "Bar".`, 9, 25, 5, 7),
	})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfDirectly(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
      fragment fragA on Dog { ...fragA }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Cannot spread fragment "fragA" within itself.`, 2, 31),
	})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfIndirectlyReportsOppositeOrder(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
      fragment fragB on Dog { ...fragA }
      fragment fragA on Dog { ...fragB }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Cannot spread fragment "fragB" within itself via fragA.`, 2, 31, 3, 31),
	})
}
Exemplo n.º 26
0
func TestValidate_KnownTypeNames_UnknownTypeNamesAreInValid(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.KnownTypeNamesRule, `
      query Foo($var: JumbledUpLetters) {
        user(id: 4) {
          name
          pets { ... on Badger { name }, ...PetFields }
        }
      }
      fragment PetFields on Peettt {
        name
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Unknown type "JumbledUpLetters".`, 2, 23),
		testutil.RuleError(`Unknown type "Badger".`, 5, 25),
		testutil.RuleError(`Unknown type "Peettt".`, 8, 29),
	})
}
func TestValidate_UniqueArgumentNames_DuplicateDirectiveArguments(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.UniqueArgumentNamesRule, `
      {
        field @directive(arg1: "value", arg1: "value")
      }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`There can be only one argument named "arg1".`, 3, 26, 3, 41),
	})
}
func TestValidate_PossibleFragmentSpreads_ObjectIntoNotImplementingInterface(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.PossibleFragmentSpreadsRule, `
      fragment invalidObjectWithinInterface on Pet { ...humanFragment }
      fragment humanFragment on Human { pets { name } }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Fragment "humanFragment" cannot be spread here as objects of `+
			`type "Pet" can never be of type "Human".`, 2, 54),
	})
}
func TestValidate_PossibleFragmentSpreads_DifferentObjectIntoObject(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.PossibleFragmentSpreadsRule, `
      fragment invalidObjectWithinObject on Cat { ...dogFragment }
      fragment dogFragment on Dog { barkVolume }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Fragment "dogFragment" cannot be spread here as objects of `+
			`type "Cat" can never be of type "Dog".`, 2, 51),
	})
}
func TestValidate_PossibleFragmentSpreads_InterfaceIntoNonOverlappingUnion(t *testing.T) {
	testutil.ExpectFailsRule(t, graphql.PossibleFragmentSpreadsRule, `
      fragment invalidInterfaceWithinUnion on HumanOrAlien { ...petFragment }
      fragment petFragment on Pet { name }
    `, []gqlerrors.FormattedError{
		testutil.RuleError(`Fragment "petFragment" cannot be spread here as objects of `+
			`type "HumanOrAlien" can never be of type "Pet".`, 2, 62),
	})
}