标签: graphql-dotnet

带有GraphQL的EF Core

我正在探索GraphQL开发,我正在探索通过EF Core生成什么样的SQL查询,我发现无论我的GraphQL查询只包含几个字段,EF Core都会为所有字段发送SQL Select.实体.

这是我现在使用的代码:

public class DoctorType : ObjectGraphType<Doctors>
{
    public DoctorType()
    {
            Field(d => d.PrefixTitle);
            Field(d => d.FName);
            Field(d => d.MName);
            Field(d => d.LName);
            Field(d => d.SufixTitle);
            Field(d => d.Image);
            Field(d => d.EGN);
            Field(d => d.Description);
            Field(d => d.UID_Code); 
    }
}

public class Doctors : ApplicationUser
{
    public string Image { get; set; }
    [StringLength(50)]
    public string UID_Code { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用的查询是

{
  doctors{
    fName
    lName
  }
}
Run Code Online (Sandbox Code Playgroud)

生成的SQL选择Doctor实体的所有字段.

有没有办法进一步优化EF Core生成的SQL查询?

我猜这种情况发生的原因是DoctorType继承ObjectGraphType<Doctors>了医生的某些Projection,而不是来自医生的一些Projection,但我想不出一个聪明的解决方法呢?

有什么建议?

编辑: …

c# entity-framework-core graphql asp.net-core graphql-dotnet

9
推荐指数
1
解决办法
1135
查看次数

在 Azure Function 中启用同步 IO

我正在构建一个 HTTPTriggered Azure 函数,目标是运行 GraphQL .NET 服务器的 netcoreapp3.0。GraphQL .NET 要求将AllowSynchronousIO其设置为true,但我不知道如何为 Azure 函数执行此操作。我尝试实现我自己的Startup类,该类扩展FunctionsStartup并在Configure方法中添加了以下代码,但没有成功。

builder.Services
    .AddOptions<KestrelServerOptions>()
    .Configure<IConfiguration>((settings, configuration) =>
    {
        settings.AllowSynchronousIO = true;
        configuration.Bind(settings);
    });
Run Code Online (Sandbox Code Playgroud)

我得到的错误信息是:

发生了未处理的主机错误。

Microsoft.AspNetCore.Server.Kestrel.Core:不允许同步操作。调用 WriteAsync 或将 AllowSynchronousIO 设置为 true。

任何帮助将不胜感激!

.net-core azure-functions graphql-dotnet

7
推荐指数
1
解决办法
3485
查看次数

使用Relay和graphql-dotnet上传文件

我正在尝试在包含文件的中继中构建一个突变。一旦实现了getFiles()此处引用的方法,就可以:https : //facebook.github.io/relay/docs/api-reference-relay-mutation.html#getfiles

中继发送一个多部分请求,导致来自ASP.NET Core MVC的415错误。

我正在寻找一个工作示例,类似于带有graphql-dotnet库的如何在React-Relay应用程序中上传文件? ” 。

asp.net asp.net-mvc graphql relayjs graphql-dotnet

6
推荐指数
1
解决办法
1370
查看次数

如何在 Resolver 函数级别使用 GraphQL.NET 实现授权?

我正在寻找有关如何使用 GraphQL.NET 和 ASP.NET CORE 2 在解析器函数级别实现授权的示例代码和示例。

基本上,如果请求未经授权,我试图阻止查询的执行。

任何人都可以帮助我获得一些好的教程或代码示例作为实现的参考。

c# asp.net-core-2.0 graphql-dotnet

6
推荐指数
2
解决办法
7340
查看次数

将 .NET 枚举转换为 GraphQL EnumerationGraphType

如何将枚举转换为 GraphQL 使用的 EnumerationGraphType?这是一个例子来说明我在说什么:

public enum MeetingStatusType
{
    Tentative,
    Unconfirmed,
    Confirmed,
}
Run Code Online (Sandbox Code Playgroud)
public class MeetingDto
{
    public string Id { get; set; }
    public string Name { get; set; }
    public MeetingStatusType Status { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
public class MeetingStatusEnumType : EnumerationGraphType<MeetingStatusType>
{
    public MeetingStatusEnumType()
    {
        Name = "MeetingStatusType";
    }
}
Run Code Online (Sandbox Code Playgroud)
public class MeetingType : ObjectGraphType<MeetingDto>
{
    public MeetingType()
    {
        Field(m => m.Id);
        Field(m => m.Name, nullable: true);
        Field<MeetingStatusEnumType>(m => m.Status); // Fails here
     }
}
Run Code Online (Sandbox Code Playgroud)

显然这不起作用,因为没有从MeetingStatusTypeto的隐式转换 …

c# .net-core graphql graphql-dotnet

6
推荐指数
1
解决办法
2763
查看次数

在用于突变的输入类型上有一个 JSON 字典字段

我正在尝试创建一个接受输入类型的突变FooInputType。我的问题是在该输入上有一个字段来表示 JSON 字典(字符串-字符串)。例如,foo.tags字段可以有任何一组字符串类型的键值对。

样本突变:

{
  "query": "mutation ($foo: FooInputType) { addFoo(foo: $foo) { tags } }",
  "variables": {
    "foo": { "bar": "test", "tags": { "priority": "high" } }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是突变:

class MyMutation : ObjectGraphType {
    public MyMutation() {
        Field<TaskItemType>("addFoo", "Add a new foo",
            new QueryArgument<FooInputType> {Name = "foo"}
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

和输入类型:

class FooInputType : InputObjectGraphType {
    public FooInputType() {
        Field<StringGraphType>("bar");
        Field<InputObjectGraphType<JObject>>("tags");
    }
}
Run Code Online (Sandbox Code Playgroud)

c# graphql graphql-dotnet

5
推荐指数
0
解决办法
516
查看次数

使用 JWT 通过 Asp.net core 进行 GraphQL 身份验证

我正在使用GraphQL for .NET包来处理 graphql。但我无法理解如何在 graphql 查询或突变中使用 JWT 进行身份验证。

我阅读了有关授权的指南,但无法完成。

我需要用于 .NET 身份验证的 GraphQL 帮助。

任何帮助将不胜感激。

谢谢

authentication graphql asp.net-core graphql-dotnet

5
推荐指数
1
解决办法
8715
查看次数

如何处理数据加载器/GraphQL 嵌套查询中的并发 DbContext 访问?

我正在使用几个数据加载器,它们使用注入的查询服务(反过来又依赖于 DbContext)。它看起来像这样:

Field<ListGraphType<UserType>>(
  "Users",
  resolve: context =>
  {
    var loader = accessor.Context.GetOrAddBatchLoader<Guid, IEnumerable<User>>(
      "MyUserLoader",
      userQueryService.MyUserFunc);

    return loader.LoadAsync(context.Source.UserId);
  });
Run Code Online (Sandbox Code Playgroud)
Field<ListGraphType<GroupType>>(
  "Groups",
  resolve: context =>
  {
    var loader = accessor.Context.GetOrAddBatchLoader<Guid, IEnumerable<Group>>(
      "MyGroupLoader",
      groupQueryService.MyGroupFunc);

    return loader.LoadAsync(context.Source.GroupId);
  });
Run Code Online (Sandbox Code Playgroud)

当我运行同时使用两个数据加载器的嵌套查询时,出现异常 "A second operation started on this context before a previous asynchronous operation completed"因为两个数据加载器同时使用相同的 DbContext。

在查询中允许并发数据库访问而不必仔细管理 DbContexts 的最佳方法是什么ServiceLifeTime.Transient?或者数据加载器是否可以公开一种方法来知道何时处理瞬态 DbContext?

entity-framework entity-framework-core graphql graphql-dotnet dataloader

5
推荐指数
1
解决办法
783
查看次数

有些服务无法使用该库构建:NetCore.AutoRegisterDi

我正在开发基于 ASP.NET Core 3.1 GraphQL 的 API。我使用下面的参考文章在 API 层中设置自动 DI 配置并使用了 nuget 包:NetCore.AutoRegisterDi

https://www.thereformedprogrammer.net/asp-net-core-fast-and-automatic-dependency-injection-setup/

这是代码详细信息:

代码:

启动.cs:

public virtual void ConfigureServices(IServiceCollection services) => services
                      .AddGraphQLResolvers()
                      .AddProjectRepositories();
Run Code Online (Sandbox Code Playgroud)

ProjectServiceCollectionExtensions.cs

public static class ProjectServiceCollectionExtensions
{
    public static IServiceCollection AddProjectRepositories(this IServiceCollection services) => 
        services.RegisterAssemblyPublicNonGenericClasses(Assembly.GetAssembly(typeof(CommonService)))
                .Where(c => c.Name.EndsWith("Persistence"))
                .AsPublicImplementedInterfaces(ServiceLifetime.Scoped);

    public static IServiceCollection AddGraphQLResolvers(this IServiceCollection services) =>
       services
           .AddScoped<ICountriesResolver, CountriesResolver>()
           .AddScoped<ICountryGroupsResolver, CountryGroupsResolver>()
           .AddScoped<IDisclaimerResolver, DisclaimerResolver>();
}
Run Code Online (Sandbox Code Playgroud)

上面的CommonService是服务层的一部分,以持久性结束。

国家解析器.cs

public class CountriesResolver : Resolver, ICountriesResolver
{
    private readonly ICountryService _countryService;
    private readonly IHttpContextAccessor _accessor;
    private readonly …
Run Code Online (Sandbox Code Playgroud)

dependency-injection graphql-dotnet c#-8.0 asp.net-core-3.1 netcore.autoregisterdi

5
推荐指数
1
解决办法
3万
查看次数

GraphQL 中是否可以发送具有多个值的 Enum

在 C# 中,可以创建带有标志属性的枚举。( https://learn.microsoft.com/en-us/dotnet/api/system.flagsattribute?view=netstandard-2.1 ) 这意味着枚举可以看起来像这样:

[Flags]
enum EnumWithFlags 
{
  None = 0,
  FlagOne = 1,
  FlagTwo = 2,
  FlagThree = 4
}
Run Code Online (Sandbox Code Playgroud)

EnumWithFlags 的值为 5,这意味着它将同时具有 FlagThree 和 FlagOne。

这对于 Enum 输入类型也可能吗?有这方面的例子吗?

c# enums graphql graphql-dotnet

5
推荐指数
1
解决办法
1596
查看次数