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

Extract generated client queries #10195

Open
jh-grundfos opened this issue Nov 20, 2024 · 6 comments
Open

Extract generated client queries #10195

jh-grundfos opened this issue Nov 20, 2024 · 6 comments

Comments

@jh-grundfos
Copy link

Is your feature request related to a problem? Please describe.

Hey

I'm in need of a CLI or similar to extract all queries from a client app. The intended use is for mocking, testing and perhaps query whitelisting in production.

The Apollo CLI had this feature

https://github.com/apollographql/apollo-tooling/?tab=readme-ov-file#apollo-clientextract-output

However, that repo is actively being deprecated and is no longer maintainted, and not compatible with newer versions of Node.

I thought this might be something on your radar/roadmap as maintainers of

@graphql-codegen/cli and @graphql-codegen/client-preset ?

A button to export queries active on an environment on the Hive dashboard could also make sense, but maybe that request should be specifically made to the Hive team?

Describe the solution you'd like

A tool that extracts the queries in an app that uses @graphql-codegen/cli, preferably to a JSON output.

Describe alternatives you've considered

https://github.com/apollographql/apollo-tooling/?tab=readme-ov-file#apollo-clientextract-output

Any additional important details?

No response

@eddeee888
Copy link
Collaborator

Hi @jh-grundfos ,

Interesting! Could you please help me understand the behaviour by giving a small example of what the outcome would look like?

For example, if I have these two queries, what would be the outcome?

# src/User.graphql
query User {
  user(id: "1") {
    id
    name
  }
}

# src/Book.graphql
query Book {
  book(id: "1") {
    id
    isbn
  }
}

@jh-grundfos
Copy link
Author

Well, in my perfect world, you would be able to specify different output formats that could be expanded over time as new use cases emerge.

At present time, I can see these being a good starting point:

  • A Postman collection that includes all the queries and related fragments, and automatically populate the input variables with the relevant shape (but without values)
  • Similar for Insomnia - but I guess those are maybe the same format?
  • A JSON output where the JSON.stringified query/variable string is linked to operation name, i.e.
const output = {
  query: JSON.stringify(gqlQuery),
  queryName: "User",
  variables: `{id: ''}`,
};

The point of the JSON output format is flexibility, allowing users of the CLI to do what they want until the main CLI includes their use case.

But allowing output to tools like Postman would enable the use of that whole toolchain it enables - mocking, testing, performance testing etc., while also making it more comfortable for users not familiar with GraphQL to query the server, as they then only have to input the query variables to get a response.

@eddeee888
Copy link
Collaborator

To me, this is sounding less like the responsibility of Codegen CLI, and more like plugins: given a schema, the user can generate different targets using plugins. For example, it might look like this:

generates: {
  'src/postman': {
    plugins: ['postman']
  },
  'src/insomnia': {
    plugins: ['insomnia']
  }
}

You can look at the guide on how to write plugin here and look at our other community plugins in this repo

@jh-grundfos
Copy link
Author

jh-grundfos commented Dec 5, 2024

Another use case would be to do query whitelisting in production, to avoid malicious actors to perform random queries of high complexity without permission.

So essentially:

  1. Extract queries from client in pipelines
  2. Upload queries to trusted source/repo
  3. Server only allows those queries in production

Apollo has a similar service, but I'd love it if Hive supported something similar, and this would allow us to build that ourselves

EDIT: It would seem this plugin does the same for Hasura

@jh-grundfos
Copy link
Author

To me, this is sounding less like the responsibility of Codegen CLI, and more like plugins: given a schema, the user can generate different targets using plugins. For example, it might look like this:

generates: {
  'src/postman': {
    plugins: ['postman']
  },
  'src/insomnia': {
    plugins: ['insomnia']
  }
}

You can look at the guide on how to write plugin here and look at our other community plugins in this repo

Hm, you may be right :)

If the use case is wide enough and could provide extra value to the Hive product, is there any chance you/they could be interested in building the plugin?

@eddeee888
Copy link
Collaborator

eddeee888 commented Dec 9, 2024

Our current focus is currently on improving support for the core GraphQL workflows e.g. with Client Preset, Server Preset, Federation support etc.
Postman/Insomnia feels more like adjacent tools, so the plugin might need to be driven by the community (even if we built it) 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants