如何使用 GraphQL 和 node 实现接口

Posted

技术标签:

【中文标题】如何使用 GraphQL 和 node 实现接口【英文标题】:How to implement interface using GraphQL and node 【发布时间】:2019-08-06 03:54:36 【问题描述】:

我想在另一种对象类型中实现一种对象类型的字段

这是我的架构文件。

const Films = new GraphQLInterfaceType(
  name: 'films',
  fields: () => (
    id:
        type: GraphQLID
      ,
      name: 
        type: GraphQLString,
      ,
    )
)

const MovieStream = new GraphQLObjectType(
    name: 'MovieStream',
    interfaces: () => [Films], 
    fields: () => (
        id: 
            type: GraphQLID,    
          ,    
          movie_id: 
              type: GraphQLString,   
            ,    
    )
)

这里我尝试使用界面。但它显示错误:


  "errors": [
    
      "message": "Query root type must be Object type, it cannot be  __validationErrors: undefined, __allowedLegacyNames: [], _queryType: undefined, _mutationType: undefined, _subscriptionType: undefined, _directives: [@include, @skip, @deprecated], astNode: undefined, extensionASTNodes: undefined, _typeMap:  __Schema: __Schema, __Type: __Type, __TypeKind: __TypeKind, String: String, Boolean: Boolean, __Field: __Field, __InputValue: __InputValue, __EnumValue: __EnumValue, __Directive: __Directive, __DirectiveLocation: __DirectiveLocation, films: films, ID: ID, Date: Date, JSON: JSON, MovieStream: MovieStream , _possibleTypeMap: , _implementations:  films: []  ."
    ,
    
      "message": "Expected GraphQL named type but got:  __validationErrors: undefined, __allowedLegacyNames: [], _queryType: undefined, _mutationType: undefined, _subscriptionType: undefined, _directives: [@include, @skip, @deprecated], astNode: undefined, extensionASTNodes: undefined, _typeMap:  __Schema: __Schema, __Type: __Type, __TypeKind: __TypeKind, String: String, Boolean: Boolean, __Field: __Field, __InputValue: __InputValue, __EnumValue: __EnumValue, __Directive: __Directive, __DirectiveLocation: __DirectiveLocation, films: films, ID: ID, Date: Date, JSON: JSON, MovieStream: MovieStream , _possibleTypeMap: , _implementations:  films: []  ."
    
  ]

这里是查询类型:

const QueryRoot = new GraphQLObjectType(

  name: 'Query',
  fields: () => (
        getContentList:
        type: new GraphQLList(contentCategory),
        args: 
          id: 
            type: GraphQLInt
          ,
          permalink: 
              type: GraphQLString
          ,
          language: 
              type: GraphQLString
          ,
          content_types_id: 
              type: GraphQLString
          ,
          oauth_token:
              type: GraphQLString
          

        ,

        resolve: (parent, args, context, resolveInfo) => 
           var category_flag = 0;
           var menuItemInfo = '';
           user_id = args.user_id ? args.user_id : 0;
          // console.log("context"+context['oauth_token']);
           return AuthDb.models.oauth_registration.findAll(attributes: ['oauth_token', 'studio_id'],where:
              //  oauth_token:context['oauth_token'],
               $or: [
                  
                      oauth_token: 
                      
                          $eq: context['oauth_token']
                      
                  ,

                  
                      oauth_token: 
                      
                          $eq: args.oauth_token
                      
                  ,
              ]

              ,limit:1).then(oauth_registration =>  
              var oauthRegistration = oauth_registration[0]
              // for(var i = 0;i<=oauth_registration.ength;i++)
                      if(oauth_registration && oauthRegistration && oauthRegistration.oauth_token == context['oauth_token'] || oauthRegistration.oauth_token == args.oauth_token)
                         studio_id = oauthRegistration.studio_id;
                         return joinMonster.default(resolveInfo,, sql => 
                            return contentCategoryDb.query(sql).then(function(result)  

                                return result[0];
                            );   
                         ,dialect: 'mysql');

                      else
                          throw new Error('Invalid OAuth Token');
                      

                  )

        ,

        where: (filmTable, args, context) => 
            return getLanguage_id(args.language).then(language_id=>
                return ` $filmTable.permalink = "$args.permalink" and $filmTable.studio_id = "$studio_id" and ($filmTable.language_id = "$language_id"  OR  $filmTable.parent_id = 0 AND $filmTable.id NOT IN (SELECT $filmTable.parent_id FROM content_category WHERE $filmTable.permalink = "$args.permalink" and $filmTable.language_id = "$language_id" and $filmTable.studio_id = "$studio_id"))`
            )

              ,

    

  )
)

module.exports = new GraphQLSchema(
    query: QueryRoot
)

请帮帮我。是不是我在界面的使用上做错了什么?

我通过这篇文章找到了答案 Is it possible to fetch data from multiple tables using GraphQLList

任何人请告诉我在我的代码中使用接口的确切方法。

【问题讨论】:

【参考方案1】:

虽然您打印的错误与interfaces 实现无关,但为了让您使用接口,您必须实现接口引用的方法/类型。因此,在您的情况下,您的对象MovieStream 缺少您在对象Films 中引用的类型name

您的代码应该类似于:

const Films = new GraphQLInterfaceType(
    name: 'films',
    fields: () => (
        id:
            type: GraphQLID
        ,
        name: 
            type: GraphQLString,

        ,
    )
)

const MovieStream = new GraphQLObjectType(
    name: 'MovieStream',
    interfaces: () => [Films],
    fields: () => (
        id: 
            type: GraphQLID,
        ,
        name: 
            type: GraphQLString // You're missing this!
        ,
        movie_id: 
            type: GraphQLString,
        ,
    )
)

现在回到你打印的错误"message": "Query root type must be Object type, it cannot be...

这似乎与您的QueryRoot 对象有关,似乎GraphQLSchema 无法识别根对象。如果修复界面后此问题仍然存在,请查看此答案here

【讨论】:

我已经发布了另一个与此相关的错误,请检查***.com/questions/55179820/… @Upasana 的答案是否有助于解决这个问题? 请查看另一个帖子。实际上这是同一个相关问题 @Upasana,你的问题(这里)是关于在 GraphQL 中使用接口的,这回答了它。因此,如果这有助于解决此问题,请将答案标记为已接受,因为它用于帮助可能有相同问题的其他人。

以上是关于如何使用 GraphQL 和 node 实现接口的主要内容,如果未能解决你的问题,请参考以下文章

是否可以在 GraphQL 中实现多个接口?

如何使用 apollo / graphql 实现节点查询解析器

graphql prisma node js postgresql如何将创建日期/更新字段添加到graphql类型?

如何使用 GraphQL 和 ApolloStack 解析联合/接口字段

如何从 Node.js/Express 服务器调用 GraphQL API?

Node.js - 如何使用 graphql 获取关联的模型数据?