public void FieldsAndFragmentDefinitionsInterfaceType() { // arrange Schema schema = CreateSchema(); DocumentNode query = Utf8GraphQLParser.Parse(@" { a ... Test } fragment Test on IFoo { x: a } "); var variables = new VariableValueCollection( TypeConversion.Default, new Dictionary <string, object>()); var fragments = new FragmentCollection(schema, query); OperationDefinitionNode operation = query.Definitions .OfType <OperationDefinitionNode>().First(); // act var fieldResolver = new FieldCollector( fragments, (f, s) => null, TypeConversion.Default, Array.Empty <IArgumentCoercionHandler>()); IReadOnlyCollection <FieldSelection> fields_a = fieldResolver .CollectFields(schema.GetType <ObjectType>("Foo"), operation.SelectionSet, null); IReadOnlyCollection <FieldSelection> fields_b = fieldResolver .CollectFields(schema.GetType <ObjectType>("Fum"), operation.SelectionSet, null); // assert Assert.Collection(fields_a, f => { Assert.Equal("a", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }, f => { Assert.Equal("x", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }); Assert.Collection(fields_b, f => { Assert.Equal("a", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }); }
public void FieldsAndFragmentDefinitionsUnionType() { // arrange Schema schema = CreateSchema(); DocumentNode query = Parser.Default.Parse(@" { a ... Test } fragment Test on FooUnion { x: a } "); var variables = new VariableCollection( TypeConversion.Default, new Dictionary <string, object>()); var fragments = new FragmentCollection(schema, query); OperationDefinitionNode operation = query.Definitions .OfType <OperationDefinitionNode>().First(); // act var fieldResolver = new FieldCollector(variables, fragments); IReadOnlyCollection <FieldSelection> fields_a = fieldResolver .CollectFields(schema.GetType <ObjectType>("Foo"), operation.SelectionSet, e => { }); IReadOnlyCollection <FieldSelection> fields_b = fieldResolver .CollectFields(schema.GetType <ObjectType>("Fum"), operation.SelectionSet, e => { }); // assert Assert.Collection(fields_a, f => { Assert.Equal("a", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }, f => { Assert.Equal("x", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }); Assert.Collection(fields_b, f => { Assert.Equal("a", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }); }
public void SkipOverIncludeFields() { // arrange Schema schema = CreateSchema(); DocumentNode query = Parser.Default.Parse(@" { a @include(if:true) x:c @include(if:true) @skip(if:true) } "); var variables = new VariableCollection( TypeConversion.Default, new Dictionary <string, object>()); var fragments = new FragmentCollection(schema, query); OperationDefinitionNode operation = query.Definitions .OfType <OperationDefinitionNode>().First(); // act var fieldResolver = new FieldCollector(fragments, (f, s) => null); IReadOnlyCollection <FieldSelection> fields = fieldResolver .CollectFields(schema.GetType <ObjectType>("Foo"), operation.SelectionSet, null); // assert Assert.Collection(fields.Where(t => t.IsVisible(variables)), f => { Assert.Equal("a", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }); }
public void InvalidFieldError() { // arrange Schema schema = CreateSchema(); DocumentNode query = Utf8GraphQLParser.Parse(@" { a x:c xyz } "); var variables = new VariableValueCollection( TypeConversion.Default, new Dictionary <string, object>()); var fragments = new FragmentCollection(schema, query); OperationDefinitionNode operation = query.Definitions .OfType <OperationDefinitionNode>().First(); // act var fieldResolver = new FieldCollector( fragments, (f, s) => null, TypeConversion.Default); Action action = () => fieldResolver .CollectFields(schema.GetType <ObjectType>("Foo"), operation.SelectionSet, null); // assert Assert.Throws <QueryException>(action); }
public void Coerce_InputObject_NonNullFieldIsNull() { // arrange DocumentNode document = Utf8GraphQLParser.Parse("{ foo(a: { a: { } }) }"); OperationDefinitionNode operation = document.Definitions .OfType <OperationDefinitionNode>().First(); ISchema schema = CreateSchema(); var fragments = new FragmentCollection( schema, document); var variables = new VariableCollection( TypeConversion.Default, new Dictionary <string, object>()); var collector = new FieldCollector(fragments, (f, s) => null); IReadOnlyCollection <FieldSelection> selections = collector.CollectFields(schema.QueryType, operation.SelectionSet, null); FieldSelection selection = selections.First(); // act Action action = () => selection.CoerceArguments(variables); // assert Assert.Throws <QueryException>(action).Errors.MatchSnapshot(); }
public IReadOnlyCollection <FieldSelection> CollectFields( ObjectType objectType, SelectionSetNode selectionSet, Path path) { if (objectType == null) { throw new ArgumentNullException(nameof(objectType)); } if (selectionSet == null) { throw new ArgumentNullException(nameof(selectionSet)); } IReadOnlyList <FieldSelection> fields = _cachedQuery.GetOrCollectFields( objectType, selectionSet, () => _fieldCollector.CollectFields( objectType, selectionSet, path)); var visibleFields = new List <FieldSelection>(); for (int i = 0; i < fields.Count; i++) { if (fields[i].IsVisible(Variables)) { visibleFields.Add(fields[i]); } } return(visibleFields); }
public void Coerce_NonNullString_ToAbc() { // arrange DocumentNode document = Utf8GraphQLParser.Parse("{ bar (a: \"abc\") }"); OperationDefinitionNode operation = document.Definitions .OfType <OperationDefinitionNode>().First(); ISchema schema = CreateSchema(); var fragments = new FragmentCollection( schema, document); var variables = new VariableCollection( TypeConversion.Default, new Dictionary <string, object>()); var collector = new FieldCollector(fragments, (f, s) => null); IReadOnlyCollection <FieldSelection> selections = collector.CollectFields(schema.QueryType, operation.SelectionSet, Path.New("bar")); FieldSelection selection = selections.First(); var path = Path.New("bar"); // act IReadOnlyDictionary <NameString, ArgumentValue> arguments = selection.CoerceArguments(variables); // assert MatchSnapshot(arguments); }
public void SkipFields() { // arrange Schema schema = CreateSchema(); DocumentNode query = Parser.Default.Parse(@" { a x:c @skip(if:true) } "); VariableCollection variables = new VariableCollection( new Dictionary <string, object>()); FragmentCollection fragments = new FragmentCollection(schema, query); OperationDefinitionNode operation = query.Definitions .OfType <OperationDefinitionNode>().First(); // act FieldCollector fieldResolver = new FieldCollector(schema, variables, fragments); IReadOnlyCollection <FieldSelection> fields = fieldResolver .CollectFields(schema.GetType <ObjectType>("Foo"), operation.SelectionSet, e => { }); // assert Assert.Collection(fields, f => { Assert.Equal("a", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }); }
public void MergeMerged() { // arrange DocumentNode query = Utf8GraphQLParser.Parse( FileResource.Open("MergeQuery.graphql")); OperationDefinitionNode operation = query.Definitions.OfType <OperationDefinitionNode>().Single(); var schema = Schema.Create( FileResource.Open("MergeSchema.graphql"), c => c.Use(next => context => Task.CompletedTask)); var fragments = new FragmentCollection(schema, query); var variables = new VariableValueCollection( TypeConversion.Default, new Dictionary <string, object>()); var collector = new FieldCollector( fragments, (f, s) => null, TypeConversion.Default, Array.Empty <IArgumentCoercionHandler>()); IReadOnlyCollection <FieldSelection> selections = collector.CollectFields(schema.QueryType, operation.SelectionSet, Path.New("foo")); // act selections = collector.CollectFields( schema.GetType <ObjectType>("Application"), selections.Single().Selection.SelectionSet, Path.New("bat")); // assert Assert.Collection(selections, selection => Assert.Equal("id", selection.ResponseName), selection => Assert.Equal("name", selection.ResponseName), selection => Assert.Equal("parts", selection.ResponseName)); }
public void FieldsAndInlineFragments() { // arrange Schema schema = CreateSchema(); DocumentNode query = Utf8GraphQLParser.Parse(@" { a ... on Foo { z:a } ... on Fa { x:a } } "); var variables = new VariableValueCollection( TypeConversion.Default, new Dictionary <string, VariableValue>()); var fragments = new FragmentCollection(schema, query); OperationDefinitionNode operation = query.Definitions .OfType <OperationDefinitionNode>().First(); // act var fieldResolver = new FieldCollector( fragments, (f, s) => null, TypeConversion.Default, Array.Empty <IArgumentCoercionHandler>()); IReadOnlyCollection <FieldSelection> fields = fieldResolver .CollectFields(schema.GetType <ObjectType>("Foo"), operation.SelectionSet, null); // assert Assert.Collection(fields.Where(t => t.IsVisible(variables)), f => { Assert.Equal("a", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }, f => { Assert.Equal("z", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }); }
public void MergeMerged() { // arrange DocumentNode query = Parser.Default.Parse( FileResource.Open("MergeQuery.graphql")); OperationDefinitionNode operation = query.Definitions.OfType <OperationDefinitionNode>().Single(); Schema schema = Schema.Create( FileResource.Open("MergeSchema.graphql"), c => c.Use(next => context => Task.CompletedTask)); var fragments = new FragmentCollection(schema, query); var variables = new VariableCollection( TypeConversion.Default, new Dictionary <string, object>()); var collector = new FieldCollector(variables, fragments); IReadOnlyCollection <FieldSelection> selections = collector.CollectFields(schema.QueryType, operation.SelectionSet, error => { }); // act selections = collector.CollectFields( schema.GetType <ObjectType>("Application"), selections.Single().Selection.SelectionSet, error => { }); // assert Assert.Collection(selections, selection => Assert.Equal("id", selection.ResponseName), selection => Assert.Equal("name", selection.ResponseName), selection => Assert.Equal("parts", selection.ResponseName)); }
public void MergeFieldsWithFragmentSpreads() { // arrange DocumentNode query = Utf8GraphQLParser.Parse( FileResource.Open("MergeQuery.graphql")); OperationDefinitionNode operation = query.Definitions.OfType <OperationDefinitionNode>().Single(); var schema = Schema.Create( FileResource.Open("MergeSchema.graphql"), c => c.Use(next => context => Task.CompletedTask)); var fragments = new FragmentCollection(schema, query); var variables = new VariableValueCollection( TypeConversion.Default, new Dictionary <string, object>()); // act var collector = new FieldCollector( fragments, (f, s) => null, TypeConversion.Default, Array.Empty <IArgumentCoercionHandler>()); IReadOnlyCollection <FieldSelection> selections = collector.CollectFields(schema.QueryType, operation.SelectionSet, Path.New("foo")); // assert Assert.Collection(selections, selection => { Assert.Collection(selection.Selection.SelectionSet.Selections, selectionNode => { FragmentSpreadNode fragment = Assert.IsType <FragmentSpreadNode>( selectionNode); Assert.Equal("app", fragment.Name.Value); }, selectionNode => { FragmentSpreadNode fragment = Assert.IsType <FragmentSpreadNode>( selectionNode); Assert.Equal("parts", fragment.Name.Value); }); }); }
public void Fields() { // arrange var errorRaised = false; Schema schema = CreateSchema(); DocumentNode query = Utf8GraphQLParser.Parse(@" { a x:c } "); var variables = new VariableValueCollection( TypeConversion.Default, new Dictionary <string, object>()); var fragments = new FragmentCollection(schema, query); OperationDefinitionNode operation = query.Definitions .OfType <OperationDefinitionNode>().First(); // act var fieldResolver = new FieldCollector( fragments, (f, s) => null, TypeConversion.Default); IReadOnlyCollection <FieldSelection> fields = fieldResolver .CollectFields( schema.GetType <ObjectType>("Foo"), operation.SelectionSet, null); // assert Assert.Collection(fields.Where(t => t.IsVisible(variables)), f => { Assert.Equal("a", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }, f => { Assert.Equal("x", f.ResponseName); Assert.Equal("c", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }); Assert.False(errorRaised); }
public IReadOnlyCollection <FieldSelection> CollectFields( ObjectType objectType, SelectionSetNode selectionSet) { if (objectType == null) { throw new ArgumentNullException(nameof(objectType)); } if (selectionSet == null) { throw new ArgumentNullException(nameof(selectionSet)); } return(_fieldCollector.CollectFields( objectType, selectionSet, ReportError)); }
public void MergeFieldsWithFragmentSpreads() { // arrange DocumentNode query = Parser.Default.Parse( FileResource.Open("MergeQuery.graphql")); OperationDefinitionNode operation = query.Definitions.OfType <OperationDefinitionNode>().Single(); Schema schema = Schema.Create( FileResource.Open("MergeSchema.graphql"), c => c.Use(next => context => Task.CompletedTask)); var fragments = new FragmentCollection(schema, query); var variables = new VariableCollection( TypeConversion.Default, new Dictionary <string, object>()); // act var collector = new FieldCollector(variables, fragments); IReadOnlyCollection <FieldSelection> selections = collector.CollectFields(schema.QueryType, operation.SelectionSet, error => { }); // assert Assert.Collection(selections, selection => { Assert.Collection(selection.Selection.SelectionSet.Selections, selectionNode => { var fragment = Assert.IsType <FragmentSpreadNode>( selectionNode); Assert.Equal("app", fragment.Name.Value); }, selectionNode => { var fragment = Assert.IsType <FragmentSpreadNode>( selectionNode); Assert.Equal("parts", fragment.Name.Value); }); }); }
public void InvalidFieldError() { // arrange var errorRaised = false; Schema schema = CreateSchema(); DocumentNode query = Parser.Default.Parse(@" { a x:c xyz } "); var variables = new VariableCollection( TypeConversion.Default, new Dictionary <string, object>()); var fragments = new FragmentCollection(schema, query); OperationDefinitionNode operation = query.Definitions .OfType <OperationDefinitionNode>().First(); // act var fieldResolver = new FieldCollector(variables, fragments); IReadOnlyCollection <FieldSelection> fields = fieldResolver .CollectFields(schema.GetType <ObjectType>("Foo"), operation.SelectionSet, e => { errorRaised = true; }); // assert Assert.Collection(fields, f => { Assert.Equal("a", f.ResponseName); Assert.Equal("a", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }, f => { Assert.Equal("x", f.ResponseName); Assert.Equal("c", f.Field.Name); Assert.Equal("String", f.Field.Type.TypeName()); }); Assert.True(errorRaised); }
public void Coerce_NonNullString_ToNull() { // arrange DocumentNode document = Utf8GraphQLParser.Parse("{ bar }"); OperationDefinitionNode operation = document.Definitions .OfType <OperationDefinitionNode>().First(); ISchema schema = CreateSchema(); var fragments = new FragmentCollection( schema, document); var variables = new VariableValueCollection( TypeConversion.Default, new Dictionary <string, object>()); var collector = new FieldCollector( fragments, (f, s) => null, TypeConversion.Default, Array.Empty <IArgumentCoercionHandler>()); IReadOnlyCollection <FieldSelection> selections = collector.CollectFields(schema.QueryType, operation.SelectionSet, Path.New("bar")); FieldSelection selection = selections.First(); var path = Path.New("bar"); // act Action action = () => selection.CoerceArguments(variables, TypeConversion.Default); // assert Assert.Throws <QueryException>(action).Errors.MatchSnapshot(); }