Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum generation from a json schema does not work properly #424

Closed
wsong-fv opened this issue May 19, 2020 · 7 comments
Closed

Enum generation from a json schema does not work properly #424

wsong-fv opened this issue May 19, 2020 · 7 comments
Labels
bug Something isn't working

Comments

@wsong-fv
Copy link

I was trying to generate mesh.ts with this json schema:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "array",
  "items": {
    "$ref": "#/definitions/NameraAbDirElement"
  },
  "definitions": {
    "Province": {
      "type": "string",
      "enum": ["AB"],
      "title": "Province"
    },
    "CompanyType": {
      "type":"string",
      "enum": [
        "Named Alberta Corporation",
        "Other Prov/Territory Corps",
        "Professional Corporation",
        "Federal Corporation"
      ],
      "title": "CompanyType"
    },
    "RegistrationEvent": {
      "type": "string",
      "enum": ["Incorporated", "Registered"],
      "title": "RegistrationEvent"
    },
    "NameraAbDirElement": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "company_name": {
          "type": "string"
        },
        "company_number": {
          "type": "integer"
        },
        "company_type": {
          "$ref": "#/definitions/CompanyType"
        },
        "registration_event": {
          "$ref": "#/definitions/RegistrationEvent"
        },
        "registration_date": {
          "type": "string",
          "format": "date"
        },
        "address": {
          "type": "string"
        },
        "formatted_address": {
          "type": "string"
        },
        "city": {
          "type": "string"
        },
        "province": {
          "$ref": "#/definitions/Province"
        },
        "postal_code": {
          "type": "string"
        },
        "source_url": {
          "type": "string",
          "format": "uri",
          "qt-uri-protocols": ["http"],
          "qt-uri-extensions": [".cfm"]
        },
        "issue_date": {
          "type": "string",
          "format": "date"
        },
        "geometry": {
          "type": "string"
        }
      },
      "required": [
        "address",
        "city",
        "company_name",
        "company_number",
        "company_type",
        "formatted_address",
        "geometry",
        "issue_date",
        "postal_code",
        "province",
        "registration_date",
        "registration_event",
        "source_url"
      ],
      "title": "NameraAbDirElement"
    }
  }
}

and the compiler gave me an error that CompanyType does not exist.

Full Error Message here:

Error: Type with name "CompanyType" does not exists
    at SchemaComposer.get (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/TypeStorage.js:44:13)
    at ThunkComposer._ThunkComposer.ThunkComposer [as _thunk] (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/TypeMapper.js:585:34)
    at ThunkComposer.get ofType [as ofType] (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/ThunkComposer.js:20:34)
    at NonNullComposer.getUnwrappedTC (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/NonNullComposer.js:38:15)
    at unwrapTC (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/utils/typeHelpers.js:208:31)
    at unwrapOutputTC (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/utils/typeHelpers.js:220:10)
    at ObjectTypeComposer.getFieldTC (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/ObjectTypeComposer.js:352:44)
    at tc.getFieldNames.forEach.fieldName (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/SchemaComposer.js:169:26)
    at Array.forEach (<anonymous>)
    at SchemaComposer.removeEmptyTypes (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/SchemaComposer.js:168:24)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

After digging in the node_modules, I have found that in the index.cjs.js file, the method visitEnum was causing the issue. The createName function originally had [propertyName, prefix].join('_') which you can see below as the commented out line. It joined the CompanyType enum with the prefix NameraAbSearch, resulting in the name CompanyTypeNameraAbSearch, causing the program to fail to find the original name. This was fixed by removing the prefix from the argument.

visitEnum(enumDef, propertyName, prefix) {
        // const name = createName([propertyName, prefix].join('_'));  Doesn't work 😞
        const name = createName(propertyName); // Works 😄
        console.log(propertyName, "thing", thing);
        this.schemaComposer.createEnumTC({
            name,
            values: enumDef.enum.reduce((values, enumValue) => ({ ...values, [enumValue]: {} }), {});,
        });
        return name;
    }
}

Note: that this can be overridden by changing the Enum name in the schema, as shown below.

However, the expected behaviour is that the parser should not require special schema modifications for it to function.

Original:

"company_type": {
    "$ref": "#/definitions/CompanyType"
}

Fixed:

"company_type": {
   "$ref": "#/definitions/CompanyTypeNameraAbSearch"
}
@ardatan
Copy link
Owner

ardatan commented May 20, 2020

@wsong-fv Thank you for opening this issue!
Could you try with the alpha version of this PR and let us know if it's fixed?
#428 (comment)

@wsong-fv
Copy link
Author

wsong-fv commented May 20, 2020

@ardatan Nope, it still fails. Error message:

Error: Type with name "CompanyType" does not exists
    at SchemaComposer.get (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/TypeStorage.js:44:13)
    at ThunkComposer._ThunkComposer.ThunkComposer [as _thunk] (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/TypeMapper.js:585:34)
    at ThunkComposer.get ofType [as ofType] (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/ThunkComposer.js:20:34)
    at NonNullComposer.getUnwrappedTC (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/NonNullComposer.js:38:15)
    at unwrapTC (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/utils/typeHelpers.js:208:31)
    at unwrapOutputTC (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/utils/typeHelpers.js:220:10)
    at ObjectTypeComposer.getFieldTC (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/ObjectTypeComposer.js:352:44)
    at tc.getFieldNames.forEach.fieldName (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/SchemaComposer.js:169:26)
    at Array.forEach (<anonymous>)
    at SchemaComposer.removeEmptyTypes (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/graphql-compose/lib/SchemaComposer.js:168:24)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

And this is the schema I tried to build:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "array",
    "items": {
        "$ref": "#/definitions/NamaraABElement"
    },
    "definitions": {
        "NamaraABElement": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "company_name": {
                    "type": "string"
                },
                "company_number": {
                    "type": "integer"
                },
                "company_type": {
                    "$ref": "#/definitions/CompanyType"
                },
                "registration_event": {
                    "$ref": "#/definitions/RegistrationEvent"
                },
                "registration_date": {
                    "type": "string",
                    "format": "date"
                },
                "address": {
                    "type": "string"
                },
                "formatted_address": {
                    "type": "string"
                },
                "city": {
                    "type": "string"
                },
                "province": {
                    "$ref": "#/definitions/Province"
                },
                "postal_code": {
                    "type": "string"
                },
                "source_url": {
                    "type": "string",
                    "format": "uri",
                    "qt-uri-protocols": [
                        "http"
                    ],
                    "qt-uri-extensions": [
                        ".cfm"
                    ]
                },
                "issue_date": {
                    "type": "string",
                    "format": "date"
                },
                "geometry": {
                    "type": "string"
                }
            },
            "required": [
                "address",
                "city",
                "company_name",
                "company_number",
                "company_type",
                "formatted_address",
                "geometry",
                "issue_date",
                "postal_code",
                "province",
                "registration_date",
                "registration_event",
                "source_url"
            ],
            "title": "NamaraABElement"
        },
        "CompanyType": {
            "type": "string",
            "enum": [
                "Named Alberta Corporation",
                "Other Prov/Territory Corps",
                "Professional Corporation",
                "Federal Corporation"
            ],
            "title": "CompanyType"
        },
        "Province": {
            "type": "string",
            "enum": [
                "AB"
            ],
            "title": "Province"
        },
        "RegistrationEvent": {
            "type": "string",
            "enum": [
                "Incorporated",
                "Registered"
            ],
            "title": "RegistrationEvent"
        }
    }
}

I noticed that you guys didn't look into index.cjs.js in the json-schema module; since I managed to get the schema built after changing some lines within that file, I wonder if that is the root of the problem.

Edit: the temp workaround I had seemed to still cause the new alpha version to fail.

Edit 2: this version seems to break even non-enum schemas.

Error msg:

TypeError: Cannot use 'in' operator to search for 'arguments' in undefined
    at correctType (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/dist/utils/src/print-schema-with-directives.js:57:70)
    at Object.printSchemaWithDirectives (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/dist/utils/src/print-schema-with-directives.js:15:33)
    at generateTsTypes (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/@graphql-mesh/cli/bin.js:111:37)
    at Object.yargs.command.command.command [as handler] (/home/wsong/Documents/Visual-Studio-Code/vonx-graphql/node_modules/@graphql-mesh/cli/bin.js:373:30)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Non-enum schema that I tried to build:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "array",
    "items": {
        "$ref": "#/definitions/NamaraABElement"
    },
    "definitions": {
        "NamaraABElement": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "company_name": {
                    "type": "string"
                },
                "company_number": {
                    "type": "integer"
                },
                "company_type": {
                    "type": "string"
                },
                "registration_event": {
                    "type": "string"
                },
                "registration_date": {
                    "type": "string",
                    "format": "date"
                },
                "address": {
                    "type": "string"
                },
                "formatted_address": {
                    "type": "string"
                },
                "city": {
                    "type": "string"
                },
                "province": {
                    "type": "string"
                },
                "postal_code": {
                    "type": "string"
                },
                "source_url": {
                    "type": "string",
                    "format": "uri",
                    "qt-uri-protocols": [
                        "http"
                    ],
                    "qt-uri-extensions": [
                        ".cfm"
                    ]
                },
                "issue_date": {
                    "type": "string",
                    "format": "date"
                },
                "geometry": {
                    "type": "string"
                }
            },
            "required": [
                "address",
                "city",
                "company_name",
                "company_number",
                "company_type",
                "formatted_address",
                "geometry",
                "issue_date",
                "postal_code",
                "province",
                "registration_date",
                "registration_event",
                "source_url"
            ],
            "title": "NamaraABElement"
        }
    }
}

@ardatan
Copy link
Owner

ardatan commented May 20, 2020

https://codesandbox.io/s/dreamy-cartwright-m6gkr?file=/package.json
I tried to reproduce your issue but it worked with the alpha version. Let me know if I'm missing something.

@wsong-fv
Copy link
Author

wsong-fv commented May 20, 2020

Hello, this is my package.json:

"dependencies": {
    "@graphql-mesh/cli": "0.1.18-alpha-fb45a87.0",
    "@graphql-mesh/json-schema": "0.1.18-alpha-fb45a87.0",
    "@graphql-mesh/openapi": "0.1.18-alpha-fb45a87.0",
    "@graphql-mesh/runtime": "0.1.18-alpha-fb45a87.0",
    "@graphql-mesh/transform-extend": "0.1.18-alpha-fb45a87.0",
    "@graphql-mesh/transform-filter-schema": "0.1.18-alpha-fb45a87.0",
    "@graphql-mesh/transform-prefix": "0.1.18-alpha-fb45a87.0",
    "addresser": "^1.1.19",
    "graphql": "^15.0.0",
    "graphql-fields": "2.0.3",
    "typescript": "^3.8.3"
  },
  "devDependencies": {
    "ts-node": "^8.10.1"
  }

The schema I tried to build is up above, in the meshrc.yaml the relevant portion is:

- name: NamaraAB
   handler:
     jsonSchema:
       baseUrl: https://api.namara.io/v0/data_sets/b70e5999-1e16-4472-bd6a-a536d4f3d41e/data/en-2?geometry_format=wkt
       operations:
         - type: Query
           field: NamaraABSearch
           path: "&where=company_name%20LIKE%20%22%25{args.name}%25%22&select=company_name%2Ccompany_number%2Ccompany_type%2Cregistration_event%2Cregistration_date%2Caddress%2Cformatted_address%2Ccity%2Cprovince%2Cpostal_code%2Csource_url%2Cissue_date%2Cgeometry"
           method: GET
           responseSchema: api-schemas/namara_ab.json

I am still getting the same error that I reported above. This fails when I try to run yarn graphql-mesh typescript --output ./src/generated/mesh.ts. I apologize if I'm missing something else. I'm relatively new at this.

@ardatan
Copy link
Owner

ardatan commented May 20, 2020

https://codesandbox.io/s/dreamy-cartwright-m6gkr?file=/package.json
I copied your configuration and package.json to reproduce the issue but it is working well. You can test it by yourself on this sandbox. If you still have the issue, please fork this sandbox and reproduce it.

@wsong-fv
Copy link
Author

Hello @ardatan , after experimenting around, I have discovered that the combination of an openapi source and a json schema source causes the error.
Here is my forked sandbox: https://codesandbox.io/s/fancy-water-9115v?file=/.meshrc.yml

Running yarn generate should reproduce the error.

@ardatan
Copy link
Owner

ardatan commented May 26, 2020

Available in v0.1.18!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants