“响应不成功:收到状态码500”甚至使用空数据源时GraphQL Playground错误

时间:2019-03-06 15:13:27

标签: graphql apollo apollo-server

我正在关注Apollo的完整堆栈教程,每当我尝试将数据源添加到我的Apollo服务器时,操场上都会引发500错误,而没有其他数据可调试。

我尝试将内省添加为true,我删除了与babel有关的所有内容,完全重新导入了我的导入内容,因此所有内容均采用正确的格式。

如果我注释掉dataSources,即使提供空函数,它也可以正常工作。

请帮助我,这真杀了我!

index.js

const {ApolloServer} = require('apollo-server');
const typeDefs = require('./schema');
const resolvers = require('./resolvers');
const BreweryAPI = require('./datasources/brewery');

const server = new ApolloServer({
    typeDefs,
    resolvers,
    dataSources: () => {},
    introspection: true
});

server.listen().then(({ url }) => {
    console.log(` Server ready at ${url}`);
});

schema.js

const {gql} = require('apollo-server');

const typeDefs = gql`
    type Query {
        breweries: [Brewery]!
        #Get a brewery by ID
        brewery(id: ID!): Brewery

        #Get a brewery by name
        breweriesByName(name: String!): [Brewery]

        #Get a list of breweries by state
        breweriesByState(state: String!): [Brewery]

        #Get a list of breweries by tag
        breweriesByTag(tag: String!): [Brewery]
    }

    type Brewery {
        id: ID!
        name: String
        type: String
        street: String
        city: String
        state: String
        postal: String
        country: String
        latitude: String
        longitude: String
        phone: String
        url: String
        tags: [String]
    }

    type User {
        id: ID!
        email: String!
        favorites: [Brewery]
        visited: [Brewery]
        beers: Int
    }

    type RequestResponse {
        errors: [String]
        success: Boolean
    }

    type Mutation {
        #Add a favorite brewery
        addFavorite(id: ID!): RequestResponse

        #Remove a favorite brewery
        removeFavorite(id: ID!): RequestResponse

        #Add a visited brewery
        addVisited(id: ID!): RequestResponse

        #Add number of beers drank at brewery
        addBeers(beers: Int!): Int

        #Login to track your alcoholism
        login(email: String): String
    }
`;

module.exports = typeDefs;

resolvers.js

module.exports = {
    Query: {
        breweries: async (_, __, { dataSources }) => dataSources.breweryAPI.getAllBreweries()
    }
}

package.json

{
  "name": "fullstack-tutorial-server",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "test": "jest",
    "start": "node src/index.js",
    "start:ci": "node src/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "apollo-datasource": "^0.1.3",
    "apollo-datasource-rest": "^0.1.5",
    "apollo-server": "2.2.0-alpha.2",
    "apollo-server-testing": "2.2.0-alpha.2",
    "axios": "^0.18.0",
    "graphql": "^14.0.2",
    "isemail": "^3.1.3",
    "nodemon": "^1.18.4",
    "sequelize": "^4.39.0",
    "sqlite3": "^4.0.3"
  },
  "devDependencies": {
    "apollo-link": "^1.2.3",
    "apollo-link-http": "^1.5.5",
    "jest": "^23.6.0",
    "nock": "^10.0.2",
    "node-fetch": "^2.2.1"
  },
  "jest": {
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/__utils"
    ]
  }
}

我不知道现在还有什么可以尝试的,也没有其他问题可以研究堆栈溢出

1 个答案:

答案 0 :(得分:0)

const server = new ApolloServer({
    typeDefs,
    resolvers,
    dataSources: () => {},
    introspection: true
});

应该是

const server = new ApolloServer({
    typeDefs,
    resolvers,
    dataSources: () => ({}),
    introspection: true
});

奇怪的语法,但是会导致提到错误,不知道为什么!