ardatan / graphql-mesh

🕸️ GraphQL Federation Framework for any API services such as REST, OpenAPI, Swagger, SOAP, gRPC and more...
https://the-guild.dev/graphql/mesh
MIT License
3.26k stars 332 forks source link

Naming Convention Transform Breaks Union Type Query #1592

Open ddcollins opened 3 years ago

ddcollins commented 3 years ago

Adding the naming convention transform:

transforms:
  # Global
  - namingConvention:
      typeNames: pascalCase
      enumValues: upperCase
      fieldNames: camelCase

Results in the following error:

"originalError": {
    "name": "TypeError",
    "message": "Cannot read property 'getFields' of undefined",
    "stack": [
        "TypeError: Cannot read property 'getFields' of undefined",
        "    at visitObjectValue (/app/node_modules/@graphql-tools/utils/index.cjs.js:4078:27)",
        "    at visitFieldValue (/app/node_modules/@graphql-tools/utils/index.cjs.js:4148:16)",
        "    at /app/node_modules/@graphql-tools/utils/index.cjs.js:4102:26",
        "    at Array.forEach (<anonymous>)",
        "    at visitObjectValue (/app/node_modules/@graphql-tools/utils/index.cjs.js:4089:31)",
        "    at visitFieldValue (/app/node_modules/@graphql-tools/utils/index.cjs.js:4152:16)",
        "    at /app/node_modules/@graphql-tools/utils/index.cjs.js:4135:35",
        "    at Array.map (<anonymous>)",
        "    at visitListValue (/app/node_modules/@graphql-tools/utils/index.cjs.js:4135:17)",
        "    at visitFieldValue (/app/node_modules/@graphql-tools/utils/index.cjs.js:4143:16)"
    ]
},

When trying to run the following query:

{
    items {
        course(limit:10) {
            id
            title
            description
            lessons {
                title
                subTitle
                canPreview
                description
                steps {
                    stepTitle
                    canPreview
                    item {
                        ... on LessonSlide {
                            content
                            status
                            imageUrl
                        }
                        ... on LessonVideo {
                            videoUrl
                        }
                    }
                }
            }
        }
    }

}

This query works fine when the naming convention transform is removed. The query also works fine if I remove:

item {
    ... on LessonSlide {
        content
        status
        imageUrl
    }
    ... on LessonVideo {
        videoUrl
    }
}

The query is through my graphql handler.

Dependencies:

"@graphql-mesh/cli": "^0.15.*",
    "@graphql-mesh/graphql": "^0.13.*",
    "@graphql-mesh/json-schema": "^0.10.*",
    "@graphql-mesh/transform-encapsulate": "^0.1.20",
    "@graphql-mesh/transform-naming-convention": "^0.6.23",
    "@graphql-mesh/transform-resolvers-composition": "^0.7.*",
ddcollins commented 3 years ago

It's specifically this transform:

typeNames: pascalCase
ardatan commented 3 years ago

Could you share more details and a reproduction if possible?

ddcollins commented 3 years ago

Could you share more details and a reproduction if possible?

This is my mesh config

sources:
  - name: DirectusAcademy
    handler:
      graphql:
        endpoint: ${DIRECTUS_HOST}
        schemaHeaders:
          Authorization: Bearer ${DIRECTUS_API_TOKEN}
        operationHeaders:
          Authorization: Bearer ${DIRECTUS_API_TOKEN}
        batch: false

transforms:
  # Global
  - resolversComposition:
      - resolver: 'Query.*'
        composer: ./src/middleware/api-token-guard.js
      - resolver: 'Mutation.*'
        composer: ./src/middleware/api-token-guard.js
  - namingConvention:
      typeNames: pascalCase
      enumValues: upperCase
      fieldNames: camelCase

serve:
  port: 5000

We are hooking into Directus which has "many to any" relational support

e.g.

Lesson has "steps", and that step could be a "Lesson Slide", "Lesson Video", etc object.

Previous to the transforms the query was:

{
    items {
        course(limit:10) {
            id
            title
            description
            lessons {
                title
                sub_title
                can_preview
                description
                steps {
                    step_title
                    can_preview
                    item {
                        ... on lesson_slide {
                            content
                            status
                            image_url
                        }
                        ... on lesson_video {
                            video_url
                        }
                    }
                }
            }
        }
    }

}

My theory is that it's still looking for the snake case type name for lesson_slide and lesson_video and not the transformed pascal case

Here's a repo I set up with instructions on how to replicate this! https://github.com/ddcollins/mesh-directus-replication