Skip to content

Instantly share code, notes, and snippets.

View mczachurski's full-sized avatar

Marcin Czachurski mczachurski

View GitHub Profile
[Route("api/graphql")]
public class GraphQLController : Controller
{
private readonly GraphQLQuery _graphQLQuery;
private readonly IDocumentExecuter _documentExecuter;
private readonly ISchema _schema;
public GraphQLController(GraphQLQuery graphQLQuery, IDocumentExecuter documentExecuter, ISchema schema)
{
_graphQLQuery = graphQLQuery;
public class GraphQLSchema : Schema
{
public GraphQLSchema(Func<Type, GraphType> resolveType)
: base(resolveType)
{
Query = (GraphQLQuery)resolveType(typeof(GraphQLQuery));
}
}
builder.RegisterType<GraphQLSchema>().As<ISchema>();
builder.Register<Func<Type, GraphType>>(c =>
{
var context = c.Resolve<IComponentContext>();
return t => {
var res = context.Resolve(t);
return (GraphType)res;
};
});
builder.RegisterType<DocumentExecuter>().As<IDocumentExecuter>();
public class GraphQLQuery : ObjectGraphType
{
public GraphQLQuery(IServiceProvider serviceProvider)
{
var type = typeof(IResolver);
var resolversTypes = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p));
foreach(var resolverType in resolversTypes)
public interface IResolver
{
void Resolve(GraphQLQuery graphQLQuery);
}
public class GroupsResolver : Resolver, IGroupsResolver
{
private readonly IGroupsService _groupsService;
public GroupsResolver(IGroupsService groupsService)
{
_groupsService = groupsService;
}
public void Resolve(GraphQLQuery graphQLQuery)
var serviceAssembly = typeof(IResolver).GetTypeInfo().Assembly;
builder.RegisterAssemblyTypes(serviceAssembly)
.Where(t => t.Name.EndsWith("Resolver"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
public class GroupType : ObjectGraphType<GroupDto>, IGraphQLType
{
public GroupType()
{
Field(x => x.Name).Description("The group name.");
Field(x => x.SvgIcon).Description("The icon of group.");
}
}
public class GroupDto
{
public string Name { get; set; }
public string SvgIcon { get; set; }
}