0

第一次使用 Hot Chocolate 和 Apollo,我遇到了一个问题,即 Apollo 客户端创建的 Hot Chocolate API 总是返回 404。该 API 在 localhost 上运行,可以正常查询。

奇怪的是,如果我使用 Apollo Client getServerSideProps,查询返回正常。

EG 这是有效的

export async function getServerSideProps() {
const { data } = await client.query({
    query: gql`
    query {
        rolesQuery {
            roles {
                id
                name
            }
        }
    }
  `,
});

return {
    props: { roles: data.rolesQuery.roles, },
};
}

这不

export function createApolloClient() {
const httpLink = createHttpLink({
    uri: process.env.NEXT_PUBLIC_GRAPHQL_API,
    fetchOptions: {
        mode: 'no-cors',
    },
});

const client = new ApolloClient({
    link: httpLink,
    cache: new InMemoryCache(),
});

return client;
}

// Usage of the client
const apolloClient = createApolloClient();
<ApolloProvider client={apolloClient}>...</ApolloProvider>

起初我遇到了一个 CORS 问题,该问题已修复,但当从组件内的挂钩调用它时(使用 graphql-codegen 2.6.1 版),我仍然得到 404。

有任何想法吗?

编辑 前端组件调用突变

import React from "react";
import { gql, useMutation } from '@apollo/client';

const REGISTER = gql`
mutation RegisterUser ($email: String!, $password: String!) {
  usersMutation {
    registerUser (request: { email: $email, password: $password}) {
      id
      firstName
      email
    }
  }
}
`;

const SignUp = () => {
    const [registerUser, { data, loading, error }] = useMutation(REGISTER);
    console.log(registerUser())

    return <div>code removed for brevity </div>;
};

export default SignUp;
4

0 回答 0