graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

mergeSchemas doesn't allow built in scalars in secondary schemas

Open jsonfry opened this issue 8 years ago • 15 comments

I have the following code:

return mergeSchemas({
    schemas: [actions, typeDefs],
    resolvers,
    schemaDirectives: {
      date: DateFormatDirective,
    },
  });

actions is a schema made using makeRemoteExecutableSchema and typeDefs is a big gql schema string. The actions schema does not contain any use of the built in Float, but typeDefs does.

When you query the actions schema directly:

{
  __type(name:"Float") {
    name
  }
}

you get back

{
  "data": {
    "__type": null
  }
}

rather than

{
  "data": {
    "__type": {
      "name": "Float"
    }
  }
}

This is normal and expected.

However, when the above mergeSchemas function is called, an error is thrown:

     Error: Can't find type Float.
      at /usr/src/app/node_modules/graphql-tools/src/stitching/mergeSchemas.ts:102:13
      at resolveType (node_modules/graphql-tools/src/stitching/schemaRecreation.ts:187:14)
      at /usr/src/app/node_modules/graphql-tools/src/stitching/schemaRecreation.ts:156:18
      at Array.forEach (<anonymous>)
      at fieldMapToFieldConfigMap (node_modules/graphql-tools/src/stitching/schemaRecreation.ts:154:23)
      at fields (node_modules/graphql-tools/src/stitching/schemaRecreation.ts:48:9)
      at resolveThunk (node_modules/graphql/type/definition.js:370:40)
      at defineFieldMap (node_modules/graphql/type/definition.js:532:18)
      at GraphQLObjectType.getFields (node_modules/graphql/type/definition.js:506:44)
      at typeMapReducer (node_modules/graphql/type/schema.js:232:38)
      at typeMapReducer (node_modules/graphql/type/schema.js:213:12)
      at typeMapReducer (node_modules/graphql/type/schema.js:213:12)
      at typeMapReducer (node_modules/graphql/type/schema.js:213:12)
      at /usr/src/app/node_modules/graphql/type/schema.js:239:20
      at Array.forEach (<anonymous>)
      at typeMapReducer (node_modules/graphql/type/schema.js:232:51)
      at /usr/src/app/node_modules/graphql/type/schema.js:239:20
      at Array.forEach (<anonymous>)
      at typeMapReducer (node_modules/graphql/type/schema.js:232:51)
      at Array.reduce (<anonymous>)
      at new GraphQLSchema (node_modules/graphql/type/schema.js:122:28)
      at mergeSchemasImplementation (node_modules/graphql-tools/src/stitching/mergeSchemas.ts:243:22)
      at mergeSchemas (node_modules/graphql-tools/src/stitching/mergeSchemas.ts:75:10)
      ...

It seems like the built in scalar types have to be used in the first schema for them to be valid in any subsequent schemas passed to mergeSchemas.

I'm guessing this isn't intentional functionality :smile:

jsonfry avatar May 30 '18 15:05 jsonfry

I also face similar issue. Luckily, I can get gql definition for every types. So I consolidate all gql(s) and combine it then makeExecutableSchema only once. Do you happen to workaround the issue now ?

NattapongSiri avatar Jun 14 '18 06:06 NattapongSiri

any update on this? I'm able to reproduce this.

despreston avatar Dec 17 '18 22:12 despreston

I'm having the same issue, although in mine, none of the base types is used in the first schema. The first schema only declares new scalar types. The one GraphQL complains about in the second schema is Float.

fluggo avatar Jan 22 '19 21:01 fluggo

Inserting the text:

scalar Float
scalar Int

...in the second schema was enough to get it working.

fluggo avatar Jan 22 '19 21:01 fluggo

Here is a minimal reproduction of the issue:

import { mergeSchemas } from 'graphql-tools';

const BookSchema = `
  type Book {
    name: String
  }
`;

mergeSchemas({ schemas: [BookSchema] });

hayes avatar Feb 08 '19 19:02 hayes

Using makeExecutableSchema will get around this issue. I think the root issue has something to do with the way mergeSchemas parses the schema. Fields of scalar types end up being represented by GraphQLObjectType rather than GraphQLScalarType

hayes avatar Feb 08 '19 20:02 hayes

@hayes you are right, using makeExecutableSchema does fix it

Instead of doing

const mergedSchema = mergeSchemas({
    schemas: [RemoteSchema, LocalSchema],
  });

I'm doing

const mergedSchema = mergeSchemas({
    schemas: [RemoteSchema, makeExecutableSchema({ typeDefs: LocalSchema })],
    resolvers,
  });

Note: For this to work, the resolvers have to be part of the mergeSchemas, they can't be part of the ApolloServer props. One issue with this is with the typings, the resolvers params in mergeSchemas expects a IResolversParameter when the ApolloServer expects an IResolvers . Not sure what's the difference tho

tafelito avatar Feb 22 '19 22:02 tafelito

The problem seems to be fixed in graphql-tools version 5.0.0-rc.0 :tada: .

I hope we can have a stable version soon :smiley:

drawm avatar Mar 20 '19 20:03 drawm

Yes 5.0.0-rc.0 works fine 👍

gaboesquivel avatar Apr 03 '19 00:04 gaboesquivel

Can confirm 5.0.0-rc.0 fixes this, but I'm getting the following issue now:

TypeError: Cannot read property 'getType' of undefined

See my issue here: #1095

anthonykrivonos avatar Apr 03 '19 23:04 anthonykrivonos

Found a "quick n dirty" work around that does the job until fixed. Simply apply a "mock" schema as the 1st element in the array of schemas to be merged.

    // apply a default schema before merging - work around for a bug in mergeSchemas
    const abc = makeExecutableSchema({
      typeDefs: `
      type Query         
      type Mutation
      type Subscription
      type Mock {
          a: Int
          b: Float
          c: String
          d: Boolean
          e: ID
      }
      `,
    });

    return mergeSchemas({
      resolvers,
      schemas: [abc, myRealSchema],
    });

Adding the reference to the types seemed to suppress any issues with GraphQLScalarType being undefined.

There may be a nicer way of doing this (tidyer mocked schema etc), but wanted to post the general jist to help out any stranded devs! :)

JoshKneale avatar Apr 22 '19 19:04 JoshKneale

I am using Hasura and ID type was not working, @JoshKneale workaround solved the issue, but it definitely should not be the definitive solution.

fsc7 avatar Apr 26 '19 10:04 fsc7

Also having this issue with apollo-server-lambda.

tim-white87 avatar Apr 26 '19 21:04 tim-white87

Also having this issue with apollo-server-lambda.

update to 5.0.0-rc.0. It solved the issue for me.

paulrimiru avatar Jun 11 '19 13:06 paulrimiru

Closing as fixed in master, try graphql-tools@next for additional fixes

yaacovCR avatar Mar 30 '20 01:03 yaacovCR