Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript GraphQL错误:应为未定义的GraphQL类型_Javascript_Graphql_Apollo Server - Fatal编程技术网

Javascript GraphQL错误:应为未定义的GraphQL类型

Javascript GraphQL错误:应为未定义的GraphQL类型,javascript,graphql,apollo-server,Javascript,Graphql,Apollo Server,在过去的一天左右,我一直遇到这个GraphQL错误,我很难找出问题所在。我希望以下是有意义的 在我的项目中,我有以下GraphQL类型;问卷&问题是一份问卷有很多问题,一个问题属于一份问卷 我有以下查询:getQuestions(获取所有问卷),getQuestions(通过Id获取单个问卷),getQuestions(通过指定questionnaireId获取所有问题,或者获取系统中的所有问题或问卷的问题) 这是问卷类型(为了简洁起见,我省略了一系列属性): 如您所见,问卷类型的questio

在过去的一天左右,我一直遇到这个GraphQL错误,我很难找出问题所在。我希望以下是有意义的

在我的项目中,我有以下GraphQL类型;问卷&问题是一份问卷有很多问题,一个问题属于一份问卷

我有以下查询:
getQuestions
(获取所有问卷),
getQuestions
(通过Id获取单个问卷),
getQuestions
(通过指定
questionnaireId
获取所有问题,或者获取系统中的所有问题或问卷的问题)

这是问卷类型(为了简洁起见,我省略了一系列属性):

如您所见,问卷类型的
questions
属性只调用
getQuestions
的解析器

这是
getQuestions
的解析器:

import {
  GraphQLList,
  GraphQLID
} from 'graphql'

import QuestionType from '../../type/Question'

export default {
  type: GraphQLList(QuestionType),
  description: 'Returns a list of questions',
  args: {
    questionnaireId: {
      type: GraphQLID,
      description: 'Questions that belong to this questionnaire',
    },
  },
  async resolve(source: any, { questionnaireId = undefined }: any, ctx: any): Promise<any> {
    // Based on the `questionnaireId` get the questions. If `undefined` get "all" questions.
  },
}
好的。到现在为止,一直都还不错。这段代码编译并运行

但是,此代码有一个问题。
getQuestions
解析器将问卷Id作为参数(
questionnaireId
)。如果问卷Id未通过(
未定义
),它只会返回系统中的所有问题。这意味着在问卷类型中,在
questions
属性上,我们不希望直接调用
getQuestions
解析程序,但我们希望将其封装在一个解析程序中,该解析程序提取
source.id
source
在此上下文中是问卷)并将其作为参数传递给
getQuestions
解析器。如果我们不这样做,那么查询问卷上的问题将始终返回系统中的所有问题,这是我们不想要的

因此,我们在调用
getQuestions
resolver之前,将解析器包装在一个解析器中,该解析器提取问卷Id。我们的调查问卷类型现在如下所示:

import {
  GraphQLID,
  GraphQLObjectType,
  GraphQLNonNull,
} from 'graphql'
import QuestionType from './Question'
import QuestionResolver from '../resolver/question'

const QuestionnaireType: GraphQLObjectType = new GraphQLObjectType({
  name: 'Questionnaire',
  fields() {
    return {
      id: {
        type: new GraphQLNonNull(GraphQLID),
      },

      // other literal attributes go here

      questions: {
        type: GraphQLList(QuestionType),
        async resolve(source: any, args: any, ctx: any): Promise<any> {
          return QuestionResolver.query.getQuestions.resolve(
            source,
            {
              questionnaireId: source.questionnaireId,
            },
            ctx
          )
        },
      },
    }
  },
})

export default QuestionnaireType
我被难住了

getQuestions
的第11行是:

08: export default {
09:   type: GraphQLList(QuestionType),
10:   description: 'Returns a list of questions',
11:   args: {
12:     questionnaireId: {
13:       type: GraphQLID,
14:       description: 'Questions that belong to this questionnaire',
15:     },
我想我已经尝试了所有想到的方法来找到问题。我的项目比较大,我在其他几个地方使用了这种技术,没有问题,但由于某种原因,在这个特定的场景中,它无法编译

目前我唯一的解决方法是在
getQuestions
resolver中使用类似的方法:

import {
  GraphQLList,
  GraphQLID
} from 'graphql'

import QuestionType from '../../type/Question'

export default {
  type: GraphQLList(QuestionType),
  description: 'Returns a list of questions',
  args: {
    questionnaireId: {
      type: GraphQLID,
      description: 'Questions that belong to this questionnaire',
    },
  },
  async resolve(source: any, { questionnaireId = undefined }: any, ctx: any): Promise<any> {

    // The workaround
    const id = questionnaireId || source.id
  },
}
导入{
图表列表,
GraphQLID
}来自“graphql”
从“../../type/Question”导入QuestionType
导出默认值{
类型:GraphQList(问题类型),
description:'返回问题列表',
args:{
问题奈里德:{
类型:GraphQLID,
描述:“属于此问卷的问题”,
},
},
异步解析(source:any,{questionnaireId=undefined}:any,ctx:any):承诺{
//解决办法
const id=questionnaireId | | source.id
},
}
但是,
getQuestions
解析程序不负责查看
源代码
以提取参数,而且,
源代码
在技术上可能是另一个对象,而不是问卷调查,从而增加了
getQuestions
解析程序的复杂性


非常感谢您的帮助和见解。

您需要查看编译后的代码以跟踪错误。不过,根据堆栈跟踪,看起来
QuestionType
的计算结果是
undefined
。当您具有循环依赖项(即模块A导入模块B导入模块A等)时,可能会发生这种情况

首先,通常应避免调用冲突解决程序中的其他冲突解决程序。如果存在大量重复,则可以将共享逻辑提取到一个公共函数,该函数可以从两个解析器调用。当我们的解析器包含业务逻辑时,就会出现这种情况——最好尽量保持解析器的精简,将业务逻辑隔离在单独的模型或服务中,然后可以从解析器调用这些模型或服务的方法

如果看不到所有相关的代码,就很难知道如何修复循环依赖关系。解决任何循环依赖关系的保证方法是在
字段
函数中,将导入语句替换为动态
require
s。但是,您可能不会发现该解决方案特别优雅。一个好的第一步可能是合并一些模块——例如,创建一个包含问题类型和所有相关查询和变异字段的单个问题模块

[Node] /Project/api/node_modules/graphql/type/definition.js:91
[Node]     throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL type."));
[Node]     ^
[Node]
[Node] Error: Expected undefined to be a GraphQL type.
[Node]     at assertType (/Project/api/node_modules/graphql/type/definition.js:91:11)
[Node]     at new GraphQLList (/Project/api/node_modules/graphql/type/definition.js:307:19)
[Node]     at Object.GraphQLList (/Project/api/node_modules/graphql/type/definition.js:309:12)
[Node]     at Object.<anonymous> (/Project/api/build/graphql/resolver/question/getQuestions.js:11:21)
[Node]     at Module._compile (internal/modules/cjs/loader.js:936:30)
[Node]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
[Node]     at Module.load (internal/modules/cjs/loader.js:790:32)
[Node]     at Function.Module._load (internal/modules/cjs/loader.js:703:12)
[Node]     at Module.require (internal/modules/cjs/loader.js:830:19)
[Node]     at require (internal/modules/cjs/helpers.js:68:18)
08: export default {
09:   type: GraphQLList(QuestionType),
10:   description: 'Returns a list of questions',
11:   args: {
12:     questionnaireId: {
13:       type: GraphQLID,
14:       description: 'Questions that belong to this questionnaire',
15:     },
import {
  GraphQLList,
  GraphQLID
} from 'graphql'

import QuestionType from '../../type/Question'

export default {
  type: GraphQLList(QuestionType),
  description: 'Returns a list of questions',
  args: {
    questionnaireId: {
      type: GraphQLID,
      description: 'Questions that belong to this questionnaire',
    },
  },
  async resolve(source: any, { questionnaireId = undefined }: any, ctx: any): Promise<any> {

    // The workaround
    const id = questionnaireId || source.id
  },
}