@stackworx/relay-extractor
v0.0.4
Published
Relay Query Extractor for Testing
Readme
Read Me
This project is meant to extract all the GraphQL operations from a relay project and clean them so that a client can be generated
CLI Usage
Extract operations from a source folder to an output directory. Optionally provide a schema (SDL or introspection JSON) and exclude subscriptions.
npx @stackworx/relay-extractor \
extract \
--src ./src \
--out ./out \
--schema ./schema.graphql \
--exclude-subscriptions--exclude-subscriptions: When set, subscription operations are skipped and not written to the output.
Annotate your schema SDL to mark unused fields (based on operations found in source files).
npx @stackworx/relay-extractor \
annotate-unused \
--src ./src \
--schema ./schema.graphqlBy default this overwrites the file passed to --schema. To write somewhere else (or to stdout):
# Write to a different file
npx @stackworx/relay-extractor annotate-unused --src ./src --schema ./schema.graphql --out ./schema.annotated.graphql
# Print to stdout
npx @stackworx/relay-extractor annotate-unused --src ./src --schema ./schema.graphql --out -- Default annotation mode is
@deprecated(reason: "relay-extractor: unused"). - Use
--mode directive --directive-name unusedto apply a custom directive instead.
Issues
- Remove Relay Compiler Directives
- Remove Relay Interla fields (
__id) - Handle missing arguments that relay handles with
argumentDefinitions
Testing: Vitest + GraphQL fix
Background: When running Vitest with GraphQL-related tooling, you may see errors like: "Cannot use GraphQLSchema ... from another module or realm" due to how module resolution handles
graphqlsubpath imports.Fix: Add aliases so Vitest resolves the ESM entry points consistently. This is configured in
vitest.config.ts:import { defineConfig } from 'vitest/config' export default defineConfig({ resolve: { alias: { 'graphql/language/printer': 'graphql/language/printer.js', 'graphql/language': 'graphql/language/index.js', graphql: 'graphql/index.js', }, }, })Reference: https://github.com/vitest-dev/vitest/issues/4605#issuecomment-1847658160
Run tests:
npm run test -- --run
