示例#1
0
 public FieldContext(RequestContext requestContext, OperationFieldExecuter fieldExecuter, MappedSelectionField mappedField,
                     IList <OutputObjectScope> allParentScopes = null)
 {
     _requestContext = requestContext;
     _executer       = fieldExecuter;
     MappedField     = mappedField;
     FieldDef        = MappedField.Resolver.Field;
     TypeDef         = FieldDef.TypeRef.TypeDef;
     AllParentScopes = allParentScopes ?? OutputObjectScope.EmptyList;
     if (MappedField.Field.SelectionSubset != null)
     {
         AllResultScopes = new List <OutputObjectScope>();
     }
 }
示例#2
0
        // Might be called for ObjectType or Interface (for intf - just to check fields exist)
        private void MapObjectSelectionSubset(SelectionSubset selSubset, ObjectTypeDef objectTypeDef, bool isForUnion = false)
        {
            // Map arguments on fields, add directives, map fragments
            foreach (var item in selSubset.Items)
            {
                AddRuntimeRequestDirectives(item);
                switch (item)
                {
                case SelectionField selFld:
                    var fldDef = objectTypeDef.Fields[selFld.Name];
                    if (fldDef == null)
                    {
                        // if field not found, the behavior depends if it is a union; it is error for a union
                        if (!isForUnion)
                        {
                            AddError($"Field '{selFld.Name}' not found on type '{objectTypeDef.Name}'.", selFld);
                        }
                        continue;
                    }
                    selFld.MappedArgs = MapArguments(selFld.Args, fldDef.Args, selFld);
                    AddRuntimeModelDirectives(fldDef);
                    MapSelectionFieldSubsetIfPresent(selFld, fldDef.TypeRef.TypeDef);
                    break;

                case FragmentSpread fspread:
                    // Named fragment refs are NOT set by parser; parser sets only Inline fragmDefs; we need to match named fragms here
                    if (!fspread.IsInline && fspread.Fragment == null)
                    {
                        fspread.Fragment = GetFragmentDef(fspread.Name);
                        if (fspread.Fragment == null)
                        {
                            AddError($"Fragment {fspread.Name} not defined.", fspread);
                        }
                    }
                    break;
                } //switch
            }     //foreach item

            if (_requestContext.Failed)
            {
                return;
            }

            // Now create mappings for all possible entity types
            foreach (var typeMapping in objectTypeDef.Mappings)
            {
                // It is possible mapped set already exists (with unions and especially fragments)
                var existing = selSubset.MappedSubSets.FirstOrDefault(ms => ms.Mapping == typeMapping);
                if (existing != null)
                {
                    continue;
                }
                var mappedItems = new List <MappedSelectionItem>();
                foreach (var item in selSubset.Items)
                {
                    switch (item)
                    {
                    case SelectionField selFld:
                        var fldDef = typeMapping.TypeDef.Fields[selFld.Name];
                        if (fldDef == null)
                        {
                            // it is not error, it should have been caught earlier; it is unmatch for union
                            continue;
                        }
                        var fldResolver = typeMapping.GetResolver(fldDef);
                        //.FirstOrDefault(fr => fr.Field.Name == selFld.Name);
                        var mappedFld = new MappedSelectionField(selFld, fldResolver);
                        mappedItems.Add(mappedFld);
                        break;

                    case FragmentSpread fs:
                        var onType = fs.Fragment.OnTypeRef?.TypeDef;
                        var skip   = onType != null && onType.Kind == TypeKind.Object && onType != objectTypeDef;
                        if (skip)
                        {
                            continue;
                        }
                        if (fs.IsInline) // only inline fragments should be mapped from here; named fragments are mapped separately, upfront
                        {
                            MapObjectSelectionSubset(fs.Fragment.SelectionSubset, objectTypeDef, isForUnion);
                        }
                        var mappedSpread = new MappedFragmentSpread(fs);
                        mappedItems.Add(mappedSpread);
                        break;
                    } //switch
                }     //foreach item

                selSubset.MappedSubSets.Add(new MappedSelectionSubSet()
                {
                    Mapping = typeMapping, MappedItems = mappedItems
                });
            } //foreach typeMapping
        }
 public OperationFieldExecuter(RequestContext requestContext, MappedSelectionField mappedOpField, OutputObjectScope parentScope)
 {
     _requestContext = requestContext;
     _parentScope    = parentScope;
     _mappedOpField  = mappedOpField;
 }