Esempio n. 1
0
        // tries to register resolvers for type bindings that at
        // this point have no explicite resolver.
        private static void TryRegisterMissingResolvers(
            ISchemaContext schemaContext)
        {
            foreach (ObjectTypeBinding typeBinding in schemaContext.Types
                     .GetTypeBindings().OfType <ObjectTypeBinding>())
            {
                List <FieldMember> missingResolvers = new List <FieldMember>();
                foreach (FieldBinding field in typeBinding.Fields.Values)
                {
                    FieldReference fieldReference = new FieldReference(
                        typeBinding.Name, field.Name);
                    if (!schemaContext.Resolvers.ContainsResolver(
                            fieldReference))
                    {
                        missingResolvers.Add(new FieldMember(
                                                 typeBinding.Name, field.Name, field.Member));
                    }
                }

                foreach (IFieldResolverDescriptor descriptor in
                         FieldResolverDiscoverer.CreateResolverDescriptors(
                             null, typeBinding.Type, missingResolvers))
                {
                    schemaContext.Resolvers.RegisterResolver(descriptor);
                }
            }
        }
        private List <IFieldResolverDescriptor> CollectPossibleDescriptors(
            ITypeRegistry typeRegistry,
            ResolverCollectionBindingInfo resolverBinding)
        {
            var descriptors = new List <IFieldResolverDescriptor>();

            // if implicit resolver discovery is on get all possible
            // resolver members from the resolver type.
            if (resolverBinding.Behavior == BindingBehavior.Implicit)
            {
                descriptors.AddRange(FieldResolverDiscoverer.DiscoverResolvers(
                                         resolverBinding.ResolverType,
                                         resolverBinding.ObjectType,
                                         resolverBinding.ObjectTypeName,
                                         m => LookupFieldName(typeRegistry, m)));
            }

            if (resolverBinding.Fields.Count > 0)
            {
                ILookup <string, IFieldResolverDescriptor> descriptorLookup =
                    descriptors.ToLookup(t => t.Field.FieldName);

                IEnumerable <FieldMember> selectedResolvers =
                    resolverBinding.Fields.Select(
                        t => new FieldMember(
                            resolverBinding.ObjectTypeName,
                            t.FieldName, t.ResolverMember)).ToArray();

                foreach (IFieldResolverDescriptor explicitDescriptor in
                         FieldResolverDiscoverer.CreateResolverDescriptors(
                             resolverBinding.ResolverType,
                             resolverBinding.ObjectType,
                             selectedResolvers))
                {
                    // remove implicit declared descriptos for the
                    // explcitly declared ones.
                    RemoveDescriptors(descriptors,
                                      descriptorLookup[explicitDescriptor.Field.FieldName]);

                    // add the explicitly declared descriptor
                    descriptors.Add(explicitDescriptor);
                }
            }

            return(descriptors);
        }