0

有没有人用两个 graphql 端点成功实现 NextJs,其中 NextJS api 路由是您自己的数据库,另一个是第 3 方 graphQL API?或者我应该忘记 NextJS API 路由并为我自己的数据库制作独立的 GraphQL API。不确定我是否需要对我的 nextjs 路由进行身份验证,因为每个用户都已经通过 Shopify 进行了身份验证。

我的服务器错误:

Error: Cannot use GraphQLSchema "{ __validationErrors: undefined, extensions: undefined, astNode: undefined, extensionASTNodes: [], __allowedLegacyNames: [], _queryType: Query, _mutationType: undefined, _subscriptionType: undefined, _directives: [@skip, @include, @deprecated], _typeMap: { Query: Query, String: String, __Schema: __Schema, __Type: __Type, __TypeKind: __TypeKind, Boolean: Boolean, __Field: __Field, __InputValue: __InputValue, __EnumValue: __EnumValue, __Directive: __Directive, __DirectiveLocation: __DirectiveLocation }, _possibleTypeMap: {}, _implementations: {} }" from another module or realm.

Ensure that there is only one instance of "graphql" in the node_modules
┃ directory. If different versions of "graphql" are the dependencies of other
┃ relied on modules, use "resolutions" to ensure only one version is installed.
┃ 
┃ https://yarnpkg.com/en/docs/selective-version-resolutions
┃ 
┃ Duplicate "graphql" modules cannot be used at the same time since different
┃ versions may have different capabilities and behavior. The data from one
┃ version used in the function from another could produce confusing and
┃ spurious results.

Nextjs API 路由:pages/api/graphql.js

const cors = require("micro-cors")(); // highlight-line
const { ApolloServer, gql } = require("apollo-server-micro");
const knex = require("knex");
const knexfile = require("../../knexfile");

cors({ allowedMethods: ["POST", "OPTIONS"] });

const db = knex(knexfile.development);

const typeDefs = gql`
  type Query {
    todos: [Todo]!
    todo(id: ID!): Todo
  }

  type Todo {
    id: ID!
    description: String!
    done: Boolean!
  }
`;

const resolvers = {
  Query: {
    todos: (_parent, _args, _context) => {
      return db.select("*").from("todos").orderBy("id");
    },
    todo: (_parent, { id }, _context) => {
      return db.select("*").from("todos").where({ id }).first();
    },
  },
};

const apolloServer = new ApolloServer({ typeDefs, resolvers });
module.exports = apolloServer.start().then(() => {
  const handler = apolloServer.createHandler();
  return cors((req, res) =>
    req.method === "OPTIONS" || req.method === "POST"
      ? res.end()
      : handler(req, res)
  );
});


Next.js _app.js

  
  ///My Next.js API Route Path
  const myDatabase = new HttpLink({
    uri: "/api/graphql",
  });

  const thirdParty = new createHttpLink({
    fetch: userLoggedInFetch(app),
    credentials: "include",
    headers: {
      "Content-Type": "application/graphql",
    },
  });

  const client = new ApolloClient({
    link: ApolloLink.split(
      (operation) => operation.getContext().clientName === "thirdParty",
      thirdParty,
      myDatabase
    ),
    cache: new InMemoryCache(),
  });

我检查了node_modules,只看到一个graphql文件夹。

4

0 回答 0