Reputation: 660
I am getting this error when trying to check if apollo graphql is working fine.
Error: You must await server.start()
before calling server.createHandler()
Note : There is a similar question ,but I am not using express Error: You must `await server.start()` before calling `server.applyMiddleware()`
//api/graphql.js
import { ApolloServer } from "apollo-server-micro";
import { resolvers } from "../../apis/resolver";
import { typeDefs } from "../../apis/schemas";
const apolloServer = new ApolloServer({ typeDefs, resolvers });
export const config = {
api: {
bodyParser: false
}
};
export default apolloServer.createHandler({ path: "/api/graphql" });
//resolver
import axios from "axios";
export const resolvers = {
Query: {
getUsers: async () => {
try {
const users = await axios.get("https://api.github.com/users");
return users.data.map(({ id, login, avatar_url }) => ({
id,
login,
avatar_url
}));
} catch (error) {
throw error;
}
},
getUser: async (_, args) => {
try {
const user = await axios.get(
`https://api.github.com/users/${args.name}`
);
return {
id: user.data.id,
login: user.data.login,
avatar_url: user.data.avatar_url
};
} catch (error) {
throw error;
}
}
}
};
//schema
import { gql } from "apollo-server-micro";
export const typeDefs = gql`
type User {
id: ID
login: String
avatar_url: String
}
type Query {
getUsers: [User]
getUser(name: String!): User!
}`
Upvotes: 10
Views: 5927
Reputation: 1957
This works for me, copied from the guys at prisma.
import Cors from "micro-cors";
import schema from "graphql/schema";
import { ApolloServer } from "apollo-server-micro";
import { PageConfig } from "next";
import { createContext } from "graphql/context";
export const config: PageConfig = {
api: {
bodyParser: false,
},
};
const cors = Cors();
const server = new ApolloServer({
context: createContext,
schema,
});
const startServer = server.start();
export default cors(async (req, res) => {
if (req.method === "OPTIONS") {
res.end();
return false;
}
await startServer;
await server.createHandler({ path: "/api/graphql" })(req, res);
});
Upvotes: 22
Reputation: 121
i had the same problem. This is a known bug with an open issue so that for now, you can downgrade apollo-server-micro@^2.
My package.json is now as follows and it works flawlessly.
"dependencies": {
"apollo-server-micro": "2.25.0",
"graphql": "^15.6.1",
"micro": "^9.3.4",
"mongoose": "^6.0.10",
"next": "11.1.2",
"react": "17.0.2",
"react-dom": "17.0.2" }
Upvotes: 2
Reputation: 102672
If you're using an integration package for a non-serverless
framework (like express
, micro
), you must await
a call to start
immediately after creating your ApolloServer
, before attaching it to your web framework and starting to accept requests. This assertStarted() method will assert if the server is started. For more info, see comments of source code
There are some examples, see apollo-server-micro package and this
const { ApolloServer, gql } = require('apollo-server-micro');
const typeDefs = gql`
type Query {
sayHello: String
}
`;
const resolvers = {
Query: {
sayHello(parent, args, context) {
return 'Hello World!';
},
},
};
const apolloServer = new ApolloServer({ typeDefs, resolvers });
module.exports = apolloServer.start().then(() => {
return apolloServer.createHandler({ path: '/data' });
});
Upvotes: 6