Prisma1: Typescript issue with resolver-forwarding

Created on 28 May 2018  Â·  14Comments  Â·  Source: prisma/prisma1

Bug Report

I just did the example (https://github.com/prismagraphql/prisma/tree/master/examples/resolver-forwarding) in typescript but I got a compilation error.

Current behavior

TSError: ⨯ Unable to compile TypeScript
src/index.ts (62,34): Argument of type '{ context: { db: Prisma; }; resolvers: { Query: { user: (parent: any, args: any, context: any, in...' is not assignable to parameter of type 'Props'.
  Types of property 'resolvers' are incompatible.
    Type '{ Query: { user: (parent: any, args: any, context: any, info: GraphQLResolveInfo) => any; }; }' is not assignable to type 'IResolvers'.
      Property 'Query' is incompatible with index signature.
        Type '{ user: (parent: any, args: any, context: any, info: GraphQLResolveInfo) => any; }' is not assignable to type '(() => any) | IResolverObject | GraphQLScalarType'.
          Type '{ user: (parent: any, args: any, context: any, info: GraphQLResolveInfo) => any; }' is not assignable to type 'GraphQLScalarType'.
            Property 'name' is missing in type '{ user: (parent: any, args: any, context: any, info: GraphQLResolveInfo) => any; }'. (2345)
    at getOutput (/Users/gauthierrodaro/Projects/axio/axio/server/node_modules/ts-node/src/index.ts:330:15)
    at Object.compile (ts-node/src/index.ts:518:11)
    at Module.m._compile (/src/index.ts:403:43)
    at Module._extensions..js (module.js:646:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/src/index.ts:406:12)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at Object.<anonymous> (ts-node/src/bin.ts:145:12)
error Command failed with exit code 1.

Reproduction
This is the index.ts

import { GraphQLServer } from "graphql-yoga"
import { forwardTo, Prisma } from "prisma-binding"

const getPrismaInstance = () => {
  return new Prisma({
    endpoint: "http://localhost:4466",
    typeDefs: "./src/generated-schema.graphql"
  })
}

const resolvers = {
  Query: {
    user: forwardTo("db")
  }
}

const server = new GraphQLServer({
  context: {
    db: getPrismaInstance()
  },
  resolvers,
  typeDefs: "./src/schema.graphql"
})

/* tslint:disable-next-line:no-console */
server.start(() => console.log("Server is running on http://localhost:4000"))

Expected behavior?
No compilation error

bu2-confirmed bu0-needs-info

Most helpful comment

I don't know if this helps anyone but I managed to fix it by explicitly declaring i was passing a Props object when creating my GraphQLServer like:

const server = new GraphQLServer(<Props> {
    typeDefs: './src/schema.graphql',
    resolvers: resolvers,
    context: req => {
        const prisma = new Prisma({
            endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env)
            secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env)
            debug: true, // log all GraphQL queries & mutations
        });
        // other code
    },
});

All 14 comments

Same issue here! This is what I described in slack a while ago. Thanks for the reproduction!

@marktani Any solution? Or can we just not use forwardTo in Typescript?

This should work, will investigate where the error is coming from 🙂

@gauthierrodaro : Thanks for raising the issue, however, I tried to replicate this by combining the resolver-forwarding example with the index.ts provided by you in this issue.

I was not able to reproduce it - https://github.com/divyenduz/resolver-forwarding-ts

Both tsc and ts-node commands compile TS successfully.

@marktani @divyenduz I created a reproduction at https://github.com/moritzmorgenroth/resolver-forwarding-issue-reproduction, let me know if it works for you. @maticzav

@moritzmorgenroth : Still unable to replicate, there are some issues regarding resolving in the reproduction but TS not compiling is not one of them. Can you please provide exact reproduction steps? Thanks!

Hi @divyenduz, I simply created a new project with the typescript advanced BP from https://github.com/graphql-boilerplates/typescript-graphql-server/tree/master/advanced via graphql create and replaced the default post resolver with resolver forwarding, modifying the schema and the query file. I edited the readme in the rep to show my error, which is the same as the original issue. Can you really not reproduce this?

BTW, setting "strictFunctionTypes": false in tsconfig.json resolves the issue temporarily. Do you maybe have this config in your typescript config locally?

@moritzmorgenroth providing the actual code instead of asking how we are still unable to reproduce the bug would be a lot more helpful and could benefit us all greatly. Could you share the actual reproduction code or is that not an option?

@divyenduz @maticzav I created a reproduction repo at https://github.com/gauthierrodaro/resolver-forwarding-ts.

@divyenduz I didn't see any build or start command using typescript in your repo... I guess you were using globally installed packages which doesn't help for reproduction ;)

I hope this will help!

@maticzav I created a reproduction in a repo, see post above (I pinged you in it) . Here it is: https://github.com/moritzmorgenroth/resolver-forwarding-issue-reproduction Whats wrong with it?

@moritzmorgenroth : In this reproduction, the typescript compiles properly for me and I am not able to reproduce the reported issue with it. Do you have a series of steps I can follow with this repo to reproduce it?

I don't know if this helps anyone but I managed to fix it by explicitly declaring i was passing a Props object when creating my GraphQLServer like:

const server = new GraphQLServer(<Props> {
    typeDefs: './src/schema.graphql',
    resolvers: resolvers,
    context: req => {
        const prisma = new Prisma({
            endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env)
            secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env)
            debug: true, // log all GraphQL queries & mutations
        });
        // other code
    },
});

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hoodsy picture hoodsy  Â·  3Comments

dohomi picture dohomi  Â·  3Comments

marktani picture marktani  Â·  3Comments

ragnorc picture ragnorc  Â·  3Comments

thomaswright picture thomaswright  Â·  3Comments