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

feat(graphql-config): add graphql config extensions #1118

Merged
merged 11 commits into from
Mar 14, 2020
Merged

feat(graphql-config): add graphql config extensions #1118

merged 11 commits into from
Mar 14, 2020

Conversation

divyenduz
Copy link
Contributor

This PR is a port of graphql/graphql-language-service#240

It adds GraphQL config extension support to the language server.

This would enable the following scenarios (not-exhaustive):

  1. Using environment variables (or variables in a custom format)
  2. Supply customDirectives dynamically - can be tool specific like
    Gatsby
  3. Supply customValidationRules dynamically
  4. Add endpoints, headers dynamically

This PR is a port of graphql/graphql-language-service#240

It adds GraphQL config extension support to the language server.

This would enable the following scenarios (not-exhaustive):

1. Using environment variables (or variables in a custom format)
2. Supply customDirectives dynamically - can be tool specific like
Gatsby
3. Supply customValidationRules dynamically
4. Add endpoints, headers dynamically
let config = getGraphQLConfig(rootPath);
if (this._extensions && this._extensions.length > 0) {
/* eslint-disable no-await-in-loop */
for (let i = 0; i < this._extensions.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have support for the latest language features now, so if you want to use for... await of here you can! I think Its node 9+.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumped the loop to new syntax but kept /* eslint-disable no-await-in-loop */ becuase lint still disables it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@divyenduz interesting note, perhaps we should change this rule now that we have modern for...await of

@acao
Copy link
Member

acao commented Dec 17, 2019

this looks great @divyenduz, thanks!

@codecov
Copy link

codecov bot commented Dec 17, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@b0edf6e). Click here to learn what that means.
The diff coverage is 42.85%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #1118   +/-   ##
=========================================
  Coverage          ?   43.98%           
=========================================
  Files             ?       64           
  Lines             ?     3008           
  Branches          ?      652           
=========================================
  Hits              ?     1323           
  Misses            ?     1414           
  Partials          ?      271
Impacted Files Coverage Δ
...graphql-language-service-server/src/startServer.js 0% <0%> (ø)
...raphql-language-service-server/src/GraphQLCache.js 54.93% <50%> (ø)
...ql-language-service-server/src/MessageProcessor.js 44.13% <66.66%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b0edf6e...9e3e973. Read the comment docs.

@acao
Copy link
Member

acao commented Dec 17, 2019

oh dear, looks like codecov blocked it. but yeah this is right up our alley in terms of our intended route with graphql config. I’m looking forward to getting the LanguageService class and its own GraphQLCache working in the browser soon. We may end up figuring out how graphql config could be one config service, or you can bring your own, but that seems too complicated. different platforms want to have their own config formats, but maybe we can introduce something to graphql-config where they could specify different extensions and map their values to graphql-config values?

@divyenduz
Copy link
Contributor Author

but maybe we can introduce something to graphql-config where they could specify different extensions and map their values to graphql-config values?

That is interesting indeed, are you suggesting that we move this feature from here into the GraphQL config? IIRC they had some extension work done in GraphQL Config back then, we chose to do this feature back then in LSP because we wanted dynamic config changes (my memory on this is faded, but we can surely re-think it)

@Urigo
Copy link
Collaborator

Urigo commented Dec 17, 2019

@kamilkisiela has already introduced a start for extending GraphQL Config for different tools in the new alpha.

@divyenduz we would love to chat and see if that fits your use case or should we change anything!

@acao
Copy link
Member

acao commented Dec 20, 2019

@divyenduz just needs some more test coverage to get builds passing! maybe we can get this merged before the typescript conversion is merged?

@divyenduz
Copy link
Contributor Author

@acao When do you plan to merge your refactor? I am traveling over the weekend. I can maybe get to this on 26th? (or will see if I can find a moment from an airport or something 😅)

@acao
Copy link
Member

acao commented Dec 20, 2019

it’s almost finished, just two more tests, but then itll need plenty of time to review. that should be plenty of time! enjoy your holidays, thank you @divyenduz !

@ardatan ardatan mentioned this pull request Jan 14, 2020
2 tasks
@acao
Copy link
Member

acao commented Jan 18, 2020

@divyenduz this one should be relatively easy to update for typescript when you get a chance? can you update docs as well?

@divyenduz
Copy link
Contributor Author

divyenduz commented Jan 18, 2020

@acao Is this even valid after #1225 is merged? I need to build more context on that but it looks like they are removing the extensions feature, making this PR already outdated 😅

@acao
Copy link
Member

acao commented Jan 18, 2020

@divyenduz I think they are deprecating the endpoints extension specifically, however I think extensions will remain!

https://graphql-config.com/docs/introduction

so this feature is very useful.

@ardatan @kamilkisiela agreed this is still compatible with 3.0.0-alpha-n? on top of the graphql-config migration PR @ardatan already created?

@acao
Copy link
Member

acao commented Jan 18, 2020

@divyenduz an alpha release is now available :D

@acao acao mentioned this pull request Feb 16, 2020
2 tasks
@acao acao added this to In progress in 3. Complete LSP Features via automation Mar 10, 2020
@@ -46,10 +47,13 @@ type Options = {
// port for the LSP server to run on
port?: number;
// socket, streams, or node (ipc). if socket, port is required
method?: 'socket' | 'stream' | 'node';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks incorrect. I think this should be 'socket' | 'stream' | 'node'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed via f6d8a56

@divyenduz
Copy link
Contributor Author

@acao I wonder why CI tests that blocked me last time didn't appear this time? 🤔

@acao
Copy link
Member

acao commented Mar 12, 2020

@divyenduz that is strange, i wonder if there's an issue with github actions?

@acao
Copy link
Member

acao commented Mar 12, 2020

possibly because this PR pre-dates the switch to github actions?
but that wasnt an issue when we first switched?
was it a travis failure or gh actions failure before?

@divyenduz
Copy link
Contributor Author

@acao haha, weird indeed, I was talking about this comment: #1118 (comment)

Maybe GH actions don't run for older PRs. I can create a new PR and replace this one with that. Does that sounds good?

@acao
Copy link
Member

acao commented Mar 13, 2020

oh right! i have not restored the codecov behavior in github actions, my bad, forgot to do that! yikes.

@divyenduz
Copy link
Contributor Author

But still, even the tests did not run! 🤔

I see that they are running for other PRs. I will just re-create this one to see if it runs.

@divyenduz
Copy link
Contributor Author

divyenduz commented Mar 14, 2020

"Workflows in forked repositories don't run by default." via https://help.github.com/en/actions/getting-started-with-github-actions/about-github-actions

I couldn't find the docs to enable it though, looking for it.

✅ Green in my fork: https://github.com/divyenduz/graphiql/runs/507245373?check_suite_focus=true

Haha! Let me know how to proceed, we can

  1. Merge this one and clean it up later! (by adding codecov + tests)
  2. Clean it up now

I would prefer 1 for now, just to get this PR out of the door, it has been looooong living 😄


Update: I think this is how we enable it on forked repos: https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-events-for-forked-repositories

By adding pull-request as an event to the workflow.

But it looks like this doesn't work: https://github.community/t5/GitHub-Actions/Github-Workflow-not-running-from-pull-request-from-forked/td-p/32979

Quickly trying it out in this branch.

@divyenduz
Copy link
Contributor Author

Yes it works! Precioussssss! <3

Comment on lines 122 to 127
if (this._extensions && this._extensions.length > 0) {
/* eslint-disable no-await-in-loop */
for (const extension of this._extensions) {
config = await extension(config);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this is required here since we do the same thing in the getGraphQLCache function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it, nice, this was a code smell with duplicate code. Moving config to cache solves this 👍

@acao
Copy link
Member

acao commented Mar 14, 2020

nice work! thanks @divyenduz

if (extensions && extensions.length > 0) {
/* eslint-disable no-await-in-loop */
for (const extension of extensions) {
graphQLConfig = await extension(graphQLConfig);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@divyenduz so this is intended to re-declare graphQLConfig on every invocation? I'm guessing the result of await extension(graphQLConfig) returns the previous config plus the extension?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as of now, the signature of an extension is a function that takes a GraphQLConfig as input and returns a GraphQL config as its output with the modification (extension) applied.

extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>
Which implies, extension: (config: GraphQLConfig) => GraphQLConfig

@acao
Copy link
Member

acao commented Mar 14, 2020

maybe we can update docs, as well?

@acao
Copy link
Member

acao commented Mar 14, 2020

https://deploy-preview-1118--graphiql-test.netlify.com/lsp/modules/graphql_language_service_server.html#getgraphqlcache fyi, interesting how tsdoc handles this as Array<function>. I think I need to tweak the tsdoc configuration (another matter)

@divyenduz
Copy link
Contributor Author

Added a test, figuring out where the docs live!

Does your tsdoc comment mean that we get docs for free (after we fix the array function representation?) or do I need to do something else as well? :)

@acao
Copy link
Member

acao commented Mar 14, 2020

@divyenduz good question! let’s put the docs for this in graphql-language-service for now. there will be a doc site soon.

the lsp api docs are nice but not instructive enough yet, or fully configured, and the readme’s come with

@divyenduz
Copy link
Contributor Author

@acao Added some docs to the README of graphql-langauge-service package. Please see if that makes sense :)

@divyenduz
Copy link
Contributor Author

@acao I did a split of CLI and Server docs as discussed. There is some duplication in the two readme files, please let me know if any further changes are needed :)

@acao
Copy link
Member

acao commented Mar 14, 2020

@divyenduz looking great! we are ready to merge then

@acao acao changed the title feat(extensions): add graphql config extensions feat(graphql-config): add graphql config extensions Mar 14, 2020
@acao acao merged commit 2a77e47 into graphql:master Mar 14, 2020
3. Complete LSP Features automation moved this from In progress to Done Mar 14, 2020
@divyenduz divyenduz deleted the feat-extensions branch October 21, 2022 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants