Skip to content

Commit 1284369

Browse files
Merge pull request #22 from rasmusjp/feature/query-by-type
Allow quering content by type
2 parents bd656a8 + a8e1424 commit 1284369

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/Our.Umbraco.GraphQL/Our.Umbraco.GraphQL.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@
256256
<Compile Include="Types\ConnectionExtensions.cs" />
257257
<Compile Include="Types\ConnectionWithFilterBuilder.cs" />
258258
<Compile Include="Types\PublishedContentAtRootQuery.cs" />
259+
<Compile Include="Types\PublishedContentByTypeQuery.cs" />
259260
<Compile Include="Types\PublishedContentQuery.cs" />
260261
<Compile Include="Web\AppBuilderExtensions.cs" />
261262
<Compile Include="Web\GraphQLServerOptions.cs" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
using GraphQL.Types;
3+
4+
namespace Our.Umbraco.GraphQL.Types
5+
{
6+
public class PublishedContentByTypeQuery : ObjectGraphType
7+
{
8+
public PublishedContentByTypeQuery(IEnumerable<IGraphType> documentGraphTypes)
9+
{
10+
Name = "PublishedContentByTypeQuery";
11+
12+
foreach (var type in documentGraphTypes)
13+
{
14+
string documentTypeAlias = type.GetMetadata<string>("documentTypeAlias");
15+
16+
Field<PublishedContentGraphType>()
17+
.Name(type.Name)
18+
.Argument<NonNullGraphType<IdGraphType>>("id", "The unique content id")
19+
.Type(type)
20+
.Resolve(context =>
21+
{
22+
var userContext = (UmbracoGraphQLContext)context.UserContext;
23+
var id = context.GetArgument<int>("id");
24+
return userContext.Umbraco.TypedContent(id);
25+
});
26+
}
27+
}
28+
}
29+
}

src/Our.Umbraco.GraphQL/Types/PublishedContentQuery.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public PublishedContentQuery(IEnumerable<IGraphType> documentGraphTypes)
1919
return userContext.Umbraco.TypedContent(id);
2020
});
2121

22+
Field<NonNullGraphType<PublishedContentAtRootQuery>>()
23+
.Name("byType")
24+
.Resolve(context => context.ReturnType)
25+
.Type(new NonNullGraphType(new PublishedContentByTypeQuery(documentGraphTypes)));
26+
2227
Field<NonNullGraphType<PublishedContentAtRootQuery>>()
2328
.Name("atRoot")
2429
.Resolve(context => context.ReturnType)

0 commit comments

Comments
 (0)