Mutation

Specification

Execution


        [Fact]
        public async Task Mutation1()
        {
            /* Given */
            var mutation = Parser.ParseDocument(
                @"mutation AddEvent($event: NewEvent!) {
                    create(event: $event) {
                        __typename
                        ...on Success {
                            id
                            event {
                                payload
                            }
                        }
                        
                        ...on Failure {
                            message
                        }
                    }
                }");

            var variables = NewEvent(EventsModel.EventType.INSERT, "payload");

            /* When */
            var result = await Executor.ExecuteAsync(new ExecutionOptions
            {
                Schema = Schema,
                Document = mutation,
                VariableValues = variables
            });

            /* Then */
            result.ShouldMatchJson(
                @"{
                  ""data"": {
                    ""create"": {
                      ""__typename"": ""Success"",
                      ""id"": ""1"",
                      ""event"": {
                        ""payload"": ""payload""
                      }
                    }
                  }
                }");
        }


        [Fact]
        public async Task Mutation2()
        {
            /* Given */
            var mutation = Parser.ParseDocument(
                @"mutation AddEvent($event: NewEvent!) {
                    create(event: $event) {
                        __typename
                        ...on Success {
                            id
                        }
                        
                        ...on Failure {
                            message
                        }
                    }
                }");

            var variables = NewEvent(EventsModel.EventType.INSERT, null);

            /* When */
            var result = await Executor.ExecuteAsync(new ExecutionOptions
            {
                Schema = Schema,
                Document = mutation,
                VariableValues = variables
            });

            /* Then */
            result.ShouldMatchJson(
                @"{
                  ""data"": {
                    ""create"": {
                      ""__typename"": ""Failure"",
                      ""message"": ""Payload should be given""
                    }
                  }
                }");
        }

Example

These are the schema and resolvers used in the above section.

Schema

Sdl = @"
                enum EventType {
                    INSERT
                    UPDATE
                    DELETE
                }

                type Success {
                    id: ID!
                    event: Event
                }

                type Failure {
                    message: String!
                }

                union Result = Success | Failure

                type Event {
                    id: ID!
                    type: EventType!
                    payload: String
                }

                input NewEvent {
                    type: String!
                    payload: String
                }

                type Query {
                    events: [Event!]
                }

                type Mutation {
                    create(event: NewEvent!): Result
                }

                type Subscription {
                    events: Event!
                }

                schema {
                    query: Query
                    mutation: Mutation
                    subscription: Subscription
                }
                "

Initialization


        public ExecutorFacts(ITestOutputHelper atr)
        {
            Model = new EventsModel();
            Resolvers = new ObjectTypeMap
            {
                {
                    "Success", new FieldResolversMap
                    {
                        {"id", Resolve.PropertyOf<EventsModel.Success>(m => m.Id)},
                        {"event", Resolve.PropertyOf<EventsModel.Success>(m => m.Event)}
                    }
                },
                {
                    "Failure", new FieldResolversMap
                    {
                        {"message", Resolve.PropertyOf<EventsModel.Failure>(m => m.Message)}
                    }
                },
                {
                    "Event", new FieldResolversMap
                    {
                        {"id", Resolve.PropertyOf<EventsModel.Event>(ev => ev.Id)},
                        {"type", Resolve.PropertyOf<EventsModel.Event>(ev => ev.Type)},
                        {"payload", Resolve.PropertyOf<EventsModel.Event>(ev => ev.Payload)}
                    }
                },
                {
                    "NewEvent", new FieldResolversMap
                    {
                        {"type", Resolve.PropertyOf<EventsModel.NewEvent>(type => type.Type)},
                        {"payload", Resolve.PropertyOf<EventsModel.NewEvent>(type => type.Payload)}
                    }
                },
                {
                    "Query", new FieldResolversMap
                    {
                        {"events", context => new ValueTask<IResolverResult>(Resolve.As(Model.Events))}
                    }
                },
                {
                    "Mutation", new FieldResolversMap
                    {
                        {
                            "create", async context =>
                            {
                                var newEvent = context.GetObjectArgument<EventsModel.NewEvent>("event");

                                if (newEvent.Payload == null)
                                    return Resolve.As(
                                        context.ExecutionContext.Schema.GetNamedType<ObjectType>("Failure"),
                                        new EventsModel.Failure("Payload should be given"));

                                var id = await Model.AddAsync(newEvent);
                                var ev = Model.Events.Single(e => e.Id == id);

                                return Resolve.As(
                                    context.ExecutionContext.Schema.GetNamedType<ObjectType>("Success"),
                                    new EventsModel.Success(id, ev));
                            }
                        }
                    }
                },
                {
                    "Subscription", new FieldResolversMap
                    {
                        {
                            "events", async (context, unsubscribe) =>
                            {
                                await Task.Delay(0);
                                var source = Model.Subscribe(unsubscribe);
                                return source;
                            },
                            context => new ValueTask<IResolverResult>(Resolve.As(context.ObjectValue))
                        }
                    }
                }
            };

            Schema = SchemaTools.MakeExecutableSchema(
                new SchemaBuilder().Sdl(Parser.ParseDocument(Sdl)),
                Resolvers,
                Resolvers);
        }