简体   繁体   中英

Apollo-server-express returning “cannot Get /” instead of the Graphql playground

so I have this code for both my server.js and schema.js from following the apollo doc tutorial https://www.apollographql.com/docs/tutorial/introduction/

server.js

import apollo from "apollo-server-express";
const { ApolloServer } = apollo;
import express from "express";
import typeDefs from "./schema.js";

const app = express();
const server = new ApolloServer({
  typeDefs,
  playground: true,
});

server.applyMiddleware({ app });

const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
  console.log(`operating on port ${PORT}`);
})

schema.js

import pkg from "apollo-server-express"
const { gql } = pkg

// defining schema
 const typeDefs = gql`
  type Launch {
    id: ID!
    site: String
    mission: Mission
    rocket: Rocket
    isBooked: Boolean!
  }
  type Rocket {
    id: ID!
    name: String
    type: String
  }

  type User {
    id: ID!
    email: String!
    trips: [Launch]!
  }

  type Mission {
    name: String
    missionPatch(size: PatchSize): String
  }

  enum PatchSize {
    SMALL
    LARGE
  }
  type Query {
    launches: [Launch]!
    launch(id: ID!): Launch
    me: User
  }
  type Mutation {
    bookTrips(launchIds: [ID]!): TripUpdateResponse!
    cancelTrip(launchId: ID!): TripUpdateResponse!
    login(email: String): String # login token
  }
  type TripUpdateResponse {
    success: Boolean!
    message: String
    launches: [Launch]
  }
`
export default typeDefs

only tweak I did was to choose the ES modules method of import/exporting modules over the commonJS way as used in the documentation, which I think shouldn't be a problem, it compiles without errors but instead of getting the graphql playground to see my schema, I get a cannot Get /

I haven't done the resolvers, but at this point, I believe I should see the playground in action already. as per the docs

When using applyMiddleware , if you don't explicitly specify a path, it will be /graphql by default.

This means you'll access your endpoint and the GraphQL Playground interface at localhost:${PORT}/graphql , not localhost:${PORT} (which is what you're doing according to the error message).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM