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));
        }
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));
        }