Ejemplo n.º 1
0
        public ISet <FieldDependency> GetFieldDependencies(
            DocumentNode document,
            SelectionSetNode selectionSet,
            INamedOutputType declaringType)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (selectionSet == null)
            {
                throw new ArgumentNullException(nameof(selectionSet));
            }

            if (declaringType == null)
            {
                throw new ArgumentNullException(nameof(declaringType));
            }

            var context = Context.New(declaringType, GetFragments(document));

            VisitSelectionSet(selectionSet, context);

            return(context.Dependencies);
        }
Ejemplo n.º 2
0
        public FieldCollectionResult CollectFields(
            INamedOutputType type,
            SelectionSetNode selectionSet,
            Path path)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (selectionSet == null)
            {
                throw new ArgumentNullException(nameof(selectionSet));
            }

            var fields = new OrderedDictionary <string, FieldSelection>();
            var root   = new FragmentNode();

            CollectFields(type, selectionSet, path, fields, root);

            return(new FieldCollectionResult(
                       type,
                       selectionSet,
                       fields.Values.ToList(),
                       root.Children));
        }
Ejemplo n.º 3
0
        private SelectionInfo CollectFieldsInternal(
            INamedOutputType type,
            SelectionSetNode selectionSet,
            Path path)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (selectionSet == null)
            {
                throw new ArgumentNullException(nameof(selectionSet));
            }

            var fields    = new OrderedDictionary <string, FieldSelection>();
            var fragments = new List <IFragmentNode>();

            CollectFields(type, selectionSet, path, fields, fragments);

            return(new SelectionInfo(
                       type,
                       selectionSet,
                       fields.Values.ToList(),
                       fragments));
        }
Ejemplo n.º 4
0
 private Context(Context context, INamedOutputType typeContext)
 {
     Dependencies = context.Dependencies;
     TypeContext  = typeContext;
     Fragments    = context.Fragments;
     FragmentPath = context.FragmentPath;
 }
Ejemplo n.º 5
0
        public void DiscoverInputArgumentTypes()
        {
            // arrange
            // act
            ISchema schema = Schema.Create(c =>
            {
                c.RegisterQueryType <QueryFieldArgument>();
            });

            // assert
            INamedOutputType foo =
                schema.GetType <INamedOutputType>("Foo");

            Assert.NotNull(foo);

            INamedOutputType bar =
                schema.GetType <INamedOutputType>("Bar");

            Assert.NotNull(foo);

            INamedInputType fooInput =
                schema.GetType <INamedInputType>("FooInput");

            Assert.NotNull(fooInput);

            INamedInputType barInput =
                schema.GetType <INamedInputType>("BarInput");

            Assert.NotNull(barInput);
        }
Ejemplo n.º 6
0
        protected override void OnCompleteType(
            ITypeInitializationContext context)
        {
            EntityType = context.GetType <T>(
                new TypeReference(typeof(T)));

            base.OnCompleteType(context);
        }
Ejemplo n.º 7
0
 private Context(
     INamedOutputType typeContext,
     IDictionary <string, FragmentDefinitionNode> fragments)
 {
     Dependencies = new HashSet <FieldDependency>();
     TypeContext  = typeContext;
     Fragments    = fragments;
     FragmentPath = ImmutableHashSet <string> .Empty;
 }
Ejemplo n.º 8
0
        public override MaxComplexityVisitorContext SetTypeContext(
            INamedOutputType typeContext)
        {
            var newContext =
                new MaxComplexityWithMultipliersVisitorContext(this);

            newContext.TypeContext = typeContext;
            return(newContext);
        }
Ejemplo n.º 9
0
        public PossibleSelections CollectFields(
            INamedOutputType type,
            SelectionSetNode selectionSet,
            Path path)
        {
            if (!_cache.TryGetValue(type, out SelectionCache? selectionCache))
            {
                selectionCache = new SelectionCache();
                _cache.Add(type, selectionCache);
            }

            if (!selectionCache.TryGetValue(
                    selectionSet,
                    out PossibleSelections? possibleSelections))
            {
                SelectionInfo returnType =
                    CollectFieldsInternal(type, selectionSet, path);

                if (type.IsAbstractType())
                {
                    var  list             = new List <SelectionInfo>();
                    bool singleModelShape = true;

                    foreach (ObjectType objectType in _schema.GetPossibleTypes(type))
                    {
                        SelectionInfo objectSelection =
                            CollectFieldsInternal(objectType, selectionSet, path);
                        list.Add(objectSelection);

                        if (!FieldSelectionsAreEqual(
                                returnType.Fields,
                                objectSelection.Fields))
                        {
                            singleModelShape = false;
                        }
                    }

                    if (!singleModelShape)
                    {
                        possibleSelections = new PossibleSelections(returnType, list);
                    }
                }

                if (possibleSelections is null)
                {
                    possibleSelections = new PossibleSelections(returnType);
                }

                selectionCache.Add(selectionSet, possibleSelections);
            }

            return(possibleSelections);
        }
Ejemplo n.º 10
0
        public ExtractedField ExtractField(
            NameString sourceSchema,
            DocumentNode document,
            OperationDefinitionNode operation,
            IFieldSelection selection,
            INamedOutputType declaringType)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            if (selection == null)
            {
                throw new ArgumentNullException(nameof(selection));
            }

            if (declaringType == null)
            {
                throw new ArgumentNullException(nameof(declaringType));
            }

            sourceSchema.EnsureNotEmpty(nameof(sourceSchema));

            var context = new Context(
                sourceSchema, declaringType,
                document, operation);

            var syntaxNodes = new List <FieldNode>();

            foreach (FieldNode syntaxNode in selection.SyntaxNodes)
            {
                FieldNode field = RewriteField(syntaxNode, context);

                if (selection.Field.Type.NamedType().IsLeafType() ||
                    (field.SelectionSet is not null &&
                     field.SelectionSet.Selections.Count > 0))
                {
                    syntaxNodes.Add(field);
                }
            }

            return(new ExtractedField(
                       syntaxNodes,
                       context.Variables.Values.ToList(),
                       context.Fragments.Values.ToList()));
        }
Ejemplo n.º 11
0
 public Context(
     NameString schema,
     INamedOutputType typeContext,
     DocumentNode document,
     OperationDefinitionNode operation)
 {
     Schema       = schema;
     Variables    = new Dictionary <string, VariableDefinitionNode>();
     Document     = document;
     Operation    = operation;
     TypeContext  = typeContext;
     Fragments    = new Dictionary <string, FragmentDefinitionNode>();
     FragmentPath = ImmutableHashSet <string> .Empty;
 }
Ejemplo n.º 12
0
 private void ResolveFields(
     INamedOutputType type,
     ISelectionNode selection,
     Path path,
     IDictionary <string, FieldSelection> fields,
     ICollection <IFragmentNode> fragments)
 {
     if (selection is FieldNode fs && type is IComplexOutputType ct)
     {
         ResolveFieldSelection(
             ct,
             fs,
             path,
             fields);
     }
Ejemplo n.º 13
0
 private void CollectFields(
     INamedOutputType type,
     SelectionSetNode selectionSet,
     Path path,
     IDictionary <string, FieldSelection> fields,
     ICollection <IFragmentNode> fragments)
 {
     foreach (ISelectionNode selection in selectionSet.Selections)
     {
         ResolveFields(
             type,
             selection,
             path,
             fields,
             fragments);
     }
 }
Ejemplo n.º 14
0
        private Fragment CreateFragment(
            INamedOutputType parentType,
            InlineFragmentNode inlineFragment)
        {
            INamedType type;

            if (inlineFragment.TypeCondition == null)
            {
                type = parentType;
            }
            else
            {
                type = _schema.GetType <INamedType>(
                    inlineFragment.TypeCondition.Name.Value);
            }

            return(new Fragment(type.Name, FragmentKind.Inline, type, inlineFragment.SelectionSet));
        }
        public ExtractedField ExtractField(
            NameString sourceSchema,
            DocumentNode document,
            OperationDefinitionNode operation,
            FieldNode field,
            INamedOutputType declaringType)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (declaringType == null)
            {
                throw new ArgumentNullException(nameof(declaringType));
            }

            sourceSchema.EnsureNotEmpty(nameof(sourceSchema));

            var context = new Context(
                sourceSchema, declaringType,
                document, operation);

            FieldNode rewrittenField = RewriteField(field, context);

            return(new ExtractedField(
                       rewrittenField,
                       context.Variables.Values.ToList(),
                       context.Fragments.Values.ToList()));
        }
Ejemplo n.º 16
0
        public Fragment GetFragment(
            INamedOutputType parentType,
            InlineFragmentNode inlineFragment)
        {
            if (parentType == null)
            {
                throw new ArgumentNullException(nameof(parentType));
            }

            if (inlineFragment == null)
            {
                throw new ArgumentNullException(nameof(inlineFragment));
            }

            string fragmentName = CreateInlineFragmentName(inlineFragment);

            if (!_fragments.TryGetValue(fragmentName, out Fragment? fragment))
            {
                fragment = CreateFragment(parentType, inlineFragment);
                _fragments[fragmentName] = fragment;
            }

            return(fragment);
        }
Ejemplo n.º 17
0
 public PossibleSelections CollectFields(
     INamedOutputType type,
     SelectionSetNode selectionSet,
     Path path) =>
 _collector.CollectFields(type, selectionSet, path);
Ejemplo n.º 18
0
 public Context SetTypeContext(INamedOutputType type)
 {
     return(new Context(this, type));
 }
Ejemplo n.º 19
0
 public static Context New(
     INamedOutputType typeContext,
     IDictionary <string, FragmentDefinitionNode> fragments)
 {
     return(new Context(typeContext, fragments));
 }