From 7f0f146a1becac1808ae6d47ac3e5531fc51bebc Mon Sep 17 00:00:00 2001 From: Berian Date: Mon, 10 Oct 2022 11:57:31 +0200 Subject: [PATCH] Update apollo-server.mdx (#1621) Update the examples on how to correctly add `typeDefs` and `resolvers` from `graphql-scalars` to your schema. --- .../src/pages/docs/usage/apollo-server.mdx | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/website/src/pages/docs/usage/apollo-server.mdx b/website/src/pages/docs/usage/apollo-server.mdx index 399712bb3..f0b5b16b3 100644 --- a/website/src/pages/docs/usage/apollo-server.mdx +++ b/website/src/pages/docs/usage/apollo-server.mdx @@ -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 }) => {