• Docs
  • Usage
  • Apollo Server

Usage with Apollo Server

import { ApolloServer } from 'apollo-server'
// import all scalars and resolvers from graphql-scalars
import {
  typeDefs as scalarTypeDefs,
  resolvers as scalarResolvers,
} from 'graphql-scalars'
// Alternatively, import individual scalars and resolvers
// import { DateTimeResolver, DateTimeTypeDefinition, ... } from "graphql-scalars"
// Example import of your own defined resolvers
import { resolvers } from "./src/resolvers"
// Example importing of your own defined type definitions
const typeDefs = fs.readFileSync(
  path.join(path.resolve(), "./src/schema.graphql"),
  "utf-8"
)
 
const server = new ApolloServer({
  typeDefs: [
    // use spread syntax(since typeDefs from `graphql-scalars` come as an array) to add all `grahql-scalars` scalar definitions to your typeDefs array
    ...scalarTypeDefs,
    //Or you can add individual scalar definitions to your typeDefs array like this
    DateTimeTypeDefinition,
    // then add your own defined type definitions to your typeDefs array like this(without using spread syntax since this is usually a single string, unless otherwise)
    typeDefs
  ],
  resolvers: {
    // use spread syntax to add all scalar resolvers(from grahql-scalars) to your resolver map
    ...scalarResolvers,
    // Or you can add individual scalar resolvers to your resolver map like this
    DateTimeResolver,
    // then use spread syntax to add your own defined resolvers to your resolver map like this
    ...resolvers
  }
})
 
server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`)
})
Last updated on October 10, 2022