{"_id":"@a0viedo/mercurius-codegen","name":"@a0viedo/mercurius-codegen","dist-tags":{"latest":"5.0.3"},"versions":{"5.0.3":{"name":"@a0viedo/mercurius-codegen","version":"5.0.3","keywords":["fastify","graphql","gql","mercurius","typescript","codegen"],"repository":{"type":"git","url":"git+https://github.com/mercurius-js/mercurius-typescript.git"},"license":"MIT","author":{"name":"PabloSz","email":"pablosaez1995@gmail.com"},"main":"dist/index.js","types":"dist/index.d.ts","dependencies":{"@graphql-codegen/core":"^3.1.0","@graphql-codegen/plugin-helpers":"^4.2.0","@graphql-codegen/typed-document-node":"^4.0.1","@graphql-codegen/typescript":"^3.0.4","@graphql-codegen/typescript-operations":"^3.0.4","@graphql-codegen/typescript-resolvers":"^3.2.1","@graphql-codegen/visitor-plugin-common":"^3.1.1","@graphql-tools/load-files":"^7.0.0","@graphql-tools/utils":"^10.0.11","@graphql-typed-document-node/core":"^3.2.0","chokidar":"^3.5.3","mkdirp":"^2.1.6"},"devDependencies":{"@istanbuljs/nyc-config-typescript":"^1.0.2","@types/mkdirp":"^1.0.2","@types/node":"^20.10.0","@types/prettier":"^2.7.3","@types/proxyquire":"^1.3.31","@types/rimraf":"^3.0.2","ava":"^5.3.1","bob-watch":"^0.1.2","c8":"^8.0.1","changesets-github-release":"^0.1.0","concurrently":"^8.2.2","cross-env":"^7.0.3","fastify":"^4.24.3","graphql":"^16.8.1","mercurius":"^13.3.1","nyc":"15.1.0","open-cli":"^7.2.0","prettier":"^2.8.8","proxyquire":"^2.1.3","rimraf":"^3.0.2","serve":"^14.2.1","tmp-promise":"^3.0.3","ts-node":"^10.9.1","typescript":"^5.3.2","wait-for-expect":"^3.0.2","wait-on":"^7.2.0","mercurius-codegen":"npm:@a0viedo/mercurius-codegen@5.0.3"},"peerDependencies":{"fastify":"^4.24.3","graphql":"*","mercurius":"^11.0.0 || ^12.0.0 || ^13.0.0","prettier":"^2.8.8"},"engines":{"pnpm":">=8.11.0"},"scripts":{"build":"tsc --removeComments && tsc --emitDeclarationOnly","dev":"tsc --watch --preserveWatchOutput","test":"cross-env TS_NODE_PROJECT=test/tsconfig.json c8 ava test/index.test.ts","test:watch":"bob-watch -w src test package.json -c \"pnpm test\"","watch":"concurrently -r pnpm:test:watch \"wait-on coverage && serve coverage/lcov-report\" \"wait-on -s 1 tcp:5000 && open-cli http://localhost:5000\""},"description":"[![npm version](https://badge.fury.io/js/mercurius-codegen.svg)](https://badge.fury.io/js/mercurius-codegen)","bugs":{"url":"https://github.com/mercurius-js/mercurius-typescript/issues"},"homepage":"https://github.com/mercurius-js/mercurius-typescript#readme","_id":"@a0viedo/mercurius-codegen@5.0.3","_integrity":"sha512-m0b6fM2ZHPQ5JKa798LdD4/FCrg0BoI+jE4bGbcPND4u9wLe3E1utRGb4dkEUhKLkP/xLq+TJp6F3qXCaM3dJw==","_resolved":"/private/var/folders/dz/46_5llr92mlb4d4ymkfq0c000000gn/T/98fb92dfde685d610ad7f1a93d6a944d/a0viedo-mercurius-codegen-5.0.3.tgz","_from":"file:a0viedo-mercurius-codegen-5.0.3.tgz","_nodeVersion":"20.10.0","_npmVersion":"10.2.3","dist":{"integrity":"sha512-m0b6fM2ZHPQ5JKa798LdD4/FCrg0BoI+jE4bGbcPND4u9wLe3E1utRGb4dkEUhKLkP/xLq+TJp6F3qXCaM3dJw==","shasum":"f00d4f524d4fad81f79135208fcbb1847f0db827","tarball":"https://registry.npmjs.org/@a0viedo/mercurius-codegen/-/mercurius-codegen-5.0.3.tgz","fileCount":19,"unpackedSize":47377,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBAKB4hX3+5KMQd0HqLCTMQVdaGGNhjPWg19+BzjeN29AiEA7D49FEJJrxXrQ0pZC2Dq9Z9+Abe/7Y4bZKsQmBUeSzY="}]},"_npmUser":{"name":"a0viedo","email":"alejandro.oviedo.g@gmail.com"},"directories":{},"maintainers":[{"name":"a0viedo","email":"alejandro.oviedo.g@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/mercurius-codegen_5.0.3_1701163034260_0.2688284221144315"},"_hasShrinkwrap":false}},"time":{"created":"2023-11-28T09:17:14.160Z","5.0.3":"2023-11-28T09:17:14.476Z","modified":"2023-11-28T09:17:14.719Z"},"maintainers":[{"name":"a0viedo","email":"alejandro.oviedo.g@gmail.com"}],"description":"[![npm version](https://badge.fury.io/js/mercurius-codegen.svg)](https://badge.fury.io/js/mercurius-codegen)","homepage":"https://github.com/mercurius-js/mercurius-typescript#readme","keywords":["fastify","graphql","gql","mercurius","typescript","codegen"],"repository":{"type":"git","url":"git+https://github.com/mercurius-js/mercurius-typescript.git"},"author":{"name":"PabloSz","email":"pablosaez1995@gmail.com"},"bugs":{"url":"https://github.com/mercurius-js/mercurius-typescript/issues"},"license":"MIT","readme":"# mercurius-codegen\n\n[![npm version](https://badge.fury.io/js/mercurius-codegen.svg)](https://badge.fury.io/js/mercurius-codegen)\n\nGet full type-safety and autocompletion for [Mercurius](http://mercurius.dev/) using [TypeScript](https://www.typescriptlang.org/) and [GraphQL Code Generator](https://graphql-code-generator.com/) seamlessly while you code.\n\n```sh\npnpm add mercurius-codegen\npnpm add -D prettier\n# or\nyarn add mercurius-codegen\nyarn add -D prettier\n# or\nnpm install mercurius-codegen\nnpm install -D prettier\n```\n\n## Usage\n\n> **For convenience**, this package also exports a _fake_ `gql` tag that gives tooling support for _\"prettier formatting\"_ and _\"IDE syntax highlighting\"_. **It's completely optional**.\n\n```ts\nimport Fastify from 'fastify'\nimport mercurius from 'mercurius'\nimport { codegenMercurius, gql } from 'mercurius-codegen'\n\nconst app = Fastify()\n\napp.register(mercurius, {\n  schema: gql`\n    type Query {\n      hello(greetings: String!): String!\n    }\n  `,\n  resolvers: {\n    Query: {\n      hello(_root, { greetings }) {\n        // greetings ~ string\n        return 'Hello World'\n      },\n    },\n  },\n})\n\ncodegenMercurius(app, {\n  targetPath: './src/graphql/generated.ts',\n}).catch(console.error)\n\n// Then it will automatically generate the file,\n// and without doing anything special,\n// the resolvers are going to be typed,\n// or if your resolvers are in different files...\n\napp.listen(8000)\n```\n\n```ts\nimport { IResolvers } from 'mercurius'\n\n// Fully typed!\nexport const resolvers: IResolvers = {\n  Query: {\n    hello(_root, { greetings }) {\n      // greetings ~ string\n      return 'Hello World'\n    },\n  },\n}\n```\n\nIt also gives type-safety for [Mercurius Loaders](https://mercurius.dev/#/docs/loaders):\n\n```ts\nimport { MercuriusLoaders } from 'mercurius'\n\n// Fully typed!\nexport const loaders: MercuriusLoaders = {\n  Dog: {\n    async owner(queries, ctx) {\n      // queries & ctx are typed accordingly\n      return queries.map(({ obj, params }) => {\n        // obj & params are typed accordingly\n        return owners[obj.name]\n      })\n    },\n  },\n}\n```\n\n> By default it disables itself if `NODE_ENV` is **'production'**\n\n> It automatically uses [prettier](https://prettier.io/) resolving the most nearby config for you.\n\n### Operations\n\n**mercurius-codegen** also supports giving it GraphQL Operation files, basically client `queries`, `mutations` or `subscriptions`, and it creates [Typed Document Nodes](https://github.com/dotansimha/graphql-typed-document-node), that later can be used by other libraries, like for example [mercurius-integration-testing](https://github.com/mercurius-js/mercurius-integration-testing) (_that has native support for typed document nodes_), and then be able to have end-to-end type-safety and auto-completion.\n\n> You might need to install `@graphql-typed-document-node/core` manually in your project.\n\n```ts\nimport { codegenMercurius } from 'mercurius-codegen'\n\ncodegenMercurius(app, {\n  targetPath: './src/graphql/generated.ts',\n  // You can also specify an array of globs\n  operationsGlob: './src/graphql/operations/*.gql',\n}).catch(console.error)\n```\n\n> /your-project/src/graphql/operations/example.gql\n\n```graphql\nquery hello {\n  HelloWorld\n}\n```\n\nThen, for example, in your tests:\n\n```ts\nimport { createMercuriusTestClient } from 'mercurius-integration-testing'\n\nimport { helloDocument } from '../src/graphql/generated'\nimport { app } from '../src/server'\n\n// ...\n\nconst client = createMercuriusTestClient(app)\n\nconst response = await client.query(helloDocument)\n\n// response is completely typed!\n```\n\n> Keep in mind that you can always call `codegenMercurius` multiple times for different environments and different paths if you prefer to keep the production code as light as possible (which is generally a good practice).\n\n## LazyPromise\n\nThis library also exports a very lightweight helper that is very useful for lazy resolution of promises, for example, to prevent un-requested data to be fetched from a database.\n\nBasically, it creates a lazy promise that defers execution until it's awaited or when .then() / .catch() is called, perfect for GraphQL Resolvers.\n\n> Internally, it uses [p-lazy](https://github.com/sindresorhus/p-lazy), which is also re-exported from this library\n\n```ts\nimport { LazyPromise } from 'mercurius-codegen'\n\n// ...\n\n// users == Promise<User[]>\nconst users = LazyPromise(() => {\n  return db.users.findMany({\n    // ...\n  })\n})\n```\n\n### Options\n\nThere are some extra options that can be specified:\n\n```ts\ninterface CodegenMercuriusOptions {\n  /**\n   * Specify the target path of the code generation.\n   *\n   * Relative to the directory of the executed script if targetPath isn't absolute\n   * @example './src/graphql/generated.ts'\n   */\n  targetPath: string\n  /**\n   * Disable the code generation manually\n   *\n   * @default process.env.NODE_ENV === 'production'\n   */\n  disable?: boolean\n  /**\n   * Don't notify to the console\n   */\n  silent?: boolean\n  /**\n   * Specify GraphQL Code Generator configuration\n   * @example\n   * codegenConfig: {\n   *    scalars: {\n   *        DateTime: \"Date\",\n   *    },\n   * }\n   */\n  codegenConfig?: CodegenPluginsConfig\n  /**\n   * Add code at the beginning of the generated code\n   */\n  preImportCode?: string\n  /**\n   * Operations glob patterns\n   */\n  operationsGlob?: string[] | string\n  /**\n   * Watch Options for operations GraphQL files\n   */\n  watchOptions?: {\n    /**\n     * Enable file watching\n     *\n     * @default false\n     */\n    enabled?: boolean\n    /**\n     * Extra Chokidar options to be passed\n     */\n    chokidarOptions?: ChokidarOptions\n    /**\n     * Unique watch instance\n     *\n     * `Especially useful for hot module replacement environments, preventing memory leaks`\n     *\n     * @default true\n     */\n    uniqueWatch?: boolean\n  }\n  /**\n   * Write the resulting schema as a `.gql` or `.graphql` schema file.\n   *\n   * If `true`, it outputs to `./schema.gql`\n   * If a string it specified, it writes to that location\n   *\n   * @default false\n   */\n  outputSchema?: boolean | string\n}\n\ncodegenMercurius(app, {\n  targetPath: './src/graphql/generated.ts',\n  operationsGlob: ['./src/graphql/operations/*.gql'],\n  disable: false,\n  silent: true,\n  codegenConfig: {\n    scalars: {\n      DateTime: 'Date',\n    },\n  },\n  preImportCode: `\n  // Here you can put any code and it will be added at very beginning of the file\n  `,\n  watchOptions: {\n    enabled: true,\n  },\n  outputSchema: true,\n}).catch(console.error)\n```\n\n### GraphQL Schema from files\n\nAs shown in the [**examples/codegen-gql-files**](/examples/codegen-gql-files/src/index.ts), you can load all your schema type definitions directly from GraphQL _.gql_ files, and this library gives you a function that eases that process, allowing you to get the schema from files, watch for changes and preloading the schema for production environments.\n\n- Usage options\n\n```ts\nexport interface LoadSchemaOptions {\n  /**\n   * Watch options\n   */\n  watchOptions?: {\n    /**\n     * Enable file watching\n     * @default false\n     */\n    enabled?: boolean\n    /**\n     * Custom function to be executed after schema change\n     */\n    onChange?: (schema: string[]) => void\n    /**\n     * Extra Chokidar options to be passed\n     */\n    chokidarOptions?: ChokidarOptions\n    /**\n     * Unique watch instance\n     *\n     * `Especially useful for hot module replacement environments, preventing memory leaks`\n     *\n     * @default true\n     */\n    uniqueWatch?: boolean\n  }\n  /**\n   * Pre-build options\n   */\n  prebuild?: {\n    /**\n     * Enable use pre-built schema if found.\n     *\n     * @default process.env.NODE_ENV === \"production\"\n     */\n    enabled?: boolean\n  }\n  /**\n   * Don't notify the console\n   */\n  silent?: boolean\n}\n```\n\n```ts\nimport Fastify from 'fastify'\nimport mercurius from 'mercurius'\nimport { buildSchema } from 'graphql'\nimport { codegenMercurius, loadSchemaFiles } from 'mercurius-codegen'\n\nconst app = Fastify()\n\nconst { schema } = loadSchemaFiles('src/graphql/schema/**/*.gql', {\n  watchOptions: {\n    enabled: process.env.NODE_ENV === 'development',\n    onChange(schema) {\n      app.graphql.replaceSchema(buildSchema(schema.join('\\n')))\n      app.graphql.defineResolvers(resolvers)\n\n      codegenMercurius(app, {\n        targetPath: './src/graphql/generated.ts',\n        operationsGlob: './src/graphql/operations/*.gql',\n      }).catch(console.error)\n    },\n  },\n})\n\napp.register(mercurius, {\n  schema,\n  // ....\n})\n```\n\n## License\n\nMIT\n\n---\n","readmeFilename":"README.md"}