graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

[Feature] Support YAML anchors

Open xenoterracide opened this issue 5 years ago • 3 comments

I have this code with a couple of modules, but I plan to expand to many more modules, and already they're getting repetetive in the plugins block at least. I'd like to see yaml anchors supported here's a list of officially recommended parsers that support them (I'm not sure how you're parsing now) https://yaml.org scroll down to Javascript, both yaml and js-yaml are available via npm

overwrite: true
require:
  - ts-node/register
config:
  useTypeImports: true
  scalars:
    DateTime: "@js-joda/core#ZonedDateTime"
    Date: "@js-joda/core#LocalDate"
generates:
  packages/apollo/src/generated/schema.ts:
    schema:
      - packages/apollo/src/**/*.schema.graphql
      - packages/apollo/src/**/*.test-schema.graphql
    documents: packages/apollo/src/**/*.test.graphql
    plugins:
      - typescript
      - typescript-resolvers
      - typescript-operations
      - typescript-document-nodes
  packages/app/src/generated/schema.graphql:
    schema: packages/*/src/**/*.schema.graphql
    plugins:
      - schema-ast
  packages/app/src/generated/schema.ts:
    schema: packages/*/src/**/*.schema.graphql
    documents: packages/app/src/**/*.test.graphql
    plugins:
      - typescript
      - typescript-resolvers
      - typescript-operations
      - typescript-document-nodes
    config:
      noSchemaStitching: true
      contextType: ../shared/graphql#Context
      defaultMapper: Partial<{T}>
      mappers:

I'd rather rewrite it like this

configs:
  tsfile: &ts-schema
    plugins:
      - typescript
      - typescript-resolvers
      - typescript-operations
      - typescript-document-nodes

overwrite: true
require:
  - ts-node/register
config:
  useTypeImports: true
  scalars:
    DateTime: "@js-joda/core#ZonedDateTime"
    Date: "@js-joda/core#LocalDate"
generates:
  packages/apollo/src/generated/schema.ts:
    <<: *ts-schema
    schema:
      - packages/apollo/src/**/*.schema.graphql
      - packages/apollo/src/**/*.test-schema.graphql
  packages/app/src/generated/schema.graphql:
    schema: packages/*/src/**/*.schema.graphql
    plugins:
      - schema-ast
  packages/app/src/generated/schema.ts:
    <<: *ts-schema
    schema: packages/*/src/**/*.schema.graphql
    config:
> npx envinfo --preset jest                                        # services -> feature/RS2-1243-logging $ ! RC=1
npx: installed 1 in 1.461s

  System:
    OS: macOS 10.15.6
    CPU: (4) x64 Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
  Binaries:
    Node: 12.18.3 - /usr/local/bin/node
    Yarn: 2.3.0 - /usr/local/bin/yarn
    npm: 6.14.6 - /usr/local/bin/npm

xenoterracide avatar Oct 20 '20 18:10 xenoterracide

Totally agree. We are using js-yaml and I think it's already supported. Have you tried it? It should work...

dotansimha avatar Oct 21 '20 07:10 dotansimha

hmm... maybe I'm doing something wrong.... I had only tried the former of these yesterday

configs:
  tsfile: &ts-schema
    plugins:
      - typescript
      - typescript-resolvers
      - typescript-operations
      - typescript-document-nodes

generates:
  packages/apollo/src/generated/schema.ts:
    <<: *ts-schema
    schema:
      - packages/apollo/src/**/*.schema.graphql
      - packages/apollo/src/**/*.test-schema.graphql
    documents: packages/apollo/src/**/*.test.graphql

does not work

> DEBUG=1 VERBOSE=1 yarn workspace @bb/graph-modules build
[10:23:47] Parse configuration [started]
[10:23:47] Parse configuration [failed]
[10:23:47] → Invalid "generates" config!
Invalid "generates" config!

but this does

ts-plugins: &ts-plugins
  - typescript
  - typescript-resolvers
  - typescript-operations
  - typescript-document-nodes
generates:
  packages/apollo/src/generated/schema.ts:
    schema:
      - packages/apollo/src/**/*.schema.graphql
      - packages/apollo/src/**/*.test-schema.graphql
    documents: packages/apollo/src/**/*.test.graphql
    plugins: *ts-plugins

one other comment about this, and maybe it needs to be somehow supported somewhere... but in vscode it's validating my schema somehow(telling me which keys are valid), I have no idea how this is possible (maybe some plugin just has the rules defined), but also possible maybe there's some schema defined somewhere in this project? if so, it'd be nice to have some predefined hierarchy to put the anchors under.

xenoterracide avatar Oct 21 '20 15:10 xenoterracide

I think interpret the anchor is working. But soemhow, I have got the “<<“ in the output. When do console.log(yaml.parser(data)) Any ideas why??

XIAOLONGLII avatar Feb 03 '24 18:02 XIAOLONGLII