Skip to content

Commit 83817a1

Browse files
committed
2 parents 83b8e50 + 7725dfc commit 83817a1

7 files changed

Lines changed: 140 additions & 16 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Newtonsoft.Json.Linq;
2+
3+
namespace Dapper.GraphQL.Test.GraphQL
4+
{
5+
public class GraphQlQuery
6+
{
7+
public string OperationName { get; set; }
8+
public string NamedQuery { get; set; }
9+
public string Query { get; set; }
10+
public JObject Variables { get; set; }
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using GraphQL.Types;
2+
3+
namespace Dapper.GraphQL.Test.GraphQL
4+
{
5+
public class PersonInputType : InputObjectGraphType
6+
{
7+
public PersonInputType()
8+
{
9+
Name = "PersonInput";
10+
Field<StringGraphType>("firstName");
11+
Field<StringGraphType>("lastName");
12+
}
13+
}
14+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Data;
3+
using System.Linq;
4+
using Dapper.GraphQL.Test.EntityMappers;
5+
using Dapper.GraphQL.Test.Models;
6+
using GraphQL.Types;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
10+
namespace Dapper.GraphQL.Test.GraphQL
11+
{
12+
public class PersonMutation : ObjectGraphType
13+
{
14+
public PersonMutation(IQueryBuilder<Person> personQueryBuilder, IServiceProvider serviceProvider)
15+
{
16+
Field<PersonType>(
17+
"addPerson",
18+
description: "Adds new person.",
19+
arguments: new QueryArguments(
20+
new QueryArgument<PersonInputType> { Name = "person" }
21+
),
22+
resolve: context =>
23+
{
24+
var person = context.GetArgument<Person>("person");
25+
26+
using (var connection = serviceProvider.GetRequiredService<IDbConnection>())
27+
{
28+
person.Id = person.MergedToPersonId = PostgreSql.NextIdentity(connection, (Person p) => p.Id);
29+
30+
bool success = SqlBuilder
31+
.Insert(person)
32+
.Execute(connection) > 0;
33+
34+
if (success)
35+
{
36+
var personMapper = new PersonEntityMapper();
37+
38+
var query = SqlBuilder
39+
.From<Person>("Person")
40+
.Select("FirstName, LastName")
41+
.Where("ID = @personId", new { personId = person.Id });
42+
43+
var results = query
44+
.Execute(connection, context.FieldAst, personMapper)
45+
.Distinct();
46+
return results.FirstOrDefault();
47+
}
48+
49+
return null;
50+
}
51+
}
52+
);
53+
}
54+
}
55+
}
Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1-
using Dapper.GraphQL.Test.EntityMappers;
2-
using Dapper.GraphQL.Test.Models;
3-
using Dapper.GraphQL.Test.QueryBuilders;
4-
using GraphQL.Types;
5-
using Microsoft.Extensions.DependencyInjection;
6-
using System;
7-
using System.Collections.Generic;
8-
using System.Text;
1+
using GraphQL;
92

103
namespace Dapper.GraphQL.Test.GraphQL
114
{
125
public class PersonSchema :
136
global::GraphQL.Types.Schema
147
{
15-
public PersonSchema(
16-
IServiceProvider serviceProvider,
17-
PersonQuery personQuery)
8+
public PersonSchema(IDependencyResolver resolver) : base(resolver)
189
{
19-
//Mutation = mutation;
20-
ResolveType = type => serviceProvider.GetRequiredService(type) as GraphType;
21-
Query = personQuery;
10+
Query = resolver.Resolve<PersonQuery>();
11+
Mutation = resolver.Resolve<PersonMutation>();
2212
}
2313
}
2414
}

Dapper.GraphQL.Test/GraphQLTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System.Threading.Tasks;
22
using Xunit;
3+
using Dapper.GraphQL.Test.GraphQL;
4+
using Newtonsoft.Json.Linq;
5+
using Dapper.GraphQL.Test.Models;
36

47
namespace Dapper.GraphQL.Test
58
{
@@ -333,5 +336,35 @@ public async Task SimplePersonQuery()
333336

334337
Assert.True(fixture.JsonEquals(expectedJson, json));
335338
}
339+
340+
[Fact(DisplayName = "Simple person insert should succeed")]
341+
public async Task SimplePersonInsert()
342+
{
343+
GraphQlQuery graphQuery = new GraphQlQuery();
344+
graphQuery.OperationName = "addPerson";
345+
graphQuery.Variables = JObject.Parse(@"{""person"":{""firstName"":""Joe"",""lastName"":""Doe""}}");
346+
347+
graphQuery.Query = @"
348+
mutation ($person: PersonInput!) {
349+
addPerson(person: $person) {
350+
firstName
351+
lastName
352+
}
353+
}";
354+
355+
var json = await fixture.QueryGraphQLAsync(graphQuery);
356+
357+
var expectedJson = @"
358+
{
359+
data: {
360+
addPerson: {
361+
firstName: 'Joe',
362+
lastName: 'Doe'
363+
}
364+
}
365+
}";
366+
367+
Assert.True(fixture.JsonEquals(expectedJson, json));
368+
}
336369
}
337370
}

Dapper.GraphQL.Test/TestFixture.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Newtonsoft.Json.Linq;
1111
using Npgsql;
1212
using System;
13+
using System.Collections.Generic;
1314
using System.Data;
1415
using System.Linq;
1516
using System.Reflection;
@@ -96,6 +97,21 @@ public async Task<string> QueryGraphQLAsync(string query)
9697
return json;
9798
}
9899

100+
public async Task<string> QueryGraphQLAsync(GraphQlQuery query)
101+
{
102+
var result = await DocumentExecuter
103+
.ExecuteAsync(options =>
104+
{
105+
options.Schema = Schema;
106+
options.Query = query.Query;
107+
options.Inputs = query.Variables != null ? new Inputs(StringExtensions.GetValue(query.Variables) as Dictionary<string, object>) : null;
108+
})
109+
.ConfigureAwait(false);
110+
111+
var json = new DocumentWriter(indent: true).Write(result);
112+
return json;
113+
}
114+
99115
public void SetupDatabaseConnection()
100116
{
101117
// Generate a random db name
@@ -148,6 +164,8 @@ FROM pg_stat_activity
148164

149165
private void SetupDapperGraphQL(IServiceCollection serviceCollection)
150166
{
167+
serviceCollection.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));
168+
151169
serviceCollection.AddDapperGraphQL(options =>
152170
{
153171
// Add GraphQL types
@@ -156,6 +174,8 @@ private void SetupDapperGraphQL(IServiceCollection serviceCollection)
156174
options.AddType<PersonType>();
157175
options.AddType<GraphQL.PhoneType>();
158176
options.AddType<PersonQuery>();
177+
options.AddType<PersonMutation>();
178+
options.AddType<PersonInputType>();
159179

160180
// Add the GraphQL schema
161181
options.AddSchema<PersonSchema>();

Dapper.GraphQL/Dapper.GraphQL.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
<ItemGroup>
2323
<PackageReference Include="Dapper" Version="1.50.4" />
2424
<PackageReference Include="Dapper.SqlBuilder" Version="1.50.4" />
25-
<PackageReference Include="GraphQL" Version="0.17.3" />
25+
<PackageReference Include="GraphQL" Version="2.3.0" />
2626
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
27-
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
27+
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
2828
</ItemGroup>
2929

3030
</Project>

0 commit comments

Comments
 (0)