question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

allow transforming query text before passing it to `graphql-language-service-interface`

See original GitHub issue

I’d like to be able to append to editor’s query text (or more generally transform it) before sending it to graphql-language-service-interface.

Motivation

In https://github.com/gatsbyjs/gatsby we allow supplying fragments that users can use freely in their queries (we transform queries and attach used fragment definitions before query execution with the help of relay-compiler). This works well in queries to users write in their source code, but doesn’t work currently in graphiql because those 3rd party fragments are not in document and codemirror-graphql/lint report errors (missing fragment definitoins) and codemirror-graphql/hint also can’t provide autocompletion hints for those fragments it doesn’t know about.

Proposed design

Allow passing extra option to addons - for example transformQueryText function (that will default to identity transform if not specified), that will be exucuted before passing query text to graphql-language-service-interface functions (getDiagnostics for lint / getAutocompleteSuggestions for hint):

CodeMirror.fromTextArea(myTextarea, {
  mode: 'graphql',
  lint: {
    schema: myGraphQLSchema,
    transformQueryText: queryText => doSomethingWithQueryText(queryText)
  },
  hintOptions: {
    schema: myGraphQLSchema,
    transformQueryText: queryText => doSomethingWithQueryText(queryText)
  }
});

Example of this in action:

In here I make use of fragments that are not in document, but are supplied by Gatsby, and both lint (no “unknown fragment” error) and hint (autocompletion for fragments supplied by Gatsby) works as expected for Gatsby usecase: Screenshot 2019-05-25 at 18 48 05

Above screenshot is result of some monkey patching (just to to make proof of concept that this can work).

I would be happy to submit pull request, but wanted to check first if this is something that would be welcome here.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
imolorhecommented, Nov 26, 2021

@acao Looking at this, I think the external fragments solution should be sufficient for this? Are there potentially any other use cases where this ability to transform the query before sending to LSP (which specifically means affecting the linting and hinting only) makes sense?

0reactions
acaocommented, Nov 26, 2021

though… for just the purpose of manipulating operations… I’m remembering that this is a goal of @onegraph folks for explorer, and it provides us a cleaner way of implementing externalFragments without changing the external API.

when it makes sense to manipulate graphql documents - when executing operations - which happens using graphiql or playground or vscode-graphql extension client, you don’t have as much control over being able to manipulate documents appending externalFragments is nice, but a method for manipulating the query AST before execution and optionally providing language features is neat.

other thoughts:

  • custom graphiql content pane plugins could, amongst other things, provide custom operation editor hooks for manipulating AST (in the background even) in a custom operation (monaco or text-editor free) component.
  • codegen config could potentially drive some of this, where there is customisation of operation documents (i.e., patterns such as appending an id parameter to each object).
  • vscode-graphql has a provider for custom text editors that could be used to implement something like onegraph explorer.
Read more comments on GitHub >

github_iconTop Results From Across the Web

4 Simple Ways to Call a GraphQL API
Passing the GraphQL query to the useQuery hook gives us an object that can be de-structured into loading , error , and data...
Read more >
Queries and Mutations - GraphQL
On this page, you'll learn in detail about how to query a GraphQL server. Fields#. At its simplest, GraphQL is about asking for...
Read more >
Designing a URL-based query syntax for GraphQL
I suspect, though, that all attempts to pass a GraphQL query via a URL param will be far from ideal. This is because...
Read more >
Moving Existing API From REST To GraphQL - YouTube
GraphQL is an exciting new query language that's transforming the way we think about APIs. Used in production by Facebook, GitHub, ...
Read more >
GraphQL Content API - Contentful
The GraphQL Content API provides a GraphQL API interface to the content from Contentful. Each Contentful space comes with a GraphQL schema based...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found