I have successfully run my both front in nextJs and also run server stepzen start
command
here is my serverClient.ts
file
import {
ApolloClient,
DefaultOptions,
HttpLink,
InMemoryCache,
} from "@apollo/client";
const defaultOptions: DefaultOptions = {
watchQuery: {
fetchPolicy: "no-cache",
errorPolicy: "ignore",
},
query: {
fetchPolicy: "no-cache",
errorPolicy: "all",
},
mutate: {
errorPolicy: "all",
fetchPolicy: "no-cache",
},
};
export const serverClient = new ApolloClient({
ssrMode: true,
link: new HttpLink({
uri: process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT,
headers: {
Authorization: `Apikey ${process.env.GRAPHQL_TOKEN}`,
},
fetch: typeof fetch !== "undefined" ? fetch : undefined,
}),
cache: new InMemoryCache(),
defaultOptions,
});
here is apolloClient.ts
file:
import {
ApolloClient,
DefaultOptions,
InMemoryCache,
createHttpLink,
} from "@apollo/client";
export const BASE_URL =
process.env.NODE_ENV !== "development"
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
: "http://localhost:3000";
const httpLink = createHttpLink({
uri: `${BASE_URL}/api/graphql`,
});
const defaultOptions: DefaultOptions = {
watchQuery: {
fetchPolicy: "no-cache",
errorPolicy: "ignore",
},
query: {
fetchPolicy: "no-cache",
errorPolicy: "all",
},
mutate: {
errorPolicy: "all",
fetchPolicy: "no-cache",
},
};
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
defaultOptions: defaultOptions,
});
export default client;
here is my mutations:
import { gql } from "@apollo/client";
export const CREATE_CHATBOT = gql`
mutation CreateChatbot(
$clerk_user_id: String!
$name: String!
$created_at: timestamptz!
) {
insertChatbots(
clerk_user_id: $clerk_user_id
name: $name
created_at: $created_at
) {
id
name
created_at
}
}
`;
and here is handle
where I have a simple input filed that I want to save my data inside neondb
database and I have called this api using IBM API connection essential
const { user } = useUser();
const [name, setName] = React.useState("");
const router = useRouter();
const [createChatbot, { data, loading, error }] = useMutation(
CREATE_CHATBOT,
{
variables: {
clerk_user_id: user?.id,
name: name,
created_at: new Date().toISOString(),
},
}
);
const handleSubmit = async (e: FormEvent) => {
e.preventDefault();
try {
console.log("Creating Chatbot...");
const data = await createChatbot();
setName("");
router.push(`/edit-chatbot/${data.data.insertChatbots.id}`);
} catch (error) {
console.error(error);
}
};
It’s look like I have done everything correctly, how it’s not save any data in the database always show me an error like this ApolloError: Server response was missing for query 'CreateChatbot'