Beispiel #1
0
        static void Main(string[] args)
        {
            var configFile = args.FirstOrDefault() ?? "generator.json";
            var config     = JsonConvert.DeserializeObject <GenConfig>(File.ReadAllText(configFile, Encoding.UTF8));

            var schemaString = File.ReadAllText(config.Schema, Encoding.UTF8);

            // remove """ style comments
            schemaString = Regex.Replace(schemaString, "\"\"\"(.|(\\r ?\\n))*?\"\"\"", "", RegexOptions.Multiline);

            var document = new GraphQLDocumentBuilder().Build(File.ReadAllText(config.GraphQLFile, Encoding.UTF8));
            var schema   = new SchemaBuilder().Build(schemaString);

            var context = new GenContext
            {
                Schema   = schema,
                Document = document,
                Config   = config
            };

            BuildModel.Run(context);

            CSharpTransformer.Apply(context);
            var text = CSharpOutput.RenderSingleFile(context);

            File.WriteAllText(config.OutputFile, text, Encoding.UTF8);

            Console.WriteLine("Output file {0}", config.OutputFile);
        }
Beispiel #2
0
        private static IResolveFieldContext CreateResolveFieldContext(ISchema schema, string query)
        {
            var documentBuilder = new GraphQLDocumentBuilder();
            var document        = documentBuilder.Build(query);

            schema.Initialize();

            var executionContext = new ExecutionContext
            {
                Document  = document,
                Schema    = schema,
                Fragments = document.Fragments,
                Operation = document.Operations.First()
            };

            var root = new RootExecutionNode(schema.Query)
            {
                Result = executionContext.RootValue
            };

            var fields = ExecutionHelper.CollectFields(executionContext, schema.Query, executionContext.Operation.SelectionSet);

            ExecutionStrategy.SetSubFieldNodes(executionContext, root, fields);

            var subNode = root.SubFields.FirstOrDefault();

            return(new ReadonlyResolveFieldContext(subNode.Value, executionContext));
        }
Beispiel #3
0
        private IValidationResult Validate(string query, ISchema schema, IEnumerable <IValidationRule> rules)
        {
            var documentBuilder = new GraphQLDocumentBuilder();
            var document        = documentBuilder.Build(query);
            var validator       = new DocumentValidator();

            return(validator.ValidateAsync(query, schema, document, rules).Result);
        }
Beispiel #4
0
        private IValidationResult Validate(string query, ISchema schema, IEnumerable <IValidationRule> rules, Inputs inputs)
        {
            var documentBuilder = new GraphQLDocumentBuilder();
            var document        = documentBuilder.Build(query);
            var validator       = new DocumentValidator();

            return(validator.ValidateAsync(schema, document, document.Operations.FirstOrDefault()?.Variables, rules, inputs: inputs).Result.validationResult);
        }
Beispiel #5
0
        private IValidationResult Validate(ValidationTestConfig config)
        {
            HttpContext.User = config.User;
            var documentBuilder = new GraphQLDocumentBuilder();
            var document        = documentBuilder.Build(config.Query);
            var validator       = new DocumentValidator();

            return(validator.ValidateAsync(config.Schema, document, document.Operations.First().Variables, config.Rules, null, config.Inputs).GetAwaiter().GetResult().validationResult);
        }
Beispiel #6
0
        private IValidationResult Validate(ValidationTestConfig config)
        {
            var userContext = new GraphQLUserContext {
                User = config.User
            };
            var documentBuilder = new GraphQLDocumentBuilder();
            var document        = documentBuilder.Build(config.Query);
            var validator       = new DocumentValidator();

            return(validator.Validate(config.Query, config.Schema, document, config.Rules, userContext, config.Inputs));
        }
Beispiel #7
0
        public IHaveSelectionSet BuildGraphQLSelection(string body)
        {
            var document = new GraphQLDocumentBuilder().Build(body);

            return(document
                   .Operations
                   .OfType <IHaveSelectionSet>()
                   .First()?
                   .SelectionSet
                   .Selections
                   .OfType <Field>()
                   .FirstOrDefault());
        }
Beispiel #8
0
    private IValidationResult Validate(string query, ISchema schema, IEnumerable <IValidationRule> rules, Inputs variables)
    {
        var documentBuilder = new GraphQLDocumentBuilder();
        var document        = documentBuilder.Build(query);
        var validator       = new DocumentValidator();

        return(validator.ValidateAsync(new ValidationOptions
        {
            Schema = schema,
            Document = document,
            Rules = rules,
            Operation = document.Definitions.OfType <GraphQLOperationDefinition>().FirstOrDefault(),
            Variables = variables
        }).Result.validationResult);
    }
    private IValidationResult Validate(ValidationTestConfig config)
    {
        HttpContext.User = config.User;
        var documentBuilder = new GraphQLDocumentBuilder();
        var document        = documentBuilder.Build(config.Query);
        var validator       = new DocumentValidator();

        return(validator.ValidateAsync(new ValidationOptions
        {
            Schema = config.Schema,
            Document = document,
            Operation = document.Definitions.OfType <GraphQLOperationDefinition>().First(),
            Rules = config.Rules,
            Variables = config.Variables
        }).GetAwaiter().GetResult().validationResult);
    }
Beispiel #10
0
    async Task <List <Company> > GetCompanies(
        ResolveEventStreamContext context,
        MyDataContext ctx,
        long lastId,
        int take = 1,
        CancellationToken token = default)
    {
        var returnType = ctx.Companies;

        var document = new GraphQLDocumentBuilder().Build(SupposePersistedQuery());

        var fieldContext = ResolveFieldContext(ctx, token, document, context.Schema);

        var withArguments = returnType.ApplyGraphQlArguments(fieldContext);

        var greaterThanLastIdAndPaged = withArguments
                                        .Where(transaction => transaction.Id > lastId)
                                        .Take(take);

        return(await greaterThanLastIdAndPaged.ToListAsync(token));
    }
Beispiel #11
0
    public void GlobalSetup()
    {
        var services = new ServiceCollection();

        services.AddSingleton <StarWarsData>();
        services.AddSingleton <StarWarsQuery>();
        services.AddSingleton <StarWarsMutation>();
        services.AddSingleton <HumanType>();
        services.AddSingleton <HumanInputType>();
        services.AddSingleton <DroidType>();
        services.AddSingleton <CharacterInterface>();
        services.AddSingleton <EpisodeEnum>();
        services.AddSingleton <ISchema, StarWarsSchema>();

        _provider = services.BuildServiceProvider();
        _schema   = _provider.GetRequiredService <ISchema>();
        _schema.Initialize();
        _validator = new DocumentValidator();

        _introspectionDocument = new GraphQLDocumentBuilder().Build(Queries.Introspection);
        _fragmentsDocument     = new GraphQLDocumentBuilder().Build(Queries.Fragments);
        _heroDocument          = new GraphQLDocumentBuilder().Build(Queries.Hero);
    }
Beispiel #12
0
//        [Fact]
        public void core_builder()
        {
            var builder = new GraphQLDocumentBuilder();

            buildMany(builder);
        }
        public static GraphQLResult HandleQuery(string query, IDataStore datastore, string idFieldName)
        {
            Document d;

            try
            {
                IDocumentBuilder doc = new GraphQLDocumentBuilder();
                d = doc.Build(query);
            }
            catch (GraphQLSyntaxErrorException e)
            {
                return(new GraphQLResult {
                    Errors = new List <string> {
                        e.Message
                    }
                });
            }

            var operation = GetOperation("", d);

            if (operation.OperationType == OperationType.Query)
            {
                try
                {
                    var fields = CollectFields(operation.SelectionSet);

                    var queryResult = ExecuteFields(datastore, null, fields, true);

                    return(new GraphQLResult {
                        Data = queryResult.ToDictionary(e => e.Key, e => e.Value.Data)
                    });
                }
                catch (Exception e)
                {
                    return(new GraphQLResult {
                        Errors = new List <string> {
                            e.Message
                        }
                    });
                }
            }
            else if (operation.OperationType == OperationType.Mutation)
            {
                try
                {
                    var fields = CollectFields(operation.SelectionSet);

                    var mutaationResults = ExecuteMutationFields(datastore, null, fields, idFieldName);

                    var responseData  = mutaationResults.ToDictionary(pair => pair.Key, pair => pair.Value.Item1);
                    var notifications = mutaationResults.Select(pair => pair.Value.Item2).Where(e => e != null).ToList();

                    return(new GraphQLResult {
                        Data = responseData, Notifications = notifications
                    });
                }
                catch (Exception e)
                {
                    return(new GraphQLResult {
                        Errors = new List <string> {
                            e.Message
                        }
                    });
                }
            }

            return(new GraphQLResult {
                Errors = new List <string> {
                    $"{operation.OperationType} operation not supported"
                }
            });
        }
Beispiel #14
0
        public static Schema Create(ISchema originalSchema, string query)
        {
            var document = new GraphQLDocumentBuilder().Build(query);

            return(Create(originalSchema, document));
        }