Exemple #1
0
        public async Task <ExpandoObject> ComposeResultForSubscriptions(
            GraphQLComplexType type, GraphQLOperationDefinition operationDefinition)
        {
            if (string.IsNullOrWhiteSpace(this.clientId))
            {
                throw new GraphQLException(
                          "Can't invoke subscription without clientId specified",
                          new ASTNode[] { operationDefinition });
            }

            if (!this.subscriptionId.HasValue)
            {
                throw new GraphQLException(
                          "Can't invoke subscription without subscriptionId specified",
                          new ASTNode[] { operationDefinition });
            }

            var context = this.CreateExecutionContext(operationDefinition);

            var scope = new FieldScope(context, type, null);

            return(await this.ProcessSubscription(
                       (GraphQLSubscriptionType)type,
                       context.FieldCollector,
                       scope));
        }
Exemple #2
0
        public GraphQLComplexType[] GetImplementingInterfaces(GraphQLComplexType type)
        {
            var interfacesTypes = ReflectionUtilities.GetAllImplementingInterfaces(type.SystemType);

            return(interfacesTypes.Select(e => this.GetSchemaTypeForWithNoError(e))
                   .Select(e => e as GraphQLComplexType)
                   .Where(e => e != null)
                   .ToArray());
        }
        private void CollectFragmentSpreadFields(GraphQLComplexType runtimeType, GraphQLFragmentSpread fragmentSpread, Dictionary <string, IList <GraphQLFieldSelection> > fields)
        {
            if (!this.ShouldIncludeNode(fragmentSpread.Directives, DirectiveLocation.FRAGMENT_SPREAD))
            {
                return;
            }

            var fragment = this.fragments[fragmentSpread.Name.Value];

            this.CollectFragmentFields(runtimeType, fragment, fields);
        }
        private void CollectFieldsInSelection(GraphQLComplexType runtimeType, ASTNode selection, Dictionary <string, IList <GraphQLFieldSelection> > fields)
        {
            switch (selection.Kind)
            {
            case ASTNodeKind.Field: this.CollectField((GraphQLFieldSelection)selection, fields); break;

            case ASTNodeKind.FragmentSpread: this.CollectFragmentSpreadFields(runtimeType, (GraphQLFragmentSpread)selection, fields); break;

            case ASTNodeKind.InlineFragment: this.CollectFragmentFields(runtimeType, (GraphQLInlineFragment)selection, fields); break;
            }
        }
        public Dictionary <string, IList <GraphQLFieldSelection> > CollectFields(
            GraphQLComplexType runtimeType, GraphQLSelectionSet selectionSet)
        {
            var fields = new Dictionary <string, IList <GraphQLFieldSelection> >();

            foreach (var selection in selectionSet.Selections)
            {
                this.CollectFieldsInSelection(runtimeType, selection, fields);
            }

            return(fields);
        }
Exemple #6
0
 public FieldScope(
     ExecutionContext context,
     GraphQLComplexType type,
     object parent,
     IEnumerable <object> parentPath       = null,
     IList <GraphQLException> parentErrors = null)
 {
     this.type      = type;
     this.parent    = parent;
     this.arguments = new List <GraphQLArgument>();
     this.context   = context;
     this.path      = parentPath ?? new List <object>();
     this.Errors    = parentErrors ?? new List <GraphQLException>();
 }
        private void CollectFragmentFields(GraphQLComplexType runtimeType, GraphQLInlineFragment fragment, Dictionary <string, IList <GraphQLFieldSelection> > fields)
        {
            if (!this.ShouldIncludeNode(fragment.Directives, DirectiveLocation.INLINE_FRAGMENT))
            {
                return;
            }

            if (!this.DoesFragmentConditionMatch(runtimeType, fragment))
            {
                return;
            }

            this.CollectFields(runtimeType, fragment.SelectionSet)
            .ToList().ForEach(e => fields.Add(e.Key, e.Value));
        }
        private bool DoesFragmentConditionMatch(GraphQLComplexType runtimeType, GraphQLInlineFragment fragment)
        {
            if (fragment.TypeCondition == null)
            {
                return(true);
            }

            var type = this.schemaRepository.GetSchemaOutputTypeByName(fragment.TypeCondition.Name.Value);

            if (type == runtimeType || TypeComparators.IsPossibleType(runtimeType, type, this.schemaRepository))
            {
                return(true);
            }

            return(false);
        }
Exemple #9
0
        public async Task <ExpandoObject> ComposeResultForMutation(
            GraphQLComplexType type, GraphQLOperationDefinition operationDefinition)
        {
            var context = this.CreateExecutionContext(operationDefinition);
            var scope   = new FieldScope(context, type, null);

            var fields       = context.FieldCollector.CollectFields(type, operationDefinition.SelectionSet);
            var resultObject = await scope.GetObjectSynchronously(fields);

            await this.AppendIntrospectionInfo(scope, fields, resultObject);

            var returnObject           = new ExpandoObject();
            var returnObjectDictionary = (IDictionary <string, object>)returnObject;

            returnObjectDictionary.Add("data", resultObject);

            if (scope.Errors.Any())
            {
                returnObjectDictionary.Add("errors", scope.Errors);
            }

            return(returnObject);
        }
Exemple #10
0
        private async Task <object> GetDefinitionAndExecuteField(
            GraphQLComplexType type,
            GraphQLFieldSelection selection,
            IDictionary <string, object> dictionary)
        {
            var fieldInfo       = this.GetFieldInfo(type, selection);
            var arguments       = this.GetArgumentsFromSelection(selection, fieldInfo)?.ToList() ?? new List <GraphQLArgument>();
            var directivesToUse = selection.Directives;

            Func <Task <object> > fieldResolver = async() =>
            {
                var fieldResult = await this.TryResolveField(selection, fieldInfo, arguments, this.parent);

                await this.PublishToEventChannel(fieldInfo, fieldResult);

                return(fieldResult);
            };

            var result = await this.ApplyDirectivesToResult(selection, dictionary, fieldResolver);

            var resultType = this.GetResultType(type, fieldInfo, result);

            return(await this.CompleteValue(result, resultType, selection, arguments, this.path));
        }
Exemple #11
0
        private GraphQLObjectTypeFieldInfo GetFieldInfo(GraphQLComplexType type, GraphQLFieldSelection selection)
        {
            var name = this.GetFieldName(selection);

            return(type.GetFieldInfo(name));
        }
Exemple #12
0
 private Type GetResultType(
     GraphQLComplexType type, GraphQLObjectTypeFieldInfo fieldInfo, object result)
 {
     return(fieldInfo?.SystemType ?? result?.GetType());
 }
Exemple #13
0
        private string PrintFields(GraphQLComplexType type)
        {
            var fields = type.GetFieldsInfo();

            return(string.Join("\n", fields.Select((field, i) => this.PrintField(field, i == 0))));
        }
 internal ComplexIntrospectedType(ISchemaRepository schemaRepository, GraphQLComplexType type)
 {
     this.schemaRepository = schemaRepository;
     this.type             = type;
 }