graphql-server

Go to documentation

Easy GraphQL server implementation with Nuxt

GraphQL Server Toolkit for Nuxt

npm versionnpm downloadsGithub ActionsCodecov

This package allows you to easily develop a GraphQL server in your Nuxt 3 application.

For consuming a GraphQL API on the client-side, we recommend the modules Nuxt Apollo, Nuxt GraphQL Client or nuxt/graphql-client.

Features

  • Provides a virtual module #graphql/schema from where you can import your schema. Under the hood, it automatically merges multiple schema files together into a complete schema. Moreover, you no longer need to worry about deploying schema graphql files.
  • Automatically generated typescript definitions for your resolvers via the virtual module #graphql/resolver.

Installation

# npmnpm install @apollo/server graphql @as-integrations/h3 nuxt-graphql-server# yarnyarn add @apollo/server graphql @as-integrations/h3 nuxt-graphql-server# pnpmpnpm add @apollo/server graphql @as-integrations/h3 nuxt-graphql-server

Usage

  1. Add the module in nuxt.config.ts:
    export default defineNuxtConfig({   modules: [      'nuxt-graphql-server'   ],   graphqlServer: {      // Optional: config   }})̀ ``
  2. Define the GraphQL schema in .graphql files located in the server folder.
  3. Expose the GraphQL API endpoint by creating server/api/graphql.ts with the following content:
    import { Resolvers } from '#graphql/resolver'import { schema } from '#graphql/schema'import { ApolloServer } from '@apollo/server'import { startServerAndCreateH3Handler } from '@as-integrations/h3'const resolvers: Resolvers = {   Query: {      // Typed resolvers   },}const apollo = new ApolloServer({typeDefs: schema, resolvers})export default startServerAndCreateH3Handler(apollo, {   // Optional: Specify context   context: (event) => {...},})

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable (use npm i -g corepack for Node.js < 16.10).
  • Install dependencies using pnpm install --shamefully-hoist.
    • Run pnpm run prepare to generate type stubs.
  • Use pnpm run dev to start playground in development mode.

License

Made with 💛

Published under MIT License.