Exemplo n.º 1
0
        public IGraphQlDocument Validate(Type queryType)
        {
            if (custom == null)
            {
                custom = new GraphQlCustomiseSchema();
            }

            try
            {
                schema = GraphQlSchemaLoader.GetSchema(queryType);

                if (schema == null)
                {
                    schema = GraphQlSchemaLoader.InitializeSchema(queryType, custom);
                }

                var z = new GraphQlMainValidation(this, schema);
            }
            catch (GraphQlException e)
            {
                validation_errors = true;
                output            = new GraphQlJObject();
                output.AddException(e);
            }
            catch (Exception e)
            {
                validation_errors = true;
                output            = new GraphQlJObject();
                output.AddException(new GraphQlException(e, 0, 0));
            }
            return(this);
        }
Exemplo n.º 2
0
        public static __SchemaContainer InitializeSchema(Type root, GraphQlCustomiseSchema custom)
        {
            if (cached.ContainsKey(root) == false)
            {
                var queryAttribute = root.GetProperties()
                                     .FirstOrDefault(x => x.GetCustomAttribute(typeof(GraphQlTopLevelQueryAttribute)) != null);

                if (queryAttribute == null)
                {
                    throw new Exception(
                              $"Top Level query - {root.Name} - must contain a public property with '[GRaphQlQuery]' as attribute - to indicate top level query ... if mutations are required then must include [GraphQlMutations] attribute on a different property");
                }

                var queryType = root.GetProperties().Where(x => x.GetCustomAttribute <GraphQlTopLevelQueryAttribute>() != null)
                                .Select(x => x.PropertyType).First();

                var mutationAttribute = root.GetProperties()
                                        .FirstOrDefault(x => x.GetCustomAttribute(typeof(GraphQlMutationsAttribute)) != null);

                Type mutationType = null;

                if (mutationAttribute == null)
                {
                    L.Trace($"[GraphQlMutation] attribute on {root.Name} not found ... continuing");
                }
                else
                {
                    mutationType = root.GetProperties()
                                   .Where(x => x.GetCustomAttribute <GraphQlMutationsAttribute>() != null)
                                   .Select(x => x.PropertyType).First();
                }


                var types = new Dictionary <Type, __Type>();

                var __queryType = new __Type(queryType, types, custom);

                __SchemaContainer schemaContainer = null;
                if (mutationType == null)
                {
                    schemaContainer = new __SchemaContainer(new __Schema(__queryType));
                }
                else
                {
                    var __mutationType = new __Type(mutationType, types, custom);
                    schemaContainer = new __SchemaContainer(new __Schema(__queryType, __mutationType));
                }

                var foo =
                    new[]
                {
                    typeof(__SchemaContainer)
                }.Select(x => new __Type(x, types, custom))
                .ToArray();
                schemaContainer.__schema.types = types.Values.Where(SchemaTypeFilter).ToList();
                cached[root] = schemaContainer;
            }
            return(cached[root]);
        }
Exemplo n.º 3
0
 public Object Run(IGraphQuery query, GraphQlCustomiseSchema schema = null)
 {
     return(new GraphQlDocument(query.Query)
            .CustomiseSchema(schema ?? new GraphQlCustomiseSchema())
            .Validate(topLevelType)
            .Run(ResolveTopLevelType(topLevelType), query.OperationName, query.Variables)
            .GetOutput());
 }
Exemplo n.º 4
0
        public __Type(Type t, Dictionary <Type, __Type> resolver, GraphQlCustomiseSchema customise, bool isInputType = false)
        {
            this.resolver        = resolver;
            this.customiseSchema = customise;
            dotNetType           = t;

            if (resolver.ContainsKey(t) == false)
            {
                resolver[t] = this;
            }

            if (TypeCheck.IsNumeric(t) || TypeCheck.IsString(t) || TypeCheck.IsDateTime(t) || TypeCheck.IsBoolean(t))
            {
                ScalarType();
            }
            else if (TypeCheck.IsEnum(t))
            {
                EnumType();
            }
            else if (TypeCheck.IsEnumerableType(t))
            {
                ListType();
            }
            else if (TypeCheck.IsClass(t) && isInputType)
            {
                InputObjectType();
            }
            else if (TypeCheck.IsClass(t))
            {
                ObjectOrInterfaceType(__TypeKind.OBJECT);
            }
            else if (t.IsInterface)
            {
                ObjectOrInterfaceType(__TypeKind.INTERFACE);
            }
            else if (TypeCheck.IsValueType(t))
            {
                throw new Exception($"Unexpected value type = {t.Name}");
            }
            else
            {
                throw new Exception($"Unexpected type = {t.Name}");
            }
        }
Exemplo n.º 5
0
 public IGraphQlDocument CustomiseSchema(GraphQlCustomiseSchema custom)
 {
     this.custom = custom;
     return(this);
 }