コード例 #1
0
        public static ISchema MakeExecutableSchemaWithIntrospection(
            SchemaBuilder builder,
            IResolverMap resolvers,
            ISubscriberMap subscribers = null,
            IReadOnlyDictionary <string, IValueConverter> converters        = null,
            IReadOnlyDictionary <string, CreateDirectiveVisitor> directives = null)
        {
            // add converters
            if (converters != null)
            {
                UseConverters(builder, converters);
            }

            builder.UseResolversAndSubscribers(resolvers, subscribers);

            if (directives != null)
            {
                builder.ApplyDirectives(directives);
            }

            var schema        = builder.Build();
            var introspection = Introspect.Schema(schema);

            return(new SchemaBuilder()
                   .Merge(schema, introspection)
                   .Build());
        }
コード例 #2
0
ファイル: SchemaTools.cs プロジェクト: qonn/tanka-graphql
        public static ISchema MakeExecutableSchema(
            SchemaBuilder builder,
            IResolverMap resolvers,
            ISubscriberMap subscribers = null,
            Dictionary <string, CreateDirectiveVisitor> directives = null)
        {
            // bind resolvers
            builder.UseResolversAndSubscribers(resolvers, subscribers);

            // execute directives
            if (directives != null)
            {
                builder.ApplyDirectives(directives);
            }

            return(builder.Build());
        }
コード例 #3
0
        public async Task Part3_ApplyDirectives_on_Object_fields()
        {
            // Create builder, load sdl with our types
            // and bind the resolvers
            var builder = new SchemaBuilder()
                          .Sdl(@"
                    directive @duplicate on FIELD_DEFINITION

                    type Query {
                        name: String @duplicate                
                    }
                    ")
                          .GetQuery(out var query)
                          .UseResolversAndSubscribers(
                new ObjectTypeMap
            {
                {
                    query.Name, new FieldResolversMap
                    {
                        {
                            "name", context =>
                            {
                                var result = Resolve.As("Test");
                                return(new ValueTask <IResolverResult>(result));
                            }
                        }
                    }
                }
            });

            // Apply directives to schema by providing a visitor which
            // will transform the fields with the directive into new
            // fields with the directive logic. Note that the original
            // field will be replaced.
            builder.ApplyDirectives(new Dictionary <string, CreateDirectiveVisitor>()
            {
                ["duplicate"] = _ => new DirectiveVisitor()
                {
                    // Visitor will visit field definitions
                    FieldDefinition = (directive, fieldDefinition) =>
                    {
                        // We will create new field definition with
                        // new resolver. New resolver will execute the
                        // original resolver, duplicate value and return it
                        return(fieldDefinition
                               .WithResolver(resolver =>
                                             resolver.Use(async(context, next) =>
                        {
                            // We need to first call the original resolver to
                            // get the initial value
                            var result = await next(context);

                            // for simplicity we expect value to be string
                            var initialValue = result.Value.ToString();

                            // return new value
                            return Resolve.As(initialValue + initialValue);
                        }).Run(fieldDefinition.Resolver)));
                    }
                }
            });

            // Build schema
            var schema = builder.Build();

            // Get resolver for Query.name field
            var nameResolver = schema.GetResolver(schema.Query.Name, "name");

            // Execute the resolver. This is normally handled by the executor.
            var nameValue = await nameResolver(null);

            Assert.Equal("TestTest", nameValue.Value);
        }