5

Now I am trying to use apolloClient with both graphql and rest api in typescript.

Therefore, I applied apollo-link-rest. But I got the error below.

./node_modules/apollo-link-rest/bundle.umd.js Module not found: Can't resolve 'graphql-anywhere/lib/async' in 'D:\forked\syntegrate_app_client\node_modules\apollo-link-rest'

Type 'RestLink' is not assignable to type 'ApolloLink'.
  Types of property 'split' are incompatible.
    Type '(test: (op: import("d:/forked/syntegrate_app_client/node_modules/@apollo/client/link/core/types").Operation) => boolean, left: import("d:/forked/syntegrate_app_client/node_modules/@apollo/client/link/core/ApolloLink").ApolloLink | import("d:/forked/syntegrate_app_client/node_modules/@apollo/client/link/core/types")....' is not assignable to type '(test: (op: import("d:/forked/syntegrate_app_client/node_modules/apollo-link/lib/types").Operation) => boolean, left: import("d:/forked/syntegrate_app_client/node_modules/apollo-link/lib/link").ApolloLink | import("d:/forked/syntegrate_app_client/node_modules/apollo-link/lib/types").RequestHandler, right?: import("d...'.
      Types of parameters 'test' and 'test' are incompatible.
        Types of parameters 'op' and 'op' are incompatible.
          Property 'toKey' is missing in type 'import("d:/forked/syntegrate_app_client/node_modules/@apollo/client/link/core/types").Operation' but required in type 'import("d:/forked/syntegrate_app_client/node_modules/apollo-link/lib/types").Operation'.ts(2322)
types.d.ts(24, 5): 'toKey' is declared here.

This is the code that I used.

const restLink = new RestLink({
    uri: process.env.REST_API
});

const authLink = setContext((_, { headers }) => {
    // get the authentication token from local storage if it exists
    const token = getCookie("token");
    // return the headers to the context so httpLink can read them
    return {
        headers: {
            ...headers,
            STKN: token ? `${token}` : "",
        },
    };
});

const client = new ApolloClient({
    // link: errorLink.concat(restLink).concat(authLink).concat(link),
    link: ApolloLink.from([errorLink, restLink, authLink, link]),
    cache,
    resolvers,
});

enter image description here

and this is my version of @apollo/client

"dependencies": {
        "@apollo/client": "^3.0.0-beta.44",
        "@apollo/link-context": "^2.0.0-beta.3",
     "apollo-link": "^1.2.14",
        "apollo-link-batch-http": "^1.2.13",
        "apollo-link-http": "^1.5.16",
        "apollo-link-rest": "^0.8.0-beta.0",
Pooh
  • 71
  • 1
  • 5

1 Answers1

2

If you want to use apollo-link-rest you will have to use version 0.7.3 and use apollo-client 2.6.10, this fixed the problem for me

The 0.8.0-beta.0 has not been updated in some time, and it's unclear if this will be maintained to work properly with @apollo/client 3

eurobob
  • 221
  • 1
  • 4
  • 18