Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

TypeScript Compile Type Error while calling introspectSchema with 'apollo-fetch' #170

Open
AdelBachene opened this issue Apr 20, 2018 · 1 comment

Comments

@AdelBachene
Copy link

#I'm Trying to create a remote Schema and Im doing it like this:

import {makeRemoteExecutableSchema, introspectSchema} from 'graphql-tools';
import {createApolloFetch} from 'apollo-fetch';
const fetcher = createApolloFetch({uri: this.uri});
        return makeRemoteExecutableSchema({
            schema: await introspectSchema(fetcher),
            fetcher
        });

and I'm getting this error when I try to run my project

TSError: ⨯ Unable to compile TypeScript
2|SMALA-Ga | server/graphql/proxy/index.ts (18,43): Argument of type '{ schema: any; fetcher: ApolloFetch; }' is not assignable to parameter of type '{ schema: string | GraphQLSchema; link?: ApolloLink; fetcher?: Fetcher; }'.
2|SMALA-Ga | Types of property 'fetcher' are incompatible.
2|SMALA-Ga | Type 'ApolloFetch' is not assignable to type 'Fetcher'. (2345)

the message of the error is clear so when we check accepted parameters by introspectSchema we find ApolloLink | Fetcher only :

export default function introspectSchema(fetcher: ApolloLink | Fetcher, linkContext?: {
    [key: string]: any;
}): Promise<GraphQLSchema>;

but ApolloFetch which is the return type of createApolloFetch is not accepted
export declare function createApolloFetch(params?: FetchOptions): ApolloFetch;

well this issue can be fixed by changing the fetch to ApolloLink for example but personally, I like ApolloFetch due it's flexibility on changing the request parameters --> I mean something like this:

const fetcher = createApolloFetch({uri: this.uri});
        fetcher.use(({ request, options }, next) => {
            if(context.req.get('authorization')) {
                if (!options.headers) {
                    options.headers = {};
                }
                options.headers['Authorization'] = context.req.get('authorization');
                next();
            }

I'm Using TypeScript with ts-node

ts-node v6.0.0
node v9.5.0
typescript v2.8.1

I'm not really sure about supporting typescript in Apollo because I found other typing issues that I will report them in different threads.

@hnq90
Copy link

hnq90 commented Oct 18, 2018

@AdelBachene Use const fetcher = createApolloFetch({uri: this.uri}) as any and the typing error will be disappeared.

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

2 participants