Exemplo n.º 1
0
        // tries to register resolvers for type bindings that at this point have no explicite resolver.
        private void TryRegisterMissingResolvers(
            ISchemaContext schemaContext)
        {
            FieldResolverDiscoverer discoverer = new FieldResolverDiscoverer();

            foreach (ObjectTypeBinding typeBinding in schemaContext.Types
                     .GetTypeBindings().OfType <ObjectTypeBinding>())
            {
                List <FieldResolverMember> missingResolvers = new List <FieldResolverMember>();
                foreach (FieldBinding field in typeBinding.Fields.Values)
                {
                    FieldReference fieldReference = new FieldReference(
                        typeBinding.Name, field.Name);
                    if (!schemaContext.Resolvers.ContainsResolver(fieldReference))
                    {
                        missingResolvers.Add(new FieldResolverMember(
                                                 typeBinding.Name, field.Name, field.Member));
                    }
                }

                foreach (FieldResolverDescriptor descriptor in discoverer
                         .GetSelectedResolvers(typeBinding.Type, typeBinding.Type,
                                               missingResolvers))
                {
                    schemaContext.Resolvers.RegisterResolver(descriptor);
                }
            }
        }
Exemplo n.º 2
0
        private List <FieldResolverDescriptor> CollectPossibleDescriptors(
            ITypeRegistry typeRegistry,
            ResolverCollectionBindingInfo resolverBinding)
        {
            List <FieldResolverDescriptor> descriptors =
                new List <FieldResolverDescriptor>();

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

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

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

                foreach (FieldResolverDescriptor explicitDescriptor in
                         _fieldResolverDiscoverer.GetSelectedResolvers(
                             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);
        }