javascript - GraphQL Schema 缺少名称
问题描述
我已经看过类似的问题,例如this和this,但我没有成功解决我的问题。
我正在尝试使用 GraphQL 来访问 github gist api。我想返回几个字段,例如created_at
,url
和files
. 该files
属性是一个对象,在尝试进行调用时,在将文件作为请求的属性包含以使用gistsByUser
orgistById
端点返回时出现此错误:
One of the provided types for building the Schema is missing a name.
这是我的设置:
服务器/index.js:
require('dotenv').config();
const express = require('express');
const graphql = require('graphql');
const cors = require('cors');
const { graphqlHTTP } = require('express-graphql');
const { GraphQLSchema } = graphql;
const { query } = require('./schema/query');
const { mutation } = require('./schema/mutation');
const { PORT } = process.env;
const schema = new GraphQLSchema({
query,
mutation
});
let app = express();
app.use(cors());
app.use('/api', graphqlHTTP({
schema: schema,
graphiql: true,
}));
app.listen(PORT, () =>
console.log(`GraphQL server running on localhost:${PORT}`)
);
架构/query.js
const { connection } = require('../db');
const { GraphQLObjectType, GraphQLID , GraphQLList, GraphQLString} = require("graphql");
const { FavoriteType } = require('./types');
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
type: "Query",
fields: {
favorite: {
type: new GraphQLList(FavoriteType),
args: { id: { type: GraphQLID } },
resolve() {
const query = "SELECT * from favorites;";
return connection
.any(query)
.then(res => res)
.catch(err => err);
}
},
gistsByUser: {
type: new GraphQLList(FavoriteType),
args: { username: { type: GraphQLString } },
async resolve(parentVal, args) {
let res = await axios.get(`https://api.github.com/users/${args.username}/gists`);
let data = res.data;
return data;
}
},
gistById: {
type: FavoriteType,
args: { id: { type: GraphQLString } },
async resolve(parentVal, args) {
let res = await axios.get(`https://api.github.com/gists/${args.id}`);
let data = res.data;
return data;
}
}
}
});
exports.query = RootQuery;
架构/types.js
const graphql = require("graphql");
const { GraphQLObjectType, GraphQLString, GraphQLBoolean, GraphQLInt, GraphQLList } = graphql;
const FileType = new GraphQLObjectType({
name: "File",
fields: {
file: {
filename: { type: GraphQLString },
type: { type: GraphQLString },
language: { type: GraphQLString },
raw_url: { type: GraphQLString },
size: { type: GraphQLInt }
}
}
})
const FavoriteType = new GraphQLObjectType({
name: "Favorite",
type: "Query",
fields: {
url: { type: GraphQLString },
forks_url: { type: GraphQLString },
commits_url: { type: GraphQLString },
id: { type: GraphQLString },
files: { type: FileType },
git_pull_url: { type: GraphQLString },
git_push_url: { type: GraphQLString },
html_url: { type: GraphQLString },
created_at: { type: GraphQLString },
description: { type: GraphQLString }
}
});
exports.FavoriteType = FavoriteType;
解决方案
推荐阅读
- cordova - 在 Cordova 手机应用程序中保持数据记录功能需要什么
- angular - 在 jhipster 生成的 Angular 应用中加载外部 js 文件
- git - 删除一些包含数据文件的最旧提交
- sql - 如何列出所有未注释的 PostgreSQL 列?
- java - 属性的杰克逊继承
- reactjs - 使用 ReactJS / Reactstrap 的 DropdownMenu 和 DropdownItem onClick
- excel - 函数 if 和 elseif 来实现 ".Body"
- animation - Threejs将对象保存到带有动画的json
- python - QGIS渲染shapefile时如何让Python等待
- python-3.x - 如何打开 VSC 网易音乐?