gpt4 book ai didi

javascript - GraphQL:Query.type 字段类型必须是输出类型但得到:未定义

转载 作者:搜寻专家 更新时间:2023-10-31 22:44:45 26 4
gpt4 key购买 nike

我正在尝试我的第一个 GraphQL 方法。这是代码:

schema.js:

import {
GraphQLSchema,
GraphQLObjectType,
GraphQLInputObjectType,
GraphQLNonNull,
GraphQLString,
GraphQLBoolean,
GraphQLInt,
GraphQLID,
GraphQLList
} from 'graphql';

import Company from '../../models/Company';

const CompanyType = new GraphQLObjectType({
name: 'Company',
description: 'Company',
fields: {
_id: {
type: new GraphQLNonNull(GraphQLID)
},
name: {
type: GraphQLString
}
}
})


const Companies = {
type: CompanyType,
args: {
id: {
name: 'ID',
type: new GraphQLNonNull(GraphQLID)
}
},
resolve(root, params) {
return Company.find(params.id).exec();
}
}


export default new GraphQLSchema({

query: new GraphQLObjectType({
name: 'Query',
fields: Companies
})
});

然后在我的 server.js 上:

import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import morgan from 'morgan';
import graphqlHTTP from 'express-graphql';

import schema from './schema';

mongoose.Promise = global.Promise;

// set up example server
const app = express();
app.set('port', (process.env.API_PORT || 3001));


// logger
app.use(morgan('dev'));

// parse body
app.use(bodyParser.json());

// redirect all requests to /graphql
app.use(function redirect(req, res) {
res.redirect('/graphql');
});


app.use('/graphql', graphqlHTTP({
schema: schema,
graphqli: true,
pretty: true
}));

我收到以下错误:

D:\test\node_modules\graphql\jsutils\invariant.js:19
throw new Error(message);
^

Error: Query.type field type must be Output Type but got: undefined.
at invariant (D:\test\node_modules\graphql\jsutils\invariant.js:19:11)
at D:\test\node_modules\graphql\type\definition.js:361:29
at Array.forEach (native)
at defineFieldMap (D:\test\node_modules\graphql\type\definition.js:352:14)
at GraphQLObjectType.getFields (D:\test\node_modules\graphql\type\definition.js:306:44)
at typeMapReducer (D:\test\node_modules\graphql\type\schema.js:206:25)
at Array.reduce (native)
at new GraphQLSchema (D:\test\node_modules\graphql\type\schema.js:95:34)
at Object.<anonymous> (D:/9. DEV/WORKSPACE/mom/client/graphql/index.js:62:16)
at Module._compile (module.js:570:32)
at loader (D:\test\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (D:\test\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)

最佳答案

在您的查询类型定义中,您写道:

fields: Companies

应该是

fields: { Companies }

查看 docs , fields 采用一个对象或一个返回对象的函数,该对象的键映射到该类型中所有字段的名称(在这种情况下是您架构中的所有查询)。

此外,为了清楚起见,由于查询和变更是它们各自类型的字段,因此最好以驼峰命名而不是 PascalCase 命名它们(即 { companies: Companies }

关于javascript - GraphQL:Query.type 字段类型必须是输出类型但得到:未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44980989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com