graphqlapollo-servergraphql-tag

How to use custom graphQL types imported from npm package in apollo's graphql-gql


I don't want to write my own custom scalar types, I want to import them from a library. For example, I want UnixTimestamp from this npm package - gnt

in my typeDefs.js I try something like this

import { gql } from 'apollo-server-express'
import { UnixDate } from 'gnt'

export default gql`
type vacation {
  from: ${UnixDate}
  to: ${UnixDate}
}
/* other types and queries */
`

But it doesn't work. I get Error: Type "UnixDate" not found in document.


Solution

  • Adding UnixDate to a field this way assigns that scalar to that particular field and tells GraphQL how to handle parsing it, but it does not explicitly add the scalar to the schema itself. You still need to add the scalar to the typeDefs, like this:

    export default gql`
      scalar ${UnixDate} /* use template literal placeholder */
    
      type vacation {
        from: ${UnixDate}
        to: ${UnixDate}
      }
      # Other typeDefs
    `