简体   繁体   中英

Invalid hook calls when calling a graphql-codegen query with typescript + generated core used as a package

I'm using Yarn workspaces with a npm package having the generated code and a mobile package that uses it.

When trying to make a call from an imported query I get an invalid hook call. Using any other hook in that component (ie useState) works fine.

My package.json of my mobile package imports: "@workspace-library/core": "1.0.0",

I have an index.js file at core/src that has:

export * from "./generated/graphql";
export { apollo, useAuth, withAuth };

And I import the queries like this:

import {
  useArticlesQuery
} from "@workspace-library/core";

const SidebarArticles: FunctionComponent<{ props: any }> = (props: any) => {
  const [test, setTest] = useState("false");
  const data = useArticlesQuery({
    fetchPolicy: "no-cache",
    notifyOnNetworkStatusChange: true,
  });

   return (
    <View>
      <Text>Heyhey</Text>
    </View>
  );
};

I can see the useArticlesQuery in my generated/graphql.jsx, just in case:

export function useArticlesQuery(baseOptions) { return ApolloReactHooks.useQuery(ArticlesDocument, baseOptions); }

Environment:

  • OS: Ubuntu
  • "@graphql-codegen/cli": "^1.13.1",
  • "@graphql-codegen/typescript": "^1.13.1",
  • "@graphql-codegen/typescript-operations": "^1.13.1",
  • "@graphql-codegen/typescript-react-apollo": "^1.13.1",
  • Yarn workspaces

Additional context Error in question:

Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
* /home/dogchef/Development/workspace-library/packages/core/node_modules/react/cjs/react.development.js:1589:4 in resolveDispatcher
* /home/dogchef/Development/workspace-library/packages/core/node_modules/react/cjs/react.development.js:1597:29 in useContext
* http://192.168.1.2:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false:181621:41 in useBaseQuery
* /home/dogchef/Development/workspace-library/packages/core/node_modules/@apollo/react-hooks/lib/react-hooks.cjs.js:550:18 in useQuery
* http://192.168.1.2:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false:197883:37 in useArticlesQuery
* components/SidebarArticles.tsx:10:2 in SidebarArticles

I run into the same issue. Using useQuery is working though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM