标签: graphql-dotnet

带有 List<Dictionary<string, string>> 的 GraphQL.NET 突变 | JSON 字符串

我想alarms在我的服务器应用程序上注册。为了防止传递 10+ 个参数,我alarm在客户端序列化我的并将其传递List<JSONString>给我的服务器。反序列化它,注册它并给出注册的alarm.

现在我的问题是,我不知道如何传递这些参数:

使用 Mutation - DictionaryType

字典

类型为 \"[String]!\" 的变量 \"$params\" 用于期望类型 \"[DictionaryType]!\" 的位置。"

使用 Mutation - StringGraphType

“无法将值转换为 AST:System.Collections.Generic.Dictionary`2[System.String,System.Object]”,**

服务器

变异 - DictionaryType

public class Mutation : ObjectGraphType
{
    public Mutation()
    {
        Name = "Mutation";

        FieldAsync<HtStaticAlarmBaseType>(
            "registerStaticAlarms",
            "Register a list with static alarms.",
            arguments: new QueryArguments(
                new QueryArgument<NonNullGraphType<ListGraphType<DictionaryType>>> {Name = "params"}
            ),
            resolve: async context =>
            {
                List<object> parameterString = context.GetArgument<List<object>>("params");

                //TODO

                return null;
            }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

变异 - …

c# graphql graphql-dotnet

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

在GraphQL .NET中,如何指定查询可以采用可选参数?

假设我希望能够通过指定用户ID 指定其他标识符(例如电子邮件地址)来查询用户。

您如何构造根Query对象以接受它?

鉴于这种

public class MyQuery : ObjectGraphType
{
    public MyQuery(IUserService userService)
    {
        Name = "Query";

        Field<UserType>(
            "user",
            arguments: new QueryArguments(
                new QueryArgument<IntGraphType>() { Name = "id" },
                new QueryArgument<StringGraphType>() { Name = "email" }
            ),
            resolve: context =>
            {
                int? id = context.GetArgument<int>("id");
                if (id != null)
                {
                    return userService.GetUserById(id);
                }
                string email = context.GetArgument<string>("email");
                if (email != null)
                {
                    return userService.GetUserByEmail(email);
                }
                return null;
            }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?如果在查询中找不到参数,是否会context.GetArgument()返回null?还是提供两个参数来QueryArguments表示查询需要两个参数?

graphql graphql-dotnet

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

如何在c#中的GraphQL客户端端点的请求正文中发送oauth_token和client_id

大家好,我想要在 GraphQL 客户端的请求正文中传递oauth_token和。client_id那么我该如何传递它们,因为 GraphQLRequest 只有三个字段(即 Query 、 Variables 和 OperationName )。请建议。

using GraphQL.Client;

var heroRequest = new GraphQLRequest{ Query = Query };
var graphQLClient = new GraphQLClient("URL");

var  graphQLResponse = await graphQLClient.PostAsync(heroRequest);
Run Code Online (Sandbox Code Playgroud)

c# graphql graphql-dotnet

4
推荐指数
1
解决办法
9336
查看次数

为什么graphql-dotnet会为此模式返回“预期非空值”错误?

我有一个要查询的简单架构,如下所示:

{
  subQuery {
    subObjectGraph {
      Name
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是“ graphiql”抛出以下错误,甚至没有运行我的查询。

{
  "errors": [
    {
      "message": "Expected non-null value, resolve delegate return null for \"$Api.Schema.Queries.MySubObjectGraphType\"",
      "extensions": {
        "code": "INVALID_OPERATION"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我的架构有什么问题(如下)?我正在新建一个SubObject,所以我不明白为什么错误消息暗示该值为null。

    public class Schema: GraphQL.Types.Schema
    {
        public Schema(IDependencyResolver resolver): base(resolver)
        {
            Query = resolver.Resolve<RootQuery>();  
            Mutation = null;
        }
    }

    public class RootQuery: ObjectGraphType
    {
        public RootQuery(IDependencyResolver resolver)
        {
            Name = "Query";

            Field<MySubQuery>(
                name: "subQuery",
                resolve: ctx => resolver.Resolve<MySubQuery>());
        }
    }


    public class MySubQuery: ObjectGraphType
    {
        public …
Run Code Online (Sandbox Code Playgroud)

c# graphql graphql-dotnet

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

如何访问 ASP.NET Core GraphQL 中嵌套字段中的参数

我有一个带有“统计”字段的查询,它是一个具有三个不同累积值的子查询。我希望能够为统计数据提供国家/地区参数,然后在子查询中不同字段的解析器中访问该参数。

我正在使用 GraphQL.Server.Transports.AspNetCore nuget 包。子查询使用 IDependencyResolver 进行解析,因为它对不同字段的解析器中使用的服务有一些依赖性。

我尝试通过 ResolveFieldContext 访问父级,但它似乎不可用。上下文中有一个名为“Source”的属性,但它指的是子查询对象。

如果我们看看 GraphQL 的其他实现,似乎应该是可能的,但我不知道如何从 ASP.NET Core 获取结果

下面的代码显示了名为“CompanyGroupQuery”的主查询的一部分

Field<CompanyStatisticsQuery>(
                "statistics",
                arguments: new QueryArguments(new QueryArgument<StringGraphType> { Name = "country" }),
                resolve: context => resolver.Resolve<CompanyStatisticsQuery>()                
            );
Run Code Online (Sandbox Code Playgroud)

子查询看起来像这样

Field<IntGraphType>(
                "completeInvoicesCount",
                resolve: context => {
                    // This is to show what I'm trying to achieve
                    var parent = context.Parent;
                    var companyId = context.GetArgument<string>("companyId");
                    var country = context.GetArgument<string>("country");

                    return null;

                }
            );
Run Code Online (Sandbox Code Playgroud)

graphql asp.net-core graphql-dotnet

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

在asp.net core 2 graphql端点的情况下,如何提取请求标头并将其传递给业务逻辑?

我使用 asp.net web api 2 和 EntityFramework 6 开发了以下代码片段。

public class TestController : BaseApiController
{
    private readonly ITestService _testService;
    private readonly ICommonService _commonService;
    private readonly IImageService _imageService;
    public TestController(ITestService testService, ICommonService commonService, IImageService imageService)
    {
        _testService = testService;
        _commonService = commonService;
        _imageService = imageService;
    }

    [Route("test")]
    public IHttpActionResult Get()
    {
        var resp = _testService.GetDetailsForLocation(locale);
        return Ok(resp);
    }
}

public class BaseApiController : ApiController
{
    public string locale
    {
        get
        {
            if (Request.Headers.Contains("Accept-Language"))
            {
                return Request.Headers.GetValues("Accept-Language").First();
            }
            else
            {
                return string.Empty; …
Run Code Online (Sandbox Code Playgroud)

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

2
推荐指数
1
解决办法
1388
查看次数

使用 JWT 对字段进行身份验证的 GraqhQL 问题

所以我有 graphql 作为后端和 React / Apollo 作为前端。我已经实现了我的 JWT 令牌身份验证,效果很好。

除此之外,我还有我的中间件,其中提供了 HttpContext 并且用户正确加载了所有声明:

namespace xxx.Web.GQL.Middleware
{
public class GraphQLMiddleware
{
    private readonly RequestDelegate _next;
    private readonly IDocumentWriter _writer;
    private readonly IDocumentExecuter _executor;
    private readonly ISchema _schema;

    public GraphQLMiddleware(RequestDelegate next, IDocumentWriter writer, IDocumentExecuter executor, ISchema schema)
    {
        _next = next;
        _writer = writer;
        _executor = executor;
        _schema = schema;
    }

    public async Task InvokeAsync(HttpContext httpContext)
    {
        if (httpContext.Request.Path.StartsWithSegments("/graphql") && string.Equals(httpContext.Request.Method, "POST", StringComparison.OrdinalIgnoreCase))
        {
            string body;
            using (var streamReader = new StreamReader(httpContext.Request.Body))
            { …
Run Code Online (Sandbox Code Playgroud)

c# graphql graphql-dotnet

2
推荐指数
1
解决办法
1009
查看次数

GraphQL .NET - 结果中的扩展

我开始使用 .NET Core 3.1 的 GraphQL ( https://github.com/graphql-dotnet/graphql-dotnet )。我已经根据我在网上看到的内容构建了一个简单的示例(关于它的信息还不是很多)。

当我进行查询时,我得到如下结果:

{
  "data": {
    "orders": [
      {
        "id": "e9c4325e-4d4c-42f6-963e-be1ad71a5b36",
        "created": "2020-07-18"
      },
      {
        "id": "12926137-cf6f-4b53-8848-443659e50823",
        "created": "2020-07-18"
      },
      {
        "id": "3c0d782d-15b1-474d-8ca9-01e33dad8e00",
        "created": "2020-07-19"
      },
      {
        "id": "befbcd57-7814-4134-9f17-fb45672e44c0",
        "created": "2020-07-19"
      }
    ]
  },
  "extensions": {
    "tracing": {
      "Version": 1,
      "StartTime": "2020-07-18T19:45:15.5554022Z",
      "EndTime": "2020-07-18T19:45:15.6044022Z",
      "Duration": 49108500,
      "Parsing": {
        "StartOffset": 16700,
        "Duration": 1102700
      },
      "Validation": {
        "StartOffset": 1131500,
        "Duration": 421899
      },
      "Execution": {
        "Resolvers": [
          {
            "Path": [
              "orders"
            ],
            "ParentType": "Query",
            "FieldName": "orders", …
Run Code Online (Sandbox Code Playgroud)

graphql graphql-dotnet graphql.net

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