public static HttpQueryRequest CreateIntrospectionQuery(ISchemaFeatures features)
        {
            DocumentNode document   = CreateIntrospectionQueryDocument(features);
            string       sourceText = QuerySyntaxSerializer.Serialize(document, false);

            return(new HttpQueryRequest(sourceText, "introspection_phase_2"));
        }
Example #2
0
        private static DocumentNode CreateIntrospectionQueryDocument(ISchemaFeatures features)
        {
            DocumentNode query = Utf8GraphQLParser.Parse(GetIntrospectionQuery());

            OperationDefinitionNode operation =
                query.Definitions.OfType <OperationDefinitionNode>().First();

            FieldNode schema =
                operation.SelectionSet.Selections.OfType <FieldNode>().First();

            if (schema.SelectionSet is null)
            {
                throw new IntrospectionException();
            }

            FieldNode directives =
                schema.SelectionSet.Selections.OfType <FieldNode>().First(t =>
                                                                          t.Name.Value.Equals(_directivesField,
                                                                                              StringComparison.Ordinal));

            if (directives.SelectionSet is null)
            {
                throw new IntrospectionException();
            }

            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));
        }
Example #3
0
 private static void RemoveSubscriptionIfNotSupported(
     ISchemaFeatures features,
     ICollection <ISelectionNode> selections)
 {
     if (!features.HasSubscriptionSupport)
     {
         FieldNode subscriptionField = selections.OfType <FieldNode>()
                                       .First(t => t.Name.Value.Equals(_subscriptionType,
                                                                       StringComparison.Ordinal));
         selections.Remove(subscriptionField);
     }
 }
        public async Task GetSchemaFeatures()
        {
            // arrange
            TestServer server = CreateStarWarsServer();
            var        introspectionClient = new IntrospectionClient();

            // act
            ISchemaFeatures features =
                await introspectionClient.GetSchemaFeaturesAsync(
                    server.CreateClient());

            // assert
            Assert.True(features.HasDirectiveLocations);
            Assert.True(features.HasRepeatableDirectives);
            Assert.True(features.HasSubscriptionSupport);
        }
Example #5
0
        public async Task GetSchemaFeatures()
        {
            // arrange
            TestServer server = CreateStarWarsServer();
            HttpClient client = server.CreateClient();

            client.BaseAddress = new Uri("http://localhost:5000/graphql");

            var introspectionClient = new IntrospectionClient();

            // act
            ISchemaFeatures features = await introspectionClient.GetSchemaFeaturesAsync(client);

            // assert
            Assert.True(features.HasDirectiveLocations);
            Assert.True(features.HasRepeatableDirectives);
            Assert.True(features.HasSubscriptionSupport);
        }
        private static void AddDirectiveFeatures(
            ISchemaFeatures features,
            ICollection <ISelectionNode> selections)
        {
            if (features.HasDirectiveLocations)
            {
                selections.Add(CreateField(SchemaFeatures.Locations));
            }
            else
            {
                selections.Add(CreateField(_onField));
                selections.Add(CreateField(_onFragment));
                selections.Add(CreateField(_onOperation));
            }

            if (features.HasRepeatableDirectives)
            {
                selections.Add(CreateField(SchemaFeatures.IsRepeatable));
            }
        }
Example #7
0
        public async Task <DocumentNode> DownloadSchemaAsync(
            HttpClient client,
            CancellationToken cancellationToken = default)
        {
            if (client is null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            ISchemaFeatures features = await GetSchemaFeaturesAsync(
                client, cancellationToken)
                                       .ConfigureAwait(false);

            HttpQueryRequest request = IntrospectionQueryHelper.CreateIntrospectionQuery(features);

            IntrospectionResult result = await ExecuteIntrospectionAsync(
                client, request, cancellationToken)
                                         .ConfigureAwait(false);

            EnsureNoGraphQLErrors(result);

            return(IntrospectionDeserializer.Deserialize(result).RemoveBuiltInTypes());
        }