private static async Task <SchemaFeatures> GetSchemaFeaturesAsync( HttpClient httpClient, HttpQueryClient queryClient) { var features = new SchemaFeatures(); var request = new HttpQueryRequest { Query = GetIntrospectionQuery(_phase1) }; string json = await queryClient.FetchStringAsync( request, httpClient) .ConfigureAwait(false); IntrospectionResult result = JsonConvert.DeserializeObject <IntrospectionResult>(json); FullType directive = result.Data.Schema.Types.First(t => t.Name.Equals(_directiveName, StringComparison.Ordinal)); features.HasRepeatableDirectives = directive.Fields.Any(t => t.Name.Equals(_isRepeatable, StringComparison.Ordinal)); features.HasDirectiveLocations = directive.Fields.Any(t => t.Name.Equals(_locations, StringComparison.Ordinal)); FullType schema = result.Data.Schema.Types.First(t => t.Name.Equals(_schemaName, StringComparison.Ordinal)); features.HasSubscriptionSupport = schema.Fields.Any(t => t.Name.Equals(_subscriptionType, StringComparison.Ordinal)); return(features); }
private static DocumentNode CreateIntrospectionQuery( SchemaFeatures features) { DocumentNode query = Parser.Default.Parse( GetIntrospectionQuery(_phase2)); OperationDefinitionNode operation = query.Definitions.OfType <OperationDefinitionNode>().First(); FieldNode schema = operation.SelectionSet.Selections.OfType <FieldNode>().First(); FieldNode directives = schema.SelectionSet.Selections.OfType <FieldNode>().First(t => t.Name.Value.Equals(_directivesField , StringComparison.Ordinal)); var selections = directives.SelectionSet.Selections.ToList(); if (features.HasDirectiveLocations) { selections.Add(CreateField(_locations)); } else { selections.Add(CreateField(_onField)); selections.Add(CreateField(_onFragment)); selections.Add(CreateField(_onOperation)); } if (features.HasRepeatableDirectives) { selections.Add(CreateField(_isRepeatable)); } FieldNode newField = directives.WithSelectionSet( directives.SelectionSet.WithSelections(selections)); selections = schema.SelectionSet.Selections.ToList(); selections.Remove(directives); selections.Add(newField); newField = schema.WithSelectionSet( schema.SelectionSet.WithSelections(selections)); selections = operation.SelectionSet.Selections.ToList(); selections.Remove(schema); selections.Add(newField); OperationDefinitionNode newOp = operation.WithSelectionSet( operation.SelectionSet.WithSelections(selections)); var definitions = query.Definitions.ToList(); definitions.Remove(operation); definitions.Insert(0, newOp); return(query.WithDefinitions(definitions)); }
private static void RemoveSubscriptionIfNotSupported( SchemaFeatures features, ICollection <ISelectionNode> selections) { if (!features.HasSubscriptionSupport) { FieldNode subscriptionField = selections.OfType <FieldNode>() .First(t => t.Name.Value.Equals(_subscriptionType, StringComparison.Ordinal)); selections.Remove(subscriptionField); } }
private static async Task <DocumentNode> LoadSchemaInternalAsync( HttpClient httpClient) { var queryClient = new HttpQueryClient(); SchemaFeatures features = await GetSchemaFeaturesAsync(httpClient, queryClient) .ConfigureAwait(false); return(await ExecuteIntrospectionAsync( httpClient, queryClient, features) .ConfigureAwait(false)); }
public void IntrospectionWithoutDirectiveIsRepeatableField() { // arrange var features = new SchemaFeatures { HasRepeatableDirectives = false }; // act DocumentNode document = IntrospectionClient.CreateIntrospectionQuery(features); // assert QuerySyntaxSerializer.Serialize(document).MatchSnapshot(); }
public void IntrospectionWithSubscription() { // arrange var features = new SchemaFeatures { HasSubscriptionSupport = true }; // act DocumentNode document = IntrospectionClient.CreateIntrospectionQuery(features); // assert QuerySyntaxSerializer.Serialize(document).MatchSnapshot(); }
public void IntrospectionWithDirectiveLocationField() { // arrange var features = new SchemaFeatures { HasDirectiveLocations = true }; // act DocumentNode document = IntrospectionClient.CreateIntrospectionQuery(features); // assert QuerySyntaxSerializer.Serialize(document).MatchSnapshot(); }
internal static DocumentNode CreateIntrospectionQuery( SchemaFeatures features) { DocumentNode query = Parser.Default.Parse( GetIntrospectionQuery(_phase2)); OperationDefinitionNode operation = query.Definitions.OfType <OperationDefinitionNode>().First(); FieldNode schema = operation.SelectionSet.Selections.OfType <FieldNode>().First(); FieldNode directives = schema.SelectionSet.Selections.OfType <FieldNode>().First(t => t.Name.Value.Equals(_directivesField, StringComparison.Ordinal)); var selections = directives.SelectionSet.Selections.ToList(); AddDirectiveFeatures(features, selections); FieldNode newField = directives.WithSelectionSet( directives.SelectionSet.WithSelections(selections)); selections = schema.SelectionSet.Selections.ToList(); RemoveSubscriptionIfNotSupported(features, selections); selections.Remove(directives); selections.Add(newField); newField = schema.WithSelectionSet( schema.SelectionSet.WithSelections(selections)); selections = operation.SelectionSet.Selections.ToList(); selections.Remove(schema); selections.Add(newField); OperationDefinitionNode newOp = operation.WithSelectionSet( operation.SelectionSet.WithSelections(selections)); var definitions = query.Definitions.ToList(); definitions.Remove(operation); definitions.Insert(0, newOp); return(query.WithDefinitions(definitions)); }
private static async Task <DocumentNode> ExecuteIntrospectionAsync( HttpClient httpClient, HttpQueryClient queryClient, SchemaFeatures features) { DocumentNode query = CreateIntrospectionQuery(features); var request = new HttpQueryRequest { Query = QuerySyntaxSerializer.Serialize(query) }; string json = await queryClient.FetchStringAsync( request, httpClient) .ConfigureAwait(false); return(IntrospectionDeserializer.Deserialize(json)); }
private static void AddDirectiveFeatures( SchemaFeatures features, ICollection <ISelectionNode> selections) { if (features.HasDirectiveLocations) { selections.Add(CreateField(_locations)); } else { selections.Add(CreateField(_onField)); selections.Add(CreateField(_onFragment)); selections.Add(CreateField(_onOperation)); } if (features.HasRepeatableDirectives) { selections.Add(CreateField(_isRepeatable)); } }
private static async Task <DocumentNode> ExecuteIntrospectionAsync( HttpClient httpClient, HttpQueryClient queryClient, SchemaFeatures features) { DocumentNode query = CreateIntrospectionQuery(features); var request = new HttpQueryRequest { Query = QuerySyntaxSerializer.Serialize(query), OperationName = "introspection_phase_2" }; (string json, HttpResponseMessage _)response = await queryClient.FetchStringAsync( request, httpClient) .ConfigureAwait(false); return(IntrospectionDeserializer.Deserialize(response.json)); }