Example #1
0
        public GraphQLSchema BuildSchema(IEnumerable <ISchemaEntity> schemas)
        {
            // Do not add schema without fields.
            allSchemas.AddRange(SchemaInfo.Build(schemas).Where(x => x.Fields.Count > 0));

            // Only published normal schemas (not components are used for entities).
            var schemaInfos = allSchemas.Where(x => x.Schema.SchemaDef.IsPublished && x.Schema.SchemaDef.Type != SchemaType.Component).ToList();

            foreach (var schemaInfo in schemaInfos)
            {
                var contentType = new ContentGraphType(schemaInfo);

                contentTypes[schemaInfo]       = contentType;
                contentResultTypes[schemaInfo] = new ContentResultGraphType(contentType, schemaInfo);
            }

            foreach (var schemaInfo in allSchemas)
            {
                var componentType = new ComponentGraphType(schemaInfo);

                componentTypes[schemaInfo] = componentType;
            }

            var newSchema = new GraphQLSchema
            {
                Query = new AppQueriesGraphType(this, schemaInfos)
            };

            newSchema.RegisterType(SharedTypes.ComponentInterface);
            newSchema.RegisterType(SharedTypes.ContentInterface);

            if (schemaInfos.Any())
            {
                var mutations = new AppMutationsGraphType(this, schemaInfos);

                if (mutations.Fields.Count > 0)
                {
                    newSchema.Mutation = mutations;
                }
            }

            foreach (var(schemaInfo, contentType) in contentTypes)
            {
                contentType.Initialize(this, schemaInfo, schemaInfos);
            }

            foreach (var(schemaInfo, componentType) in componentTypes)
            {
                componentType.Initialize(this, schemaInfo);
            }

            foreach (var contentType in contentTypes.Values)
            {
                newSchema.RegisterType(contentType);
            }

            newSchema.Initialize();

            return(newSchema);
        }
Example #2
0
        public GraphQLModel(IAppEntity app,
                            IEnumerable <ISchemaEntity> schemas,
                            int pageSizeContents,
                            int pageSizeAssets,
                            IUrlGenerator urlGenerator, ISemanticLog log)
        {
            this.log = log;

            partitionResolver = app.PartitionResolver();

            CanGenerateAssetSourceUrl = urlGenerator.CanGenerateAssetSourceUrl;

            assetType     = new AssetGraphType(this);
            assetListType = new ListGraphType(new NonNullGraphType(assetType));

            graphQLTypeVisitor = new GraphQLTypeVisitor(contentTypes, this, assetListType);

            var allSchemas = schemas.Where(x => x.SchemaDef.IsPublished).ToList();

            BuildSchemas(allSchemas);

            graphQLSchema = BuildSchema(this, pageSizeContents, pageSizeAssets, allSchemas);
            graphQLSchema.RegisterValueConverter(JsonConverter.Instance);
            graphQLSchema.RegisterValueConverter(InstantConverter.Instance);

            InitializeContentTypes(allSchemas, pageSizeContents);
        }
Example #3
0
        private static GraphQLSchema BuildSchema(GraphQLModel model, int pageSizeContents, int pageSizeAssets, List <ISchemaEntity> schemas)
        {
            var schema = new GraphQLSchema
            {
                Query = new AppQueriesGraphType(model, pageSizeContents, pageSizeAssets, schemas)
            };

            return(schema);
        }
        public async Task ExecutingThenDisposing_DoesNotThrowException()
        {
            var executer = new DocumentExecuter();
            var schema = new Schema();

            await executer.ExecuteAsync(schema, null, "{noop}", null).ConfigureAwait(false);

            Should.NotThrow(() => schema.Dispose());
        }
 public async Task<ExecutionResult> Execute(
   Schema schema,
   object rootObject,
   string query,
   string operationName = null,
   Inputs inputs = null)
 {
     var executer = new DocumentExecuter();
     return await executer.ExecuteAsync(schema, rootObject, query, operationName);
 }
 public ExecutionResult Execute(
   Schema schema,
   object rootObject,
   string query,
   string operationName = null,
   Inputs inputs = null)
 {
     var executer = new DocumentExecuter();
     return executer.Execute(schema, rootObject, query, operationName);
 }
        public ValidationResult IsValid(Schema schema, Document document, string operationName)
        {
            var result = new ValidationResult();

            if (string.IsNullOrWhiteSpace(operationName)
                && document.Operations.Count() > 1)
            {
                result.Errors.Add(new ExecutionError("Must provide operation name if query contains multiple operations"));
            }

            return result;
        }
        /// <summary>
        ///     Create schema from the field definitions.
        /// </summary>
        public GraphQL.Types.Schema CreateSchema(
            IEnumerable <FieldDefinition> definitions)
        {
            var mutation = new ObjectGraphType
            {
                Name = "RootMutations"
            };
            var query = new ObjectGraphType
            {
                Name = "RootQueries"
            };

            foreach (var definition in definitions)
            {
                if (definition.Field == null)
                {
                    continue;
                }

                var type = EnsureGraphType(definition.Field.Response);

                if (definition.Field.IsMutation)
                {
                    mutation.FieldAsync(
                        type,
                        definition.Field.Name,
                        TypeHelper.GetDescription(definition.Field.Method),
                        definition.Field.Arguments,
                        definition.Resolve,
                        definition.Field.ObsoleteReason);
                }
                else
                {
                    query.FieldAsync(
                        type,
                        definition.Field.Name,
                        TypeHelper.GetDescription(definition.Field.Method),
                        definition.Field.Arguments,
                        definition.Resolve,
                        definition.Field.ObsoleteReason);
                }
            }

            var schema = new GraphQL.Types.Schema(TypeResolver)
            {
                Mutation = mutation.Fields.Any() ? mutation : null,
                Query    = query.Fields.Any() ? query : null
            };

            return(schema);
        }
Example #9
0
        public GraphQLModel(IAppEntity app, IEnumerable <ISchemaEntity> schemas, IGraphQLUrlGenerator urlGenerator)
        {
            this.app = app;

            partitionResolver = app.PartitionResolver();

            CanGenerateAssetSourceUrl = urlGenerator.CanGenerateAssetSourceUrl;

            assetType   = new AssetGraphType(this);
            schemasById = schemas.ToDictionary(x => x.Id);
            schemaTypes = new QueryGraphTypeVisitor(GetContentType, new ListGraphType(new NonNullGraphType(assetType)));

            graphQLSchema = BuildSchema(this);

            InitializeContentTypes();
        }
Example #10
0
        public GraphQLModel(IAppEntity app, IEnumerable <ISchemaEntity> schemas, IGraphQLUrlGenerator urlGenerator)
        {
            this.app = app;

            partitionResolver = app.PartitionResolver();

            CanGenerateAssetSourceUrl = urlGenerator.CanGenerateAssetSourceUrl;

            assetType     = new AssetGraphType(this);
            assetListType = new ListGraphType(new NonNullGraphType(assetType));

            schemasById = schemas.ToDictionary(x => x.Id);

            graphQLSchema = BuildSchema(this);
            graphQLSchema.RegisterValueConverter(JsonConverter.Instance);

            InitializeContentTypes();
        }
Example #11
0
        private static GraphQLSchema BuildSchema(GraphQLModel model, List <ISchemaEntity> schemas)
        {
            var schema = new GraphQLSchema
            {
                Query = new AppQueriesGraphType(model, schemas)
            };

            schema.RegisterType(ContentInterfaceGraphType.Instance);

            var schemasWithFields = schemas.Where(x => x.SchemaDef.Fields.Count > 0);

            if (schemasWithFields.Any())
            {
                schema.Mutation = new AppMutationsGraphType(model, schemasWithFields);
            }

            return(schema);
        }
Example #12
0
        public static void CleanupMetadata(this GraphQLSchema schema)
        {
            var targets = new HashSet <IProvideMetadata>(ReferenceEqualityComparer.Instance);

            foreach (var type in schema.AllTypes)
            {
                FindTargets(type, targets);
            }

            foreach (var target in targets.OfType <MetadataProvider>())
            {
                var metadata = target.Metadata;

                if (metadata != null && metadata.Count == 0)
                {
                    target.Metadata = null;
                }
            }
        }
Example #13
0
        public GraphQLNetSchema Convert(IGraphSchema graphSchema)
        {
            var schema = new GraphQLNetSchema();

            if (graphSchema.Query.Fields.Any())
            {
                schema.Query = CreateSchema(OperationType.Query, graphSchema.Query);
                schema.RegisterType(schema.Query);
            }
            if (graphSchema.Mutation.Fields.Any())
            {
                schema.Mutation = CreateSchema(OperationType.Mutation, graphSchema.Mutation);
            }
            if (graphSchema.Subscription.Fields.Any())
            {
                schema.Subscription = CreateSchema(OperationType.Subscription, graphSchema.Subscription);
            }
            return(schema);
        }
Example #14
0
        public GraphQLSchema BuildSchema(IEnumerable <ISchemaEntity> schemas)
        {
            var schemaInfos = SchemaInfo.Build(schemas).ToList();

            foreach (var schemaInfo in schemaInfos)
            {
                var contentType = new ContentGraphType(this, schemaInfo);

                contentTypes[schemaInfo]       = contentType;
                contentResultTypes[schemaInfo] = new ContentResultGraphType(contentType, schemaInfo);
            }

            var newSchema = new GraphQLSchema
            {
                Query = new AppQueriesGraphType(this, schemaInfos)
            };

            newSchema.RegisterValueConverter(JsonConverter.Instance);
            newSchema.RegisterValueConverter(InstantConverter.Instance);

            newSchema.RegisterType(sharedTypes.ContentInterface);

            if (schemas.Any())
            {
                newSchema.Mutation = new AppMutationsGraphType(this, schemaInfos);
            }

            foreach (var(schemaInfo, contentType) in contentTypes)
            {
                contentType.Initialize(this, schemaInfo, schemaInfos);
            }

            foreach (var contentType in contentTypes.Values)
            {
                newSchema.RegisterType(contentType);
            }

            newSchema.Initialize();
            newSchema.CleanupMetadata();

            return(newSchema);
        }
Example #15
0
        private static GraphQLSchema BuildSchema(GraphQLModel model, int pageSizeContents, int pageSizeAssets, List <ISchemaEntity> schemas)
        {
            var schema = new GraphQLSchema
            {
                Query = new AppQueriesGraphType(
                    model,
                    pageSizeContents,
                    pageSizeAssets,
                    schemas)
            };

            var schemasWithFields = schemas.Where(x => x.SchemaDef.Fields.Count > 0);

            if (schemasWithFields.Any())
            {
                schema.Mutation = new AppMutationsGraphType(model, schemasWithFields);
            }

            return(schema);
        }
Example #16
0
        public ISchema BuildSchema(Type schemaType)
        {
            var schema = new GraphQL.Types.Schema();

            var queryProperty = schemaType.GetProperty("Query");

            if (queryProperty != null)
            {
                var graphType = TypeHelper.GetGraphType(queryProperty);
                schema.Query = Activator.CreateInstance(_objectWrapper.MakeGenericType(graphType)) as ObjectGraphType;
            }

            var mutationProperty = schemaType.GetProperty("Mutation");

            if (mutationProperty != null)
            {
                var graphType = TypeHelper.GetGraphType(queryProperty);
                schema.Mutation = Activator.CreateInstance(_objectWrapper.MakeGenericType(graphType)) as ObjectGraphType;
            }

            return(schema);
        }
Example #17
0
        public GraphQLModel(IAppEntity app, IEnumerable <ISchemaEntity> schemas, GraphQLTypeFactory typeFactory, ISemanticLog log)
        {
            graphQLTypeFactory = typeFactory;

            this.log = log;

            partitionResolver = app.PartitionResolver();

            typeVisitor = new GraphQLTypeVisitor(contentTypes, this);

            var allSchemas = schemas.Where(x => x.SchemaDef.IsPublished).ToList();

            BuildSchemas(allSchemas);

            graphQLSchema = BuildSchema(this, allSchemas);
            graphQLSchema.RegisterValueConverter(JsonConverter.Instance);
            graphQLSchema.RegisterValueConverter(InstantConverter.Instance);

            InitializeContentTypes(allSchemas);

            partitionResolver = null !;

            typeVisitor = null !;
        }
Example #18
0
        public ISchema Create(IModel model)
        {
            var schema = new GraphQL.Types.Schema(_graphTypeResolverSource.Resolve);
            var query  = schema.Query = new ObjectGraphType();

            foreach (var entityType in model.GetEntityTypes())
            {
                var fieldType
                    = AddEntityCollectionField(
                          query,
                          entityType,
                          _resolveFactory.CreateResolveEntityList(entityType));

                var entityCountFieldResolver = _resolveFactory.CreateResolveEntityCount(entityType);

                query.Field <IntGraphType>(
                    $"{fieldType.Name}Count",
                    $"Gets the total number of {fieldType.Name}.",
                    arguments: entityCountFieldResolver.Arguments,
                    resolve: entityCountFieldResolver.Resolve);
            }

            return(schema);
        }
 public GraphQLController()
 {
     _schema = new StarWarsSchema();
 }
        public void DisposingRightAway_DoesNotThrowException()
        {
            var schema = new Schema();

            Should.NotThrow(() => schema.Dispose());
        }
Example #21
0
 public void throw_error_on_null_with_register_types()
 {
     var schema = new Schema();
     Expect.Throws<ArgumentNullException>(() => schema.RegisterTypes(null));
 }
Example #22
0
 public void throw_error_on_non_graphtype_with_register_types()
 {
     var schema = new Schema();
     Expect.Throws<ArgumentOutOfRangeException>(() => schema.RegisterTypes(typeof(MyDto)));
 }
Example #23
0
 public void DoesNotContainTypeNames(Schema schema, params string[] typeNames)
 {
     typeNames.Apply(typeName =>
     {
         var type = schema.AllTypes.SingleOrDefault(x => x.Name == typeName);
         type.ShouldEqual(null, "Found {0} in type lookup.".ToFormat(typeName));
     });
 }
        public void build_dynamic_schema()
        {
            var schema = new Schema();

            var person = new ObjectGraphType();
            person.Name = "Person";
            person.Field("name", new StringGraphType());
            person.Field(
                "friends",
                new ListGraphType(new NonNullGraphType(person)),
                resolve: ctx => new[] {new SomeObject {Name = "Jaime"}, new SomeObject {Name = "Joe"}});

            var root = new ObjectGraphType();
            root.Name = "Root";
            root.Field("hero", person, resolve: ctx => ctx.RootValue);

            schema.Query = root;
            schema.RegisterTypes(person);

            var printed = new SchemaPrinter(schema).Print();

            #if DEBUG
            Console.WriteLine(printed);
            #endif

            AssertQuerySuccess(
                schema,
                @"{ hero { name friends { name } } }",
                @"{ hero: { name : 'Quinn', friends: [ { name: 'Jaime' }, { name: 'Joe' }] } }",
                root: new SomeObject { Name = "Quinn"});
        }
 public void throw_error_on_null_with_register_types()
 {
     var schema = new Schema();
     Type[] types = null;
     Should.Throw<ArgumentNullException>(() => schema.RegisterTypes(types));
 }
        private static IObjectGraphType?FindDataType(GraphQLSchema graphQLSchema, string schema)
        {
            var type = (IObjectGraphType)graphQLSchema.AllTypes.Single(x => x.Name == schema);

            return((IObjectGraphType?)type.GetField("flatData")?.ResolvedType?.Flatten());
        }
Example #27
0
 public void ContainsTypeNames(Schema schema, params string[] typeNames)
 {
     typeNames.Apply(typeName =>
     {
         var type = schema.FindType(typeName);
         type.ShouldNotBeNull("Did not find {0} in type lookup.".ToFormat(typeName));
     });
 }