Docs
Usage
Mocks

Using Mocks

GraphQL Scalars provides mock implementations out-of-box that is compatible with GraphQL Tools Mocking. It can be used with ApolloServer like below as well.

Using mocks with GraphQL Tools

import { addMocksToSchema } from '@graphql-tools/mock'
import { typeDefs as scalarsTypeDefs, resolvers as scalarsResolvers, mocks as scalarsMocks } from 'graphql-scalars'
 
const schema = makeExecutableSchema({
  typeDefs: [
    ...scalarsTypeDefs
    // all others typeDefs
  ],
  resolvers: {
    ...scalarsResolvers
    // all others resolvers
  }
})
 
const mockedSchema = addMocksToSchema({
  schema,
  mocks: {
    ...scalarsMocks
    // others mocks
  }
})

Using mocks with Apollo Server

import { ApolloServer } from 'apollo-server'
import { makeExecutableSchema } from '@graphql-tools/schema'
// import all scalars and resolvers
import { typeDefs, resolvers, mocks } from 'graphql-scalars'
// Alternatively, import individual scalars and resolvers
// import { DateTimeResolver, DateTimeTypeDefinition, DateTimeMock, ... } from 'graphql-scalars'
 
const server = new ApolloServer({
  typeDefs: [
    // use spread syntax to add scalar definitions to your schema
    ...typeDefs
    // DateTimeTypeDefinition,
    // ... other type definitions
  ],
  resolvers: {
    // use spread syntax to add scalar resolvers to your resolver map
    ...resolvers
    // DateTimeResolver,
    // ... the remainder of the resolver map
  },
  mocks: {
    // use spread syntax to add scalar resolvers to your resolver map
    ...mocks
    // DateTimeMock,
    // ... other mocks
  }
})
Last updated on July 27, 2022