首页 > 解决方案 > GraphQL Schema 缺少名称

问题描述

我已经看过类似的问题,例如thisthis,但我没有成功解决我的问题。

我正在尝试使用 GraphQL 来访问 github gist api。我想返回几个字段,例如created_at,urlfiles. 该files属性是一个对象,在尝试进行调用时,在将文件作为请求的属性包含以使用gistsByUserorgistById端点返回时出现此错误:

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;

标签: javascriptnode.jsgraphqlgraphql-js

解决方案


推荐阅读