示例#1
0
        private object GetDefinitionAndExecuteField(GraphQLObjectType type, GraphQLFieldSelection selection)
        {
            var arguments     = this.GetArgumentsFromSelection(selection);
            var fieldInfo     = this.GetFieldInfo(type, selection);
            var resolvedValue = this.ResolveField(fieldInfo, arguments, this.parent);

            return(this.CompleteValue(resolvedValue, selection, arguments));
        }
示例#2
0
        public DefinitionTests()
        {
            var BlogImage = new GraphQLObjectType("Image",
                fields: new GraphQLFieldDefinitionMap(
                    new []
                    {
                        new GraphQLFieldDefinition("url") {Type = Scalars.GraphQLString,},
                        new GraphQLFieldDefinition("width") {Type = Scalars.GraphQLInt,},
                        new GraphQLFieldDefinition("height") {Type = Scalars.GraphQLInt,},
                    })
                );

            var BlogAuthor = new GraphQLObjectType("Author",
                fields: new GraphQLFieldDefinitionMap(
                    new []
                    {
                        new GraphQLFieldDefinition("id") {Type = Scalars.GraphQLString,},
                        new GraphQLFieldDefinition("name") {Type = Scalars.GraphQLString,},
                        new GraphQLFieldDefinition("pic", args: new []
                        {
                            new GraphQLArgument("width", Scalars.GraphQLInt),
                            new GraphQLArgument("height", Scalars.GraphQLInt),
                        }, type: Scalars.GraphQLString),
                        new GraphQLFieldDefinition("recentArticle", type: BlogImage),
                    }));

            var BlogArticle = new GraphQLObjectType("Article",
                fields: new GraphQLFieldDefinitionMap(
                    new []
                    {
                        new GraphQLFieldDefinition("id", type: Scalars.GraphQLString),
                        new GraphQLFieldDefinition("isPublished", type: Scalars.GraphQLBoolean),
                        new GraphQLFieldDefinition("author", type: BlogAuthor),
                        new GraphQLFieldDefinition("title", type: Scalars.GraphQLString),
                        new GraphQLFieldDefinition("body", type: Scalars.GraphQLString),
                    }));
            var BlogQuery = new GraphQLObjectType("Query",
                fields: new GraphQLFieldDefinitionMap(
                    new []
                    {
                        new GraphQLFieldDefinition("article", args: new []
                        {
                            new GraphQLArgument("id", Scalars.GraphQLString),
                        }, type: BlogArticle),
                        new GraphQLFieldDefinition("feed", type: new GraphQLList(BlogArticle)),
                    }));
            var BlogMutation = new GraphQLObjectType("Mutation",
                fields: new GraphQLFieldDefinitionMap(
                    new []
                    {
                        new GraphQLFieldDefinition("writeArticle", type: BlogArticle),
                    }));
            var ObjectType = new GraphQLObjectType("Object");
            var InterfaceType = new GraphQLObjectType("Interface");
            var UnionType = new GraphQLUnionType("Union", types: ImmutableArray.Create(new GraphQLObjectType()));
        }
示例#3
0
        public static IntrospectedType CreateForObject(GraphQLObjectType type, IIntrospector introspector, IObjectTypeTranslator typeObserver)
        {
            var introspectedType = new ComplexIntrospectedType(introspector, typeObserver);

            introspectedType.Name        = type.Name;
            introspectedType.Description = type.Description;
            introspectedType.Kind        = TypeKind.OBJECT;

            return(introspectedType);
        }
示例#4
0
        public Dictionary <string, IList <GraphQLFieldSelection> > CollectFields(GraphQLObjectType runtimeType, GraphQLSelectionSet selectionSet)
        {
            var fields = new Dictionary <string, IList <GraphQLFieldSelection> >();

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

            return(fields);
        }
示例#5
0
        private void CollectFieldsInSelection(GraphQLObjectType 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;
            }
        }
示例#6
0
 private async Task <ExpandoObject> TryGetObject(FieldScope scope, GraphQLObjectType input, GraphQLFieldSelection selection, IEnumerable <object> path)
 {
     try
     {
         return(await scope.GetObject(this.context.FieldCollector.CollectFields(input, selection.SelectionSet)));
     }
     catch (GraphQLResolveException)
     {
         return(null);
     }
 }
示例#7
0
        private string PrintObject(GraphQLObjectType type)
        {
            var interfaces            = this.schema.SchemaRepository.GetImplementingInterfaces(type);
            var implementedInterfaces = interfaces.Any()
                ? $" implements {string.Join(", ", interfaces.Select(e => e.Name))}"
                : string.Empty;

            return
                (PrintDescription(type) +
                 $"type {type.Name}{implementedInterfaces} {{\n" +
                 $"{this.PrintFields(type)}\n}}");
        }
示例#8
0
        private bool DoesFragmentConditionMatch(GraphQLObjectType runtimeType, GraphQLInlineFragment fragment)
        {
            if (fragment.TypeCondition == null)
            {
                return(true);
            }

            if (fragment.TypeCondition.Name.Value == runtimeType.Name)
            {
                return(true);
            }

            return(false);
        }
示例#9
0
        private object CompleteObjectType(
            GraphQLObjectType input, GraphQLFieldSelection selection, IList <GraphQLArgument> arguments, object parentObject)
        {
            var scope = new FieldScope(
                this.typeTranslator,
                this.valueResolver,
                this.fieldCollector,
                input,
                parentObject);

            scope.arguments = arguments.ToList();

            return(scope.GetObject(this.fieldCollector.CollectFields(input, selection.SelectionSet)));
        }
示例#10
0
        public FieldScope(
            ITypeTranslator typeTranslator,
            IValueResolver valueResolver,
            IFieldCollector fieldCollector,
            GraphQLObjectType type,
            object parent)
        {
            this.type      = type;
            this.parent    = parent;
            this.arguments = new List <GraphQLArgument>();

            this.fieldCollector = fieldCollector;
            this.typeTranslator = typeTranslator;
            this.valueResolver  = valueResolver;
        }
示例#11
0
        private void CollectFragmentFields(GraphQLObjectType runtimeType, GraphQLInlineFragment fragment, Dictionary <string, IList <GraphQLFieldSelection> > fields)
        {
            if (!this.ShouldIncludeNode(fragment.Directives))
            {
                return;
            }

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

            this.CollectFields(runtimeType, fragment.SelectionSet)
            .ToList().ForEach(e => fields.Add(e.Key, e.Value));
        }
示例#12
0
        private dynamic ComposeResultForType(GraphQLObjectType type, GraphQLSelectionSet selectionSet)
        {
            var variableResolver = new VariableResolver(this.variables, this.graphQLSchema.TypeTranslator, this.operation.VariableDefinitions);
            var valueResolver    = new ValueResolver(variableResolver, this.graphQLSchema.TypeTranslator);
            var fieldCollector   = new FieldCollector(this.fragments, valueResolver);

            var scope = new FieldScope(
                this.graphQLSchema.TypeTranslator,
                valueResolver,
                fieldCollector,
                type,
                null);

            return(scope.GetObject(fieldCollector.CollectFields(type, selectionSet)));
        }
示例#13
0
        private async Task <object> CompleteObjectType(
            GraphQLObjectType input,
            GraphQLFieldSelection selection,
            IList <GraphQLArgument> arguments,
            object parentObject,
            IEnumerable <object> path)
        {
            var aliasOrName = selection.Alias?.Value ?? selection.Name.Value;

            var scope = new FieldScope(this.context, input, parentObject, path.Append(aliasOrName), this.Errors)
            {
                arguments = arguments.ToList()
            };

            return(await this.TryGetObject(scope, input, selection, path));
        }
示例#14
0
        public object CreateObjectFromDynamic(GraphQLObjectType inputObjectType, ExpandoObject inputObject)
        {
            var systemType            = this.GetType(inputObjectType);
            var fields                = GetAccessorsFromType(inputObjectType);
            var inputObjectDictionary = (IDictionary <string, object>)inputObject;

            var resultObject = Activator.CreateInstance(systemType);

            foreach (var field in fields)
            {
                if (!inputObjectDictionary.ContainsKey(field.Name))
                {
                    continue;
                }

                this.AssignValueToField(inputObjectDictionary[field.Name], resultObject, field.Lambda);
            }

            return(resultObject);
        }
示例#15
0
 public void SetUp()
 {
     this.schema = new GraphQLSchema();
     this.root   = new QueryRootType(this.schema);
 }
示例#16
0
        public DefinitionTests()
        {
            var BlogImage = new GraphQLObjectType("Image",
                                                  fields: new GraphQLFieldDefinitionMap(
                                                      new []
            {
                new GraphQLFieldDefinition("url")
                {
                    Type = Scalars.GraphQLString,
                },
                new GraphQLFieldDefinition("width")
                {
                    Type = Scalars.GraphQLInt,
                },
                new GraphQLFieldDefinition("height")
                {
                    Type = Scalars.GraphQLInt,
                },
            })
                                                  );

            var BlogAuthor = new GraphQLObjectType("Author",
                                                   fields: new GraphQLFieldDefinitionMap(
                                                       new []
            {
                new GraphQLFieldDefinition("id")
                {
                    Type = Scalars.GraphQLString,
                },
                new GraphQLFieldDefinition("name")
                {
                    Type = Scalars.GraphQLString,
                },
                new GraphQLFieldDefinition("pic", args: new []
                {
                    new GraphQLArgument("width", Scalars.GraphQLInt),
                    new GraphQLArgument("height", Scalars.GraphQLInt),
                }, type: Scalars.GraphQLString),
                new GraphQLFieldDefinition("recentArticle", type: BlogImage),
            }));

            var BlogArticle = new GraphQLObjectType("Article",
                                                    fields: new GraphQLFieldDefinitionMap(
                                                        new []
            {
                new GraphQLFieldDefinition("id", type: Scalars.GraphQLString),
                new GraphQLFieldDefinition("isPublished", type: Scalars.GraphQLBoolean),
                new GraphQLFieldDefinition("author", type: BlogAuthor),
                new GraphQLFieldDefinition("title", type: Scalars.GraphQLString),
                new GraphQLFieldDefinition("body", type: Scalars.GraphQLString),
            }));
            var BlogQuery = new GraphQLObjectType("Query",
                                                  fields: new GraphQLFieldDefinitionMap(
                                                      new []
            {
                new GraphQLFieldDefinition("article", args: new []
                {
                    new GraphQLArgument("id", Scalars.GraphQLString),
                }, type: BlogArticle),
                new GraphQLFieldDefinition("feed", type: new GraphQLList(BlogArticle)),
            }));
            var BlogMutation = new GraphQLObjectType("Mutation",
                                                     fields: new GraphQLFieldDefinitionMap(
                                                         new []
            {
                new GraphQLFieldDefinition("writeArticle", type: BlogArticle),
            }));
            var ObjectType    = new GraphQLObjectType("Object");
            var InterfaceType = new GraphQLObjectType("Interface");
            var UnionType     = new GraphQLUnionType("Union", types: ImmutableArray.Create(new GraphQLObjectType()));
        }
示例#17
0
 private static List <GraphQLObjectTypeFieldInfo> GetAccessorsFromType(GraphQLObjectType inputObjectType)
 {
     return(inputObjectType.GetFieldsInfo()
            .Where(e => e.IsResolver == false)
            .ToList());
 }
示例#18
0
        private void CollectFragmentSpreadFields(GraphQLObjectType runtimeType, GraphQLFragmentSpread fragmentSpread, Dictionary <string, IList <GraphQLFieldSelection> > fields)
        {
            var fragment = this.fragments[fragmentSpread.Name.Value];

            this.CollectFragmentFields(runtimeType, fragment, fields);
        }
示例#19
0
 private IntrospectedType IntrospectObjectType(GraphQLObjectType type)
 {
     return(IntrospectedType.CreateForObject(type, this, this.typeTranslator.GetObjectTypeTranslatorFor(type)));
 }
示例#20
0
        private GraphQLObjectTypeFieldInfo GetFieldInfo(GraphQLObjectType type, GraphQLFieldSelection selection)
        {
            var name = this.GetFieldName(selection);

            return(type.GetFieldInfo(name));
        }
 public void SetUp()
 {
     this.type = new GraphQLTestModelType();
 }