Search code examples
amazon-web-servicesgraphqlaws-appsync

makeExecutableSchema of graphql-tools failes with AWSDateTime in the schema


I am using graphql-tools for mocking the response from Appsync and it is failing for schema which has AWSDateTime as datatype of some fields. Following is the error I am getting:

Uncaught Error: Unknown type "AWSDateTime".

Unknown type "AWSDateTime".

Unknown type "AWSDateTime".

And this is code for which it is failing:

import { SchemaLink } from "apollo-link-schema";
import { makeExecutableSchema, addMockFunctionsToSchema } from "graphql-tools";

const typeDefs = `
type Dates {
    createdAt: AWSDateTime
    updatedAt: AWSDateTime
}

type Query {
    getDates(id: ID!): Dates
}`;
const schema = makeExecutableSchema({ typeDefs });

Any idea how can I fix this issue? I know AWSDateTime is scalar type defined specially for appsync, so it may not work. But is there any workaround. With ApolloLink client, it works just fine.


Solution

  • Every scalar you use, with the exception of the 5 built-in scalars, has to be explicitly defined inside your schema. This is a two-step process:

    First, add the type definition:

    scalar AWSDateTime
    

    Second, provide a GraphQLScalarType object, which encapsulates the parsing and serialization logic of the scalar. With makeExecutableSchema, this is provided through the resolver map.

    const resolvers = {
      ...
      AWSDateTime: new GraphQLScalarType({ ... }),
    }
    

    See the docs for additional details. If the serialization and parsing logic doesn't really matter because this is just for mocking anyway, then you could use the methods of an existing scalar, like String.

    const resolvers = {
      ...
      AWSDateTime: new GraphQLScalarType({
        name: 'AWSDateTime',
        parseValue: GraphQLString.parseValue,
        parseLiteral: GraphQLString.parseLiteral,
        serialize: GraphQLString.serialize,
      }),
    }