Skip to content

Commit

Permalink
Update apollo-server.mdx (#1621)
Browse files Browse the repository at this point in the history
Update the examples on how to  correctly add `typeDefs` and `resolvers` from `graphql-scalars` to your schema.
  • Loading branch information
chaiwa-berian authored Oct 10, 2022
1 parent e4431ef commit 7f0f146
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions website/src/pages/docs/usage/apollo-server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@

```js
import { ApolloServer } from 'apollo-server'
import { makeExecutableSchema } from '@graphql-tools/schema'
// import all scalars and resolvers
// import all scalars and resolvers from graphql-scalars
import {
typeDefs as scalarTypeDefs,
resolvers as scalarResolvers
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({
schema: makeExecutableSchema({
typeDefs: [
// use spread syntax to add scalar definitions to your schema
...scalarTypeDefs
// DateTimeTypeDefinition,
// ... other type definitions
],
resolvers: [
scalarResolvers
// DateTimeResolver,
// ... the remainder of the resolver map
]
})
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 }) => {
Expand Down

0 comments on commit 7f0f146

Please sign in to comment.