如何使用GraphQl和Node实现接口



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

这是我的架构文件。

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
})

请帮助我。我在使用接口时做错了什么?

我通过这篇文章找到了答案是否可以使用GraphQllist

从多个表获取数据

任何人,请告诉我使用代码中界面的确切方法。

尽管您打印的错误实际上与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似乎没有识别根对象。如果您在修复界面后仍然存在此问题,请在此处查看此答案

最新更新