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

Improve error message when missing queryGenerationFactories in the template. #92

Closed
efstathiosntonas opened this issue Jun 7, 2022 · 13 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@efstathiosntonas
Copy link

Thank you for this great generator.

While Build Template it throws this error:

 Error: Cannot generate a random value for scalar 'smallint'.
  The random generator is not able to randomly generate a value for non-standard GraphQL scalars.
  You have to provide a custom factory by providing this in your config:
  {
  'smallint': () => generateRandomCustomScalar()
  }

Can you please provide an example on how to fix this? Documentation is not clear enough, where the 'smallint': () => generateRandomCustomScalar() should go in the config? Can you provide an example of generateRandomCustomScalar()?

@pelletier197
Copy link
Collaborator

pelletier197 commented Jun 7, 2022

Thank you for raising this issue. So this is an error thrown by the Query generator plugin, which is not entirely accurate when using the magidoc template directly. I will improve the error message for this special error, since I expect it may happen quite often.

To solve it, you need to provide a query generation factory in the magidoc config

export default {
  introspection: {
    // ...
  }, 
  website: {
    // ...
    queryGenerationFactories: {
      smallint: 42
    }
  }
}

More detail about it can be found here.

@pelletier197 pelletier197 self-assigned this Jun 7, 2022
@pelletier197 pelletier197 added bug Something isn't working enhancement New feature or request labels Jun 7, 2022
@pelletier197 pelletier197 changed the title Error: Cannot generate a random value for scalar 'smallint'. Improve error message when missing queryGenerationFactories in the template. Jun 7, 2022
@efstathiosntonas
Copy link
Author

Thanks @pelletier197 for the fast reply. I've added it on config and it throws the same error, here's the full log:

   Command 'pnpm' failed with status 1 when executed in directory /var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0
  ---- Program Output----
  > @magidoc/starter-carbon-multi-page@2.0.0 build /private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0
  > svelte-kit build
  vite v2.9.10 building for production...
  transforming...
  ✓ 580 modules transformed.
  rendering chunks...
  .svelte-kit/output/client/_app/immutable/manifest.json                                           4.86 KiB
  .svelte-kit/output/client/_app/immutable/pages/__error.svelte-5f5d1a2d.js                        1.16 KiB / gzip: 0.69 KiB
  .svelte-kit/output/client/_app/immutable/pages/_...page_.svelte-11d34174.js                      1.05 KiB / gzip: 0.59 KiB
  .svelte-kit/output/client/_app/immutable/pages/index.svelte-90b5b8fc.js                          0.22 KiB / gzip: 0.19 KiB
  .svelte-kit/output/client/_app/immutable/pages/mutations/_mutation_.svelte-ba79272f.js           1.56 KiB / gzip: 0.84 KiB
  .svelte-kit/output/client/_app/immutable/pages/queries/_query_.svelte-7f7810e7.js                1.53 KiB / gzip: 0.83 KiB
  .svelte-kit/output/client/_app/immutable/pages/subscriptions/_subscription_.svelte-d112382f.js   1.58 KiB / gzip: 0.85 KiB
  .svelte-kit/output/client/_app/immutable/start-69b04ef6.js                                       25.59 KiB / gzip: 9.18 KiB
  .svelte-kit/output/client/_app/immutable/pages/__layout.svelte-be4fa9bb.js                       35.20 KiB / gzip: 10.52 KiB
  .svelte-kit/output/client/_app/immutable/chunks/paths-8dcd8f6b.js                                0.86 KiB / gzip: 0.53 KiB
  .svelte-kit/output/client/_app/immutable/chunks/ChevronDown-6302633b.js                          1.66 KiB / gzip: 0.92 KiB
  .svelte-kit/output/client/_app/immutable/chunks/index-32191452.js                                9.88 KiB / gzip: 3.88 KiB
  .svelte-kit/output/client/_app/immutable/assets/pages/types/_type_.svelte-ea72c5bf.css           0.23 KiB / gzip: 0.18 KiB
  .svelte-kit/output/client/_app/immutable/pages/types/_type_.svelte-de89b16d.js                   31.91 KiB / gzip: 6.45 KiB
  .svelte-kit/output/client/_app/immutable/assets/pages/__error.svelte-45f46132.css                0.13 KiB / gzip: 0.13 KiB
  .svelte-kit/output/client/_app/immutable/assets/FieldDetails-077d5dc0.css                        0.18 KiB / gzip: 0.14 KiB
  .svelte-kit/output/client/_app/immutable/assets/PreviousNextPage-00e2b760.css                    1.25 KiB / gzip: 0.41 KiB
  .svelte-kit/output/client/_app/immutable/assets/pages-69fbf403.css                               5.44 KiB / gzip: 1.58 KiB
  .svelte-kit/output/client/_app/immutable/chunks/ArgsList-01b31947.js                             23.40 KiB / gzip: 6.94 KiB
  .svelte-kit/output/client/_app/immutable/chunks/PreviousNextPage-34f1767a.js                     47.33 KiB / gzip: 15.97 KiB
  .svelte-kit/output/client/_app/immutable/assets/pages/__layout.svelte-4dec8d5f.css               737.09 KiB / gzip: 64.61 KiB
  .svelte-kit/output/client/_app/immutable/chunks/FieldDetails-f0adf12a.js                         507.26 KiB / gzip: 151.48 KiB
  .svelte-kit/output/client/_app/immutable/chunks/pages-e8f606df.js                                2394.64 KiB / gzip: 186.44 KiB
  (!) Some chunks are larger than 500 KiB after minification. Consider:
  - Using dynamic import() to code-split the application
  - Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/guide/en/#outputmanualchunks
  - Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.
  vite v2.9.10 building SSR bundle for production...
  transforming...
  "GraphQLObjectType" is imported from external module "graphql" but never used in "src/lib/components/tags/TypeTag.svelte", "src/lib/components/tags/NullableTag.svelte", "src/lib/components/type/TypeLink.svelte", "src/lib/model.ts" and "src/routes/types/[type].svelte".
  ✓ 429 modules transformed.
  Generated an empty chunk: "hooks"
  rendering chunks...
  .svelte-kit/output/server/manifest.json                                          4.43 KiB
  .svelte-kit/output/server/index.js                                               74.30 KiB
  .svelte-kit/output/server/entries/pages/__error.svelte.js                        1.27 KiB
  .svelte-kit/output/server/entries/pages/_...page_.svelte.js                      1.09 KiB
  .svelte-kit/output/server/entries/pages/index.svelte.js                          0.28 KiB
  .svelte-kit/output/server/entries/pages/mutations/_mutation_.svelte.js           1.73 KiB
  .svelte-kit/output/server/entries/pages/queries/_query_.svelte.js                1.70 KiB
  .svelte-kit/output/server/entries/pages/subscriptions/_subscription_.svelte.js   1.76 KiB
  .svelte-kit/output/server/entries/pages/types/_type_.svelte.js                   15.43 KiB
  .svelte-kit/output/server/chunks/paths-396f020f.js                               0.16 KiB
  .svelte-kit/output/server/chunks/index-ef261cd9.js                               9.26 KiB
  .svelte-kit/output/server/chunks/ChevronDown-fc666eff.js                         1.36 KiB
  .svelte-kit/output/server/chunks/PreviousNextPage-48fcfb1e.js                    61.21 KiB
  .svelte-kit/output/server/chunks/FieldDetails-797404db.js                        23.76 KiB
  .svelte-kit/output/server/chunks/TypeLinkTag-1aad5233.js                         12.54 KiB
  .svelte-kit/output/server/chunks/hooks-1c45ba0b.js                               0.00 KiB
  .svelte-kit/output/server/entries/pages/__layout.svelte.js                       777.85 KiB
  .svelte-kit/output/server/chunks/pages-ea90250b.js                               4442.73 KiB
  302 / -> /introduction/welcome
  Error: Cannot generate a random value for scalar 'smallint'.
  The random generator is not able to randomly generate a value for non-standard GraphQL scalars.
  You have to provide a custom factory by providing this in your config:
  {
  'smallint': () => generateRandomCustomScalar()
  }
  at randomFactory (file:///private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/@magidoc+plugin-query-generator@2.0.0/node_modules/@magidoc/plugin-query-generator/build/generator/fakeGenerator.js:101:19)
  at findMostSpecificFactory (file:///private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/@magidoc+plugin-query-generator@2.0.0/node_modules/@magidoc/plugin-query-generator/build/generator/fakeGenerator.js:92:12)
  at generateInput (file:///private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/@magidoc+plugin-query-generator@2.0.0/node_modules/@magidoc/plugin-query-generator/build/generator/fakeGenerator.js:49:21)
  at file:///private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/@magidoc+plugin-query-generator@2.0.0/node_modules/@magidoc/plugin-query-generator/build/generator/fakeGenerator.js:116:24
  at /private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/lodash.js:13469:38
  at /private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/lodash.js:4967:15
  at baseForOwn (/private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/lodash.js:3032:24)
  at Function.mapValues (/private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/lodash.js:13468:7)
  at file:///private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/@magidoc+plugin-query-generator@2.0.0/node_modules/@magidoc/plugin-query-generator/build/generator/fakeGenerator.js:115:22
  at generateInput (file:///private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/@magidoc+plugin-query-generator@2.0.0/node_modules/@magidoc/plugin-query-generator/build/generator/fakeGenerator.js:49:68)
  > 500 /queries/account (linked from /introduction/welcome)
  at file:///private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.348_svelte@3.48.0/node_modules/@sveltejs/kit/dist/chunks/index.js:1022:11
  at save (file:///private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.348_svelte@3.48.0/node_modules/@sveltejs/kit/dist/chunks/index.js:1241:4)
  at visit (file:///private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.348_svelte@3.48.0/node_modules/@sveltejs/kit/dist/chunks/index.js:1132:3)
  at processTicksAndRejections (node:internal/process/task_queues:96:5)
  ELIFECYCLE  Command failed with exit code 1.

this is the config:

export default {
  introspection: {
    type: "url",
    url: "http://localhost:9091/v1/graphql",
    headers: {
      "x-hasura-admin-secret": "myadminsecretkey",
    },
  },
  website: {
    template: "carbon-multi-page",
    queryGenerationFactories: {
      smallint: 42
    }
  },
};

@pelletier197
Copy link
Collaborator

pelletier197 commented Jun 7, 2022

Oh.. sorry, my bad. I wrote this on my phone, should have opened my computer 😅. Simply wrap it in an options key:

export default {
  introspection: {
    type: "url",
    url: "http://localhost:9091/v1/graphql",
    headers: {
      "x-hasura-admin-secret": "myadminsecretkey",
    },
  },
  website: {
    template: "carbon-multi-page",
    options: {
      queryGenerationFactories: {
        smallint: 42
      }
    }
  },
};

@efstathiosntonas
Copy link
Author

yeah, you are right, on the docs link you provided above it's inside an options object but was hard to spot due to the many comments 😅.

Probably this is not possible: Can we first check for unknown scalars prior reaching the Build Template step? It takes lot's of time to build and throw an error if scalar is not known. We know witch scalars are not compatible before hand of course, just saying.

Thanks @pelletier197, keep it up!

@pelletier197
Copy link
Collaborator

I can try to look if I could find a way to do that, or at least show all the missing scalars at once in the error.

You're right, it's not necessarily easy, because the cli doesn't actually do anything with the options, it simply forwards them directly to the template. And since the factories support glob syntax as well, it adds a little bit of complexity.

I'll see what I can do, thanks for the idea!

@efstathiosntonas
Copy link
Author

That makes sense!

It would be great to do a check first, will save some time while building the config since we won't have to cross check our scalars against it.

Another option is to have them predefined, at least the most common ones like eg timestamp, date, uuid and let the user override them on options.queryGenerationFactories if desired.

@pelletier197
Copy link
Collaborator

pelletier197 commented Jun 7, 2022

There are some default factories, but very few. I'll add those you mentioned, they clearly seem like common things that could come back in many APIs.

If you have other common scalars that could be there by default, let me know, I'll add them as well 🙂

@efstathiosntonas
Copy link
Author

efstathiosntonas commented Jun 7, 2022

great, these are some common Hasura scalars:

    {
        _text: "loremipsum",
        bigint: 1000,
        date: "2022-03-06T08:23:45.000Z",
        float8: 0.1,
        geography: { type: "Point", coordinates: [0, 0] },
        geometry: { type: "Point", coordinates: [0, 0] },
        jsonb: {},
        numeric: 1,
        smallint: 42,
        timestamp: "2022-03-06T08:23:45.000Z",
        timestamptz: "2022-03-06T08:23:45.000Z",
        uuid: "a12ce80c-42e3-4149-89cc-4afda30e5e7f",
      }

After adding all these scalars above it throws a new error:

  Error: Fatal error: A recursive input with a non-nullable circular reference was detected, which makes it impossible to generate a query. If you ever see this error, it means that your GraphQL uses an invalid schema.
  at generateInput (file:///private/var/folders/yz/dk5dbx8n2hqcfk1kdktxmknh0000gn/T/carbon-multi-page@2.0.0/node_modules/.pnpm/@magidoc+plugin-query-generator@2.0.0/node_modules/@magidoc/plugin-query-generator/build/generator/fakeGenerator.js:34:23)

ps. I'm using Hasura 2.7.0

I'll try to debug it

@pelletier197
Copy link
Collaborator

pelletier197 commented Jun 7, 2022

Thanks. Those all seem reasonable to add to the default factories.

About your error, thanks for taking the time to check it.

I'll check it out as well when I get some time. Would it be possible for you to provide me with the _schema.json file for your API? If not possible, a minimal reproduction schema would be great otherwise.

I feel like this error message needs more detail as well.

@efstathiosntonas
Copy link
Author

efstathiosntonas commented Jun 7, 2022

Sorry but I cannot share anything, it's a private repo. :(

It fails on the account_insert_input, this is how I found it:

 if (!isNullableType(input)) {
        console.log(
          "type: ",
          unwrappedType,
          "input: ",
          input,
          "name: ",
          unwrappedType.name
        );
        throw new Error(
          "Fatal error: A recursive input with a non-nullable circular reference was detected, which makes it impossible to generate a query. If you ever see this error, it means that your GraphQL uses an invalid schema."
        );
      }

this spits out type: "account_insert_input" input: "account_insert_input!" name: account_insert_input, this input type has 0 non-null types and this table has some (ORM) table connections: eg:

input account_insert_input {
    about_me: String,
    show_seen_sent: Boolean
    targeted_matches: match_arr_rel_insert_input
    time_format: String
    unban_reason: String
    unbanned_at: timestamptz
    unbanned_by: account_obj_rel_insert_input
    unbanned_by_id: String
    updated_at: timestamptz
    verified_by: account_obj_rel_insert_input
    verified_by_id: String
   devices: device_arr_rel_insert_input
}

where device_arr_rel_insert_input type is:

"input type for inserting array relation for remote table \"device\""
input device_arr_rel_insert_input {
    data: [device_insert_input!]!
    "upsert condition"
    on_conflict: device_on_conflict
}

account_obj_rel_insert_input type is:

"input type for inserting object relation for remote table \"account\""
input account_obj_rel_insert_input {
    data: account_insert_input!
    "upsert condition"
    on_conflict: account_on_conflict
}

wish I could provide more info, best thing you can do is to run a Hasura docker instance and create a simple database. Hasura has weird schema in general.

@pelletier197
Copy link
Collaborator

Okay, I think I get the issue. I'll open another bug for that. This is recursion with two layers, I'm gonna see how I can fix this, not an easy one.

In the meantime, you could probably do something like this if you're blocked and it's the only thing like this in your API.

{
  queryGenerationFactories: {
    device_arr_rel_insert_input: null,
    'device_arr_rel_insert_input!': {} // an example
   }
}

Thanks for all the help by the way, really appreciated!

@efstathiosntonas
Copy link
Author

efstathiosntonas commented Jun 7, 2022

You're welcome @pelletier197, and again, thanks for this great generator. If you need anything please tag me! If you want to get exactly the same experience like mine then visit Hasura Getting Started.

@pelletier197
Copy link
Collaborator

The issue with the recursion should be fixed now in 2.1.0, along with a few other improvements. Let me know if you have other issues.

I have not implemented the default types of Hasura yet, cause I have a little bit of confusion looking at the types example you shared compared to Hasura's docs. I'll need a little more research before implementing them. See #93 for more details.

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

No branches or pull requests

2 participants