GraphQL - 将 ObjectType 传递给参数

     2023-03-08     56

关键词:

【中文标题】GraphQL - 将 ObjectType 传递给参数【英文标题】:GraphQL - passing an ObjectType a parameter 【发布时间】:2017-01-02 07:50:28 【问题描述】:

我正在使用GraphQL,它运行良好,但是,我似乎无法弄清楚如何将参数传递到我的Event GraphQLObjectTypefields 部分。

我希望能够将currentUserId(通过令牌给我)传递给Event GraphQLObjectType,这样我就可以添加isAttending 属性。

我已经附上了我基本上想要做的 cmets 代码:

const Event = new GraphQLObjectType(
  name: 'Event',
  description: 'This represents an Event',
  fields: (currentUserId) =>  // currentUserId is the parameter I would like to pass in 
    return 
      id: 
        type: GraphQLInt,
        resolve (event) 
          return event.id;
        
      ,
      title: 
        type: GraphQLString,
        resolve (event) 
          return event.title;
        
      ,
      attendees: 
        type: new GraphQLList(User),
        resolve (event) 
          return event.getAttendees()
        
      ,
      // this is what I would like to do
      isAttending: 
        type: GraphQLBool,
        resolve (event) 
          return event.getAttendees(
            where: 
              id: currentUserId // that's the parameter I would like pass in
            
          ).then(attendee => 
            return (attendee.length > 0 ? true : false);
          );
        
      
      // end of what I'm trying to do //
    ;
  
);

const Query = new GraphQLObjectType(
  name: 'Query',
  description: 'Root query object',
  fields: () => 
    return 
      events: 
        type: new GraphQLList(Event),
        args: 
          id: 
            type: GraphQLInt
          
        ,
        resolve (root, args) 
          // here is the parameter I would like to pass to the event object
          let currentUserId = root.userId; 
          ////////

          return Db.models.event.findAll( where: args );
        
      ,
      ...

更新

我不能只做data.currentUserId = root.userId 的原因是因为当我返回collection of event objects 时它不可见,因为传递给我的Event GraphQLOBjectType 的只是event 对象。

当我执行data.currentUserId 并且data 内部有一个对象数组时的样子是这样的:

[objects, currentUserId: 1] 

与我们想要的相反:

[object, currentUserId: 1, anotherObject, currentUserId: 1] 

如果我想访问Event GraphQLObject 中的currentUserId,我唯一能想到的就是遍历每个对象并将currentUserId 添加到它上面,如下所示:

return events.map(event =>  
  event.currentUserId = currentUserId; 
  return event; 
);`

这是最好的解决方案吗?

【问题讨论】:

【参考方案1】:

恐怕你做不到。 fields 不接收任何参数,所以你也不会发送任何参数。

幸运的是,您可以通过更方便的方式实现这一点。

您的父类型 (Query) 在 resolve 函数中返回的所有内容都在子解析的 root 参数中可见。

const Query = new GraphQLObjectType(
    name: 'Query',
    description: 'Root query object',
    fields: () => (
            events: 
                type: new GraphQLList(Event),
                args: 
                    id: 
                        type: GraphQLInt
                    
                ,
                resolve (root, args) 

                    return Db.models.event.findAll( where: args )
                            .then(data => 
                                // pass the parameter here
                                data.currentUserId = root.userId;
                                return data;
                            );
                
            ,
            ...

那么您的 Event 对象将如下所示:

const Event = new GraphQLObjectType(
    name: 'Event',
    description: 'This represents an Event',
    fields: () => (

        ...

        isAttending: 
            type: GraphQLBool,
            resolve: (event) => 
                return event.getAttendees(
                    where: 
                        id: event.currentUserId // that's the parameter you've passed through parent resolve
                    
                ).then(attendee => 
                    return (attendee.length > 0 ? true : false);
                );
            
        
    )
);

【讨论】:

嘿,你能看看我对这个问题的更新吗?我尝试了您的解决方案,但不得不对其进行一些更改。请让我知道您的想法@LordDave 它可能看起来很难看,但我认为这是最好的也是唯一的解决方案。由于除了通过解析的根参数之外,没有其他方法可以将变量直接传递给子项,因此您必须使用这个新变量扩展集合中的所有对象。理想的解决方案是将其在 Sequelize 层上传递给结果,但不幸的是 Sequelize 做不到 我认为这是目前最好的解决方案。我实际上遇到了同样的问题,并编写了一个迭代器,它在 resolve 函数中通过 fieldASTs 运行,获取嵌套参数并允许根据参数查询字段。但是这个函数非常混乱,我很尴尬公开展示它:(实际上你已经可以为字段设置参数,但仅用于在获取后操作结果,即用 0 填充单个数字。 这似乎是 GraphQL 中的一个持续争论。 Facebook 自己显然通过让所有查询都从具有当前用户信息的 viewer 节点开始来处理它。

将 Typegoose 与 GraphQL 一起使用

...在另一个对象中有一个嵌套对象列表。我定义的类如下@ObjectType()exportclassChildClass@prop()@Field(nullable:true)someField:string;@ 查看详情

如何将变量传递给 GraphQL 查询?

】如何将变量传递给GraphQL查询?【英文标题】:HowtopassvariablestoaGraphQLquery?【发布时间】:2017-05-1514:36:28【问题描述】:假设我在React-Apollo中有以下GraphQL查询constCurrentUserForLayout=gql`queryCurrentUserForLayout($avatarID:Int!)currentUseravatar_url(... 查看详情

有没有办法将数据从页面 graphql 调用传递到组件 graphql 调用?

】有没有办法将数据从页面graphql调用传递到组件graphql调用?【英文标题】:Isthereawaytopassdatafromapagegraphqlcalltoacomponentgraphqlcall?【发布时间】:2020-04-0423:03:59【问题描述】:我正在尝试将数据(contentful_id)传递给组件graphql调用。这... 查看详情

GraphQL.js Node/Express:如何将对象作为 GraphQL 查询参数传递

】GraphQL.jsNode/Express:如何将对象作为GraphQL查询参数传递【英文标题】:GraphQL.jsNode/Express:HowtopassobjectasGraphQLqueryargument【发布时间】:2017-08-0101:32:17【问题描述】:我的目标是能够在GraphQL查询中将对象作为参数传递。目标:accoun... 查看详情

将 req.user 传递给 graphQL

】将req.user传递给graphQL【英文标题】:Passingreq.usertographQL【发布时间】:2019-05-2410:25:46【问题描述】:我是GraphQL新手,对Node.js有一些经验。我在我的应用程序中使用护照登录用户,因此我的req将在包含用户信息的快递中附加req.... 查看详情

GraphQL - 将枚举值作为参数直接传递给突变?

】GraphQL-将枚举值作为参数直接传递给突变?【英文标题】:GraphQL-Passinganenumvaluedirectlytoamutationasanargument?【发布时间】:2018-05-2202:59:04【问题描述】:给定以下GraphQL类型定义:consttypeDefs=`enumActionupdatedeletetypeMutationdoSomething(action:... 查看详情

GraphQL - 将非特定对象的对象作为参数传递

】GraphQL-将非特定对象的对象作为参数传递【英文标题】:GraphQL-passinganobjectofnonspecificobjectsasanargument【发布时间】:2017-10-2607:22:58【问题描述】:我对GraphQL很陌生。我正在尝试将像这样的对象作为参数传递:filters:status:\'approved\... 查看详情

如何使用 GraphQL.Net 和 c# 将集合传递给 GraphQL 突变?

】如何使用GraphQL.Net和c#将集合传递给GraphQL突变?【英文标题】:HowdoIpassacollectiontoaGraphQLmutationwithGraphQL.Netandc#?【发布时间】:2019-09-1819:48:45【问题描述】:我正在尝试构建一个接受集合的突变。GraphQL服务器是用c#.netcore编写的... 查看详情

将函数结果传递给 graphql 突变

】将函数结果传递给graphql突变【英文标题】:Passfunctionresultintographqlmutation【发布时间】:2019-05-1204:15:36【问题描述】:我的checker()函数确定用户的输入(checkedNumber)在我的素数数据库中是否匹配。如果有,我需要updateCheckedNumber()... 查看详情

将变量从输入传递到 GraphQL 搜索调用

】将变量从输入传递到GraphQL搜索调用【英文标题】:PassvariablefrominputtoGraphQLsearchcall【发布时间】:2018-08-2015:58:27【问题描述】:我正在学习graphql和react-apollo。我在我的代码中设置了一个搜索查询。我不确定如何将变量从我的代... 查看详情

如何将请求标头传递给 graphql 解析器

】如何将请求标头传递给graphql解析器【英文标题】:Howtopassrequestheadersthroughtographqlresolvers【发布时间】:2018-09-2109:29:26【问题描述】:我有一个由JWT授权的graphql端点。我的JWT策略验证JWT,然后将用户对象添加到请求对象。在一... 查看详情

React,Graphql - 如何将变量传递给突变

】React,Graphql-如何将变量传递给突变【英文标题】:React,Graphql-howtopassvariabletomutation【发布时间】:2020-05-0505:03:36【问题描述】:我正在尝试在React中使用Graphql创建一个简单的CRUD应用程序。我有创建和阅读,但我真的在为删除... 查看详情

无法将自定义结果从解析器传递到 Graphql

】无法将自定义结果从解析器传递到Graphql【英文标题】:CannotpasscustomresultfromresolvertoGraphql【发布时间】:2021-01-0617:38:12【问题描述】:我正在尝试使用带有属性的sequelize获取数据并将其传递给graphql。结果在控制台中很好,但gra... 查看详情

ApolloClient:不能将 GraphQL 查询作为 JS 变量传递? “不变违规:期望解析的 GraphQL 文档。”

】ApolloClient:不能将GraphQL查询作为JS变量传递?“不变违规:期望解析的GraphQL文档。”【英文标题】:ApolloClient:Can\'tpassGraphQLqueryasaJSvariable?"InvariantViolation:ExpectingaparsedGraphQLdocument."【发布时间】:2020-06-1920:37:45【问题描... 查看详情

如何将数据从反应组件传递到 Apollo graphql 查询?

】如何将数据从反应组件传递到Apollographql查询?【英文标题】:HowtopassdatafromreactcomponenttoApollographqlquery?【发布时间】:2018-02-2710:57:42【问题描述】:我正在使用react-native和apollo/graphql开发一个应用程序。在这种情况下,我想做... 查看详情

如何在 GraphQL 突变中使用 FaunaDB 将数组传递给字段

】如何在GraphQL突变中使用FaunaDB将数组传递给字段【英文标题】:HowtoinGraphQLmutationpassanarraytoafieldusingFaunaDB【发布时间】:2021-06-2212:36:45【问题描述】:我不认为我以正确的graphQL方式做事,但我不确定如何在graphQL中实现它。我有... 查看详情

如何允许石墨烯GraphQl中的任何字段过滤

...数。因此,此代码仅适用于wgr_bez:classGraphqlService(graphene.ObjectType):get_single_article= 查看详情

如何将请求正文传递到 GraphQL 上下文中?

】如何将请求正文传递到GraphQL上下文中?【英文标题】:HowtopassrequestbodyintoGraphQLContext?【发布时间】:2016-09-1619:22:02【问题描述】:我目前正在尝试将请求正文放入上下文中,因为正文的一部分包含需要解码的JWT。但是,当我... 查看详情