Next.js Discord

Discord Forum

Isn't this how we setup the Graph QL opening point in NEXT JS ?

Answered
Pigeon tremex posted this in #help-forum
Open in Discord
Pigeon tremexOP
I go to /api/graphql, it shows 404 page

10 Replies

@Pigeon tremex I go to /api/graphql, it shows 404 page
route handler need to be named in route.ts
so if you want the url to be /api/graphql, you have to create it in src/app/api/graphql/route.ts
@Ray route handler need to be named in `route.ts` so if you want the url to be `/api/graphql`, you have to create it in `src/app/api/graphql/route.ts`
Pigeon tremexOP
It did work but now I got another sort of error
import { ApolloServer } from '@apollo/server';
import { startServerAndCreateNextHandler } from '@as-integrations/next';
import { gql } from 'graphql-tag';

const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const typeDefs = gql`
  type Query {
    hello: String 
  }
`;

const server = new ApolloServer({
  resolvers,
  typeDefs,
});

export default startServerAndCreateNextHandler(server);

So this will be inside my route.ts but I got an error that says
Detected default export in '/home/Personal Projects/graphql-server/src/app/api/graphql/route.ts'. Export a named export for each HTTP method instead.
 ⨯ No HTTP methods exported in '/homePersonal Projects/graphql-server/src/app/api/graphql/route.ts'. Export a named export for each HTTP method.
Pigeon tremexOP
holly, it worked. What's the logic behind it ?
ohh ohh I get it I get it, so while exporting the name should always match to the http methods, GET POST right ?
@Pigeon tremex holly, it worked. What's the logic behind it ?
next is expecting these function are exporting in route.ts
Answer
@Ray https://nextjs.org/docs/app/api-reference/file-conventions/route
Pigeon tremexOP
ohh ohh oki thanks !! Appreciated ❤️