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

graphql-tools/dist has no exported member 'addMocksToSchema'. #1616

Closed
stephanebruckert opened this issue Jun 8, 2020 · 7 comments
Closed

Comments

@stephanebruckert
Copy link

stephanebruckert commented Jun 8, 2020

I'm trying to follow https://www.apollographql.com/docs/graphql-tools/mocking/ where they do:

import { makeExecutableSchema, addMocksToSchema } from 'graphql-tools';

But I get the following whereas my dependencies look up-to-date?

/node_modules/ts-node/src/index.ts:434
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError:  Unable to compile TypeScript:
server/graphql/index.ts:13:3 - error TS2305: Module '"../../node_modules/graphql-tools/dist"' has no exported member 'addMocksToSchema'.

13   addMocksToSchema,
     ~~~~~~~~~~~~~~~~

    at createTSError (node_modules/ts-node/src/index.ts:434:12)
    at reportTSError (node_modules/ts-node/src/index.ts:438:19)
    at getOutput (node_modules/ts-node/src/index.ts:578:36)
    at Object.compile (node_modules/ts-node/src/index.ts:775:32)
    at Module.m._compile (node_modules/ts-node/src/index.ts:858:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Object.require.extensions.<computed> [as .ts] (sre-smart-scaling-ui/node_modules/ts-node/src/index.ts:861:12)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Module.require (internal/modules/cjs/loader.js:830:19)

Here is my package.json

"dependencies": {
    "@apollo/react-hooks": "^3.1.1",
    "@apollo/react-ssr": "^3.1.1",
    "@types/antd": "^1.0.0",
    "@types/cookie-parser": "^1.4.2",
    "@types/cors": "^2.8.6",
    "@types/express": "^4.17.1",
    "@types/http-status": "^0.2.30",
    "@types/jest": "^24.0.18",
    "@types/jsonwebtoken": "^8.3.4",
    "@types/nock": "^11.1.0",
    "@types/node": "^12.7.5",
    "@types/node-cache": "^4.1.3",
    "@types/react": "^16.9.2",
    "@types/request-promise": "^4.1.44",
    "@types/winston": "^2.4.4",
    "@zeit/next-less": "^1.0.1",
    "@zeit/next-sass": "^1.0.1",
    "antd": "^3.23.3",
    "apollo-boost": "^0.4.4",
    "apollo-server-express": "^2.9.3",
    "chart.js": "^2.9.3",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "dotenv": "^8.1.0",
    "express": "^4.17.1",
    "graphql-import": "^1.0.2",
    "graphql-iso-date": "^3.6.1",
    "http-status": "^1.3.2",
    "isomorphic-unfetch": "^3.0.0",
    "jest": "^24.9.0",
    "jest-snapshot-serializer-function-name": "^1.1.0",
    "jsonwebtoken": "^8.5.1",
    "jwks-rsa": "^1.6.0",
    "less": "^3.10.3",
    "next": "^9.4.4",
    "next-compose-plugins": "^2.2.0",
    "nock": "^11.3.5",
    "node-cache": "^4.2.1",
    "node-sass": "^4.14.1",
    "nodemon": "^1.19.2",
    "react": "16.9.0",
    "react-chartjs-2": "^2.9.0",
    "react-dom": "16.9.0",
    "request-promise": "^4.2.4",
    "ts-jest": "^24.1.0",
    "typescript": "^3.6.3"
  },
  "devDependencies": {
    "@graphql-codegen/cli": "^1.15.1",
    "@graphql-codegen/typescript-operations": "^1.15.1",
    "@graphql-codegen/typescript-react-apollo": "^1.15.1",
    "@graphql-codegen/typescript-resolvers": "^1.15.1",
    "ts-node": "^8.4.1"
  }

thanks

@ardatan
Copy link
Owner

ardatan commented Jun 8, 2020

https://www.apollographql.com/docs/graphql-tools/mocking/ is out-of-date. GraphQL Tools has a new website. You can find the up-to-date documentation there;
https://www.graphql-tools.com/docs/mocking#addmockstoschema

@stephanebruckert
Copy link
Author

stephanebruckert commented Jun 8, 2020

Thanks a lot @ardatan

It looks like I now have a very similar issue to #957 because I'm using apollo-server at the same time. According to your comment here #957 (comment), am I stuck?

/node_modules/ts-node/src/index.ts:434
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError:  Unable to compile TypeScript:
server/graphql/index.ts:36:23 - error TS2322: Type 'typeof AuthDirective' is not assignable to type 'typeof SchemaDirectiveVisitor'.
  Types of property 'visitSchemaDirectives' are incompatible.
    Type '(schema: GraphQLSchema, directiveVisitors: Record<string, typeof SchemaDirectiveVisitor>, context?: Record<string, any>) => Record<...>' is not assignable to type '(schema: GraphQLSchema, directiveVisitors: { [directiveName: string]: typeof SchemaDirectiveVisitor; }, context?: { [key: string]: any; }) => { [directiveName: string]: SchemaDirectiveVisitor[]; }'.
      Types of parameters 'directiveVisitors' and 'directiveVisitors' are incompatible.
        Type '{ [directiveName: string]: typeof SchemaDirectiveVisitor; }' is not assignable to type 'Record<string, typeof SchemaDirectiveVisitor>'.
          Index signatures are incompatible.
            Type 'typeof import("node_modules/apollo-server-core/node_modules/graphql-tools/dist/schemaVisitor").SchemaDirectiveVisitor' is not assignable to type 'typeof import("node_modules/@graphql-tools/utils/SchemaDirectiveVisitor").SchemaDirectiveVisitor'.
              Types of property 'visitSchemaDirectives' are incompatible.
                Type '(schema: GraphQLSchema, directiveVisitors: { [directiveName: string]: typeof SchemaDirectiveVisitor; }, context?: { [key: string]: any; }) => { [directiveName: string]: SchemaDirectiveVisitor[]; }' is not assignable to type '(schema: GraphQLSchema, directiveVisitors: Record<string, typeof SchemaDirectiveVisitor>, context?: Record<string, any>) => Record<...>'.
                  Types of parameters 'directiveVisitors' and 'directiveVisitors' are incompatible.
                    Type 'Record<string, typeof SchemaDirectiveVisitor>' is not assignable to type '{ [directiveName: string]: typeof SchemaDirectiveVisitor; }'.
                      Index signatures are incompatible.
                        Type 'typeof import("node_modules/@graphql-tools/utils/SchemaDirectiveVisitor").SchemaDirectiveVisitor' is not assignable to type 'typeof import("node_modules/apollo-server-core/node_modules/graphql-tools/dist/schemaVisitor").SchemaDirectiveVisitor'.
                          Property 'getDeclaredDirectives' is protected but type 'SchemaDirectiveVisitor<TArgs, TContext>' is not a class derived from 'SchemaDirectiveVisitor'.

36   schemaDirectives: { auth: AuthDirective },
                         ~~~~

    at createTSError (node_modules/ts-node/src/index.ts:434:12)
    at reportTSError (node_modules/ts-node/src/index.ts:438:19)
    at getOutput (node_modules/ts-node/src/index.ts:578:36)
    at Object.compile (node_modules/ts-node/src/index.ts:775:32)
    at Module.m._compile (ts-node/src/index.ts:858:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Object.require.extensions.<computed> [as .ts] (node_modules/ts-node/src/index.ts:861:12)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Module.require (internal/modules/cjs/loader.js:830:19)

@ardatan
Copy link
Owner

ardatan commented Jun 8, 2020

It is due to the conflict between an old version that uses Apollo and our new version. You can create your schema according to our documentation and pass the final schema to ApolloServer.
https://www.graphql-tools.com/docs/legacy-schema-directives/

@stephanebruckert
Copy link
Author

stephanebruckert commented Jun 8, 2020

I think I'm already creating and passing the schema according to your documentation:

import { makeExecutableSchema } from '@graphql-tools/schema';
import { addMocksToSchema } from '@graphql-tools/mock';
import { MockList } from '@graphql-tools/mock';

const typeDefs = importSchema(`${__dirname}/schema.graphql`)

const schema = makeExecutableSchema({
  typeDefs,
  resolvers
})

const mocks = {
  Person: () =>({
    summary: () => new MockList([0, 12]),
  }),
}

addMocksToSchema({
  schema,
  mocks,
  preserveResolvers: true
})

const server = new ApolloServer({
  typeDefs,
  resolvers,
  schemaDirectives: { auth: AuthDirective },
  playground: {
    settings: {
      "request.credentials": "same-origin"
    }
  },
  formatError: err => {
    log.error({ message: `GraphQL Error: ${err.message}`, stack_trace: err.stack });

    // Don't give the specific errors to the client.
    if (err.message === "Not authorized") {
      return new Error("Not authorized");
    }

    // Otherwise return the original error.  The error can also
    // be manipulated in other ways, so long as it's returned.
    return err;
  },
  context: context => {
    const req = context.req as Request;
    const { user } = req;
    return { user };
  }
});

and my schema has:

directive @auth(requires: Role = USER) on OBJECT | FIELD_DEFINITION

@ardatan
Copy link
Owner

ardatan commented Jun 9, 2020

You are creating a schema with makeExecutableSchema then adding mocks with addMocksToSchema and leave it like that so you don't use that GraphQLSchemaobject in ApolloServer. schemaDirectives are defined in ApolloServer so graphql-tools is not used there. Let's use everything related to schema generation phase from graphql-tools not apollo-server. You should do the following changes in this case;

import { makeExecutableSchema } from '@graphql-tools/schema';
import { addMocksToSchema, MockList } from '@graphql-tools/mock';

const typeDefs = importSchema(`${__dirname}/schema.graphql`) // I am not sure where this comes from?

let schema = makeExecutableSchema({
  typeDefs,
  resolvers,
  schemaDirectives: { auth: AuthDirective }, // << This is where `schemaDirectives` belongs to
})

const mocks = {
  Person: () =>({
    summary: () => new MockList([0, 12]),
  }),
}

schema = addMocksToSchema({ // reassign the result, this function returns a new schema
  schema,
  mocks,
  preserveResolvers: true
})

const server = new ApolloServer({
  schema, // << Use generated schema here
  playground: {
    settings: {
      "request.credentials": "same-origin"
    }
  },
  formatError: err => {
    log.error({ message: `GraphQL Error: ${err.message}`, stack_trace: err.stack });

    // Don't give the specific errors to the client.
    if (err.message === "Not authorized") {
      return new Error("Not authorized");
    }

    // Otherwise return the original error.  The error can also
    // be manipulated in other ways, so long as it's returned.
    return err;
  },
  context: context => {
    const req = context.req as Request;
    const { user } = req;
    return { user };
  }
});

@stephanebruckert
Copy link
Author

@ardatan many thanks for your precious help on this, it now runs!

All the best and thank you for your work

@stephanebruckert
Copy link
Author

const typeDefs = importSchema(`${__dirname}/schema.graphql`) // I am not sure where this comes from?

Btw this line came from https://github.com/ardatan/graphql-import and I just saw I can migrate it to graphql-tools 👍

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

No branches or pull requests

2 participants