Graphql-tools: Custom scalars break when being stitched together by mergeSchemas

Created on 22 Nov 2017  路  8Comments  路  Source: ardatan/graphql-tools

Possibly related #454, #486

Tried with the custom Date scalar example from https://www.apollographql.com/docs/graphql-tools/scalars.html#Date-as-a-scalar. It works when the whole schema is created using a single makeExecutableSchema. But when using mergeSchemas it throws value.getTime is not a function.

Launchpad without mergeSchemas that works: https://launchpad.graphql.com/mkwqx4l99
Launchpad with mergeSchemas that doesn't work: https://launchpad.graphql.com/93ql188wr

Not sure if this is expected behavior or a bug.

bug schema stitching v5

Most helpful comment

@freiksenet Just wondering if there is an update on this one? Or is there a work around to use custom scalars with mergeSchemas ? Much appreciated 馃檪

All 8 comments

Thanks for reporting!

For future reference:

So the way it works is that in direct schema merging, we, naturally, want to preserve the scalars coming from subschemas (cause we can and they might be used internally). However, when you execute a subschema, you get the serialized result in your resolver, which is then broken because GraphQL expects a un-serialized result there.

I'll update our tests to reflect that this is broken and think more on how to fix it.

@freiksenet Just wondering if there is an update on this one? Or is there a work around to use custom scalars with mergeSchemas ? Much appreciated 馃檪

Are we sure it's fixed? Here's my DateTime custom scalar:

import {GraphQLScalarType} from "graphql"
import {Kind} from "graphql/language"

const schema = "scalar DateTime"

const resolver = {
  DateTime: new GraphQLScalarType({
    name: "DateTime",
    description: "DateTime type",
    parseValue: (value) => new Date(value),
    serialize: (value) => {
      if(typeof(value) == "number")
        return value
      else
        return value.getTime()
    },
    parseLiteral(ast) {
      if(ast.kind == Kind.INT)
        return parseInt(ast.value, 10)
      return null
    }
  })
}

export default {schema, resolver}

And my mergeSchema call:

const schema = mergeSchemas({
  schemas: [DateTime.schema, auth.schema, orders.schema],
  resolvers: [DateTime.resolver, auth.resolvers, orders.resolvers]
})

But I still get:

Error: No such type: DateTime

Is there some other way I'm supposed to specify custom scalars?

Thanks. Using 2.23.1.

OK, not entirely fixed. If my schema is only:

const schema = `
scalar DateTime
`

then the scalar doesn't appear to be created, and I'm told it exists in the resolvers field but not in the types.

If I have:

const schema = `

scalar DateTime

enum Role {
...

then everything works fine.

I am still running into this issue. Is there any known work around?

Should be fixed in graphql-tools-fork.

https://www.npmjs.com/package/graphql-tools-fork

We recently released an alpha version of GraphQL Tools (#1308) that should fix the issue.

Please update graphql-tools to next or run:

npx match-version graphql-tools next

Let us know if it solves the problem, we're counting for your feedback! :)

Was this page helpful?
0 / 5 - 0 ratings