Exemple #1
0
        public GraphSchemaInfo GetSchema(TypeInfo typeInfo)
        {
            var queryField        = typeInfo.GetProperty("Query");
            var mutationField     = typeInfo.GetProperty("Mutation");
            var subscriptionField = typeInfo.GetProperty("Subscription");

            if (queryField == null)
            {
                throw new ArgumentException("Schema has no query type.");
            }

            var schemaInfo = new GraphSchemaInfo();

            _typeResolver.ActiveSchema = schemaInfo;
            schemaInfo.Query           = GetType(queryField.PropertyType.GetTypeInfo());
            schemaInfo.Mutation        = mutationField != null
                ? GetType(mutationField.PropertyType.GetTypeInfo())
                : null;

            schemaInfo.Subscription = subscriptionField != null
                ? GetType(subscriptionField.PropertyType.GetTypeInfo())
                : null;

            schemaInfo.Types.AddRange(_typeCache.Entities.Where(t => IsValidType(t.GetTypeRepresentation())));

            return(schemaInfo);
        }
        public ISchema DeriveSchema(GraphSchemaInfo schemaInfo)
        {
            var types = schemaInfo
                        .Types
                        .Where(t => !t.IsIgnored && !t.Interfaces.Any(iface => iface.IsIgnored))
                        .ToList();

            foreach (var type in types)
            {
                DeriveType(type);
            }
            var interfaces = types
                             .Where(t => t.IsInterfaceType && !t.IsInputType && t.PossibleTypes.Any())
                             .GroupBy(t => t.Name)
                             .Select(g => g.First());
            var myPossibleTypes = interfaces
                                  .Where(t => !t.IsIgnored)
                                  .SelectMany(t => t.PossibleTypes)
                                  .Select(x => OmitNonRootTypes(x))
                                  .Where(x => x != null)
                                  .ToArray();
            var schema = new Schema(new FuncDependencyResolver(DeriveTypeFromTypeInfo))
            {
                Query        = DeriveOperationType(schemaInfo.Query),
                Mutation     = DeriveOperationType(schemaInfo.Mutation),
                Subscription = DeriveOperationType(schemaInfo.Subscription),
            };

            // Instead of using Type[] we are now using IGraphType[] for the schema registration
            schema.RegisterTypes(myPossibleTypes);
            return(schema);
        }
        public ISchema DeriveSchema(GraphSchemaInfo schemaInfo)
        {
            var types = schemaInfo
                        .Types
                        .Where(t => !t.IsIgnored && !t.Interfaces.Any(iface => iface.IsIgnored))
                        .ToList();

            foreach (var type in types)
            {
                DeriveType(type);
            }
            var interfaces = types
                             .Where(t => t.IsInterfaceType && !t.IsInputType && t.PossibleTypes.Any())
                             .GroupBy(t => t.Name)
                             .Select(g => g.First());
            var possibleTypes = interfaces
                                .Where(t => !t.IsIgnored)
                                .SelectMany(t => t.PossibleTypes)
                                .GroupBy(t => t.Name)
                                .Select(g => g.First());
            var schema = new Schema(new FuncDependencyResolver(DeriveTypeFromTypeInfo))
            {
                Query        = DeriveOperationType(schemaInfo.Query),
                Mutation     = DeriveOperationType(schemaInfo.Mutation),
                Subscription = DeriveOperationType(schemaInfo.Subscription),
            };

            schema.RegisterTypes(possibleTypes
                                 .Where(t => !t.IsIgnored && !t.Interfaces.Any(i => i.IsIgnored))
                                 .Select(t => DeriveType(t).GetType())
                                 .ToArray());
            return(schema);
        }
 private void UpdateTypeResolutionDelegate(
     GraphSchemaInfo schema,
     IEnumerable <GraphFieldInfo> fields)
 {
     foreach (var field in fields)
     {
         field.SchemaInfo = schema;
     }
 }
Exemple #5
0
        public ISchema DeriveSchema(GraphSchemaInfo schemaInfo)
        {
            var types = schemaInfo
                        .Types
                        .Where(t => !t.IsIgnored && !t.Interfaces.Any(iface => iface.IsIgnored))
                        .ToList();

            foreach (var type in types)
            {
                DeriveType(type);
            }
            var interfaces = types
                             .Where(t => t.IsInterfaceType && !t.IsInputType && t.PossibleTypes.Any())
                             .GroupBy(t => t.Name)
                             .Select(g => g.First());
            var possibleTypes = interfaces
                                .Where(t => !t.IsIgnored)
                                .SelectMany(t => t.PossibleTypes)
                                .Select(typeInfo => (typeInfo.IsIgnored || !typeInfo.IsNullable || typeInfo.Interfaces.Any(x => x.IsIgnored))
                    ? null
                    : DeriveType(typeInfo)
                                        )
                                .Where(x => x != null)
                                .GroupBy(t => t.Name)
                                .Select(g => g.First())
                                .ToArray();
            var schema = new Schema(new FuncServiceProvider(DeriveTypeFromTypeInfo))
            {
                Query        = DeriveOperationType(schemaInfo.Query),
                Mutation     = DeriveOperationType(schemaInfo.Mutation),
                Subscription = DeriveOperationType(schemaInfo.Subscription),
            };

            schema.RegisterTypes(possibleTypes);

            return(schema);
        }
        public TSchemaType Build(params TypeInfo[] schemaTypes)
        {
            var schemaInfos = schemaTypes?
                              .Select(_typeResolver.DeriveSchema)
                              .ToList() ?? new List <GraphSchemaInfo>();

            var schemaInfo = schemaInfos.FirstOrDefault();

            if (schemaInfo == null)
            {
                return(null);
            }
            else if (schemaInfos.Count == 1)
            {
                schemaInfo.TypeResolutionDelegate = TypeResolutionDelegate;
                return(_graphTypeAdapter.DeriveSchema(schemaInfo));
            }

            schemaInfo = new GraphSchemaInfo
            {
                Query                  = _typeResolver.DeriveType(typeof(Query).GetTypeInfo()),
                Mutation               = _typeResolver.DeriveType(typeof(Mutation).GetTypeInfo()),
                Subscription           = _typeResolver.DeriveType(typeof(Subscription).GetTypeInfo()),
                TypeResolutionDelegate = TypeResolutionDelegate,
            };

            schemaInfo.Query.Name        = nameof(Query);
            schemaInfo.Mutation.Name     = nameof(Mutation);
            schemaInfo.Subscription.Name = nameof(Subscription);

            foreach (var additionalSchemaInfo in schemaInfos)
            {
                additionalSchemaInfo.TypeResolutionDelegate = TypeResolutionDelegate;

                schemaInfo.Query.Fields.AddRange(RemoveDuplicateViewers(
                                                     additionalSchemaInfo.Query?.Fields,
                                                     schemaInfo.Query.Fields));
                schemaInfo.Query.Fields = schemaInfo.Query.Fields.OrderBy(f => f.Name).ToList();
                UpdateTypeResolutionDelegate(schemaInfo, schemaInfo.Query.Fields);

                schemaInfo.Mutation.Fields.AddRange(RemoveDuplicateViewers(
                                                        additionalSchemaInfo.Mutation?.Fields,
                                                        schemaInfo.Mutation.Fields));
                schemaInfo.Mutation.Fields = schemaInfo.Mutation.Fields.OrderBy(f => f.Name).ToList();
                UpdateTypeResolutionDelegate(schemaInfo, schemaInfo.Mutation.Fields);

                schemaInfo.Subscription.Fields.AddRange(RemoveDuplicateViewers(
                                                            additionalSchemaInfo.Subscription?.Fields,
                                                            schemaInfo.Subscription.Fields));
                schemaInfo.Subscription.Fields = schemaInfo.Subscription.Fields.OrderBy(f => f.Name).ToList();
                UpdateTypeResolutionDelegate(schemaInfo, schemaInfo.Subscription.Fields);

                schemaInfo.Types.AddRange(additionalSchemaInfo.Types.Except(schemaInfo.Types));
            }

            if (!schemaInfo.Query.Fields.Any())
            {
                schemaInfo.Query = null;
            }
            if (!schemaInfo.Mutation.Fields.Any())
            {
                schemaInfo.Mutation = null;
            }
            if (!schemaInfo.Subscription.Fields.Any())
            {
                schemaInfo.Subscription = null;
            }

            return(_graphTypeAdapter.DeriveSchema(schemaInfo));
        }
        protected ISchema Schema(GraphSchemaInfo schemaInfo)
        {
            var graphTypeAdapter = new GraphTypeAdapter();

            return(graphTypeAdapter.DeriveSchema(schemaInfo));
        }