apollographql / apollo-link

:link: Interface for fetching and modifying control flow of GraphQL requests
https://www.apollographql.com/docs/link/
MIT License
1.44k stars 344 forks source link

apollo-link-schema: Type 'SchemaLink' is missing the following properties from type 'ApolloLink': onError, setOnError #1258

Open truchi opened 4 years ago

truchi commented 4 years ago

Hello,

I am trying to reproduce the README example of apollo-link-schema for mocking purposes and I get the following TypeScript error:

Type 'SchemaLink' is missing the following properties from type 'ApolloLink': onError, setOnError

ApolloClient.d.ts(22, 5): The expected type comes from property 'link' which is declared here on type 'ApolloClientOptions<NormalizedCacheObject>'

for the line:

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: new SchemaLink({ // <--
    schema,
  }),
})

Is this a bug or am I doing something wrong?

Thank you!


const mocks = { Query: () => true, // Noob here Mutation: () => true, }

const schema = makeExecutableSchema({ typeDefs }) addMockFunctionsToSchema({ schema, mocks, })

const client = new ApolloClient({ cache: new InMemoryCache(), link: new SchemaLink({ // <-- Errors here schema, }), })

ReactDOM.render(

, document.getElementById('root'), ) ``` - schema.ts: ```typescript // From https://www.apollographql.com/docs/graphql-tools/generate-schema/#example export default ` type Author { id: Int! firstName: String lastName: String """ the list of Posts by this author """ posts: [Post] } type Post { id: Int! title: String author: Author votes: Int } # the schema allows the following query: type Query { posts: [Post] author(id: Int!): Author } # this schema allows the following mutation: type Mutation { upvotePost(postId: Int!): Post } ` ``` - package.json: ```json5 // Some fields omitted { "dependencies": { "@apollo/client": "^3.0.0-beta.41", // <-- Could that be the issue? "@types/jest": "^24.9.1", "@types/node": "^12.12.28", "@types/react": "^16.9.23", "@types/react-dom": "^16.9.5", "apollo-link-schema": "^1.2.4", // <-- You are here "graphql-tools": "^4.0.7", "react": "^16.13.0", "react-dom": "^16.13.0", "react-scripts": "3.4.0", "typescript": "^3.7.5" }, "devDependencies": { "apollo": "^2.25.0", } } ```
unutoiul commented 4 years ago

I found the problem

you need to declare the variable type:


const authLink ApolloLink: =
liamross commented 4 years ago

Because there isn't enough overlap, simply typing it as ApolloLink won't work. This seems to be an issue with the typing in either apollo-link-schema or @apollo/client. I'm getting the same issue, my temporary fix is to cast to unknown and then ApolloLink:

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: new SchemaLink({ schema }) as unknown as ApolloLink,
})