Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type error in config of ApolloServer. 'resolvers' should accept an array of resolvers #1775

Closed
DimaMukhin opened this issue Oct 6, 2018 · 4 comments · Fixed by #3014
Closed
Labels
🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository. 🧬 typings Relates to TypeScript changes or improvements.

Comments

@DimaMukhin
Copy link

I believe that there is a bug with the type definitions of the "config" interface passed to "ApolloServer".
According to the type definitions of the config interface, the field "resolvers" must be of type "IResolvers". However, an array of resolvers can also be passed, and the server will simply stitch all of the resolvers in the array.
When using JavaScript, this is not a problem since by default there is no type checking. However, when using TypeScript, we will get an error complaining about type miss-match and we won't be able to compile. A quick fix for this bug is to add // @ts-ignore.

Intended outcome

I want to be able to use an array of resolvers when creating an ApolloServer instance with TypeScript.

Actual outcome

ApolloServer does not accept an array of resolvers. As a result, TypeScript will complain about this and will not compile. More information and an error message can be found below.

How to reproduce the issue

Simply pass an array of resolvers when creating an instance of ApolloServer when using TypeScript. See code example below for more details.

Code Example

import * as Koa from 'koa';
import { ApolloServer, gql, IResolvers } from 'apollo-server-koa';
import { DocumentNode } from 'graphql';

const app: Koa = new Koa();

const schema: DocumentNode = gql`
  type Query {
    me: User
    user: User
  }

  type User {
    username: String!
  }
`;

const resolvers: Array<IResolvers> = [{
    Query: {
        me: () => {
            return {
                username: 'Dima Mukhin',
            };
        },
    },
},
{
    Query: {
        user: () => {
            return {
                username: 'I love apollo-server',
            };
        },
    }
}];

// @ts-ignore: 'resolvers' should accept 'Array<IResolvers>'
const server: ApolloServer = new ApolloServer({
    typeDefs: schema,
    resolvers,
});

server.applyMiddleware({ app, path: '/graphql' });
app.listen(3000);
console.log('Server running on port 3000');

The above code will work perfectly fine because we ignore the type checking of the config. However, if we remove // @ts-ignore, the application will no longer compile, and we will get the following error:

[ts]
Argument of type '{ typeDefs: DocumentNode; resolvers: IResolvers<any, any>[]; }' is not assignable to parameter of type 'Config'. Types of property 'resolvers' are incompatible. Type 'IResolvers<any, any>[]' is not assignable to type 'IResolvers<any, any>'. Index signature is missing in type 'IResolvers<any, any>[]'.

Potential Solution

First, in types.ts we change the following line
resolvers?: IResolvers;
becomes:
resolvers?: IResolvers | Array<IResolvers>;
Second, in ApolloServer.ts we change the following line
if (resolvers && !resolvers.Upload) {
becomes:
if (resolvers && !(resolvers instanceof Array) && !resolvers.Upload) {

@ghost ghost added the 🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository. label Oct 6, 2018
@cctv1237
Copy link

Yea, same issue as well

@javiercornejo
Copy link

Hello. Same issue here.

@alex996
Copy link

alex996 commented Apr 7, 2019

Any chance this could get fixed?

@keesvanlierop
Copy link

This used to work for version 1.. Not sure why they dropped support for this?

@abernix abernix added the 🧬 typings Relates to TypeScript changes or improvements. label Jul 4, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository. 🧬 typings Relates to TypeScript changes or improvements.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants