Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

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

Open
truchi opened this issue Mar 20, 2020 · 2 comments

Comments

@truchi
Copy link

truchi commented Mar 20, 2020

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!


  • index.tsx:
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import typeDefs from './schema'
import {
  ApolloClient,
  ApolloProvider,
  InMemoryCache,
} from '@apollo/client'
import { SchemaLink } from 'apollo-link-schema'
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools'

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(
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>,
  document.getElementById('root'),
)
  • schema.ts:
// 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:
// 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
Copy link

I found the problem

you need to declare the variable type:

const authLink ApolloLink: =


@liamross
Copy link

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,
})

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants