Skip to content

feat: schema provider and operation session context using context/useReducer#1359

Closed
acao wants to merge 20 commits intomasterfrom
feat/use-context-hooks
Closed

feat: schema provider and operation session context using context/useReducer#1359
acao wants to merge 20 commits intomasterfrom
feat/use-context-hooks

Conversation

@acao
Copy link
Copy Markdown
Member

@acao acao commented Feb 21, 2020

please take this and run with it tomorrow however you see fit!
it introduces:

  • useReducers which is meant to expose interfaces (to plugins eventually down the line, but for now we will supply them as props via the provider). glad to field suggestions on how to re-architect this!
  • a few useReducer related utilities inspired from previous redux workflows
  • a test utility for RTL, for testing provider output more readily
  • SchemaProvider, SchemaContext, etc. feel free to combine this provider into a migration provider like you were discussing earlier @zephraph

typescript types are not happy yet because I'm still wrapping my head around typescript + redux style patterns

note:

some features are temporarily disabled by this effort in the interest of further iterations before we release another alpha:

  • subscriptions support
  • multiple operations per query
  • persisted operations (though your previous query history works in the left sidebar, soon to be deprecated)

@acao acao requested a review from AlecAivazis February 21, 2020 22:08
@acao acao added the graphiql label Feb 21, 2020
@acao
Copy link
Copy Markdown
Member Author

acao commented Feb 28, 2020

@zephraph going to break some of these interfaces, since this PR breaks GraphiQL.state we might as well get rid of the GraphiQL.Logo pattern as well. See: pinned issue

Comment thread packages/graphiql/src/state/MigrationContext.tsx Outdated
Comment thread packages/graphiql/src/state/MigrationContext.tsx Outdated
Comment thread packages/graphiql/src/state/MigrationContext.tsx Outdated
@acao
Copy link
Copy Markdown
Member Author

acao commented Mar 10, 2020

This is mostly ready, aside from some some cleanup.

Last step, was thinking of bringing schema context into DocExplorer using just render props, since it doesn't seem to be re-rendering from the top via GraphiQL.context.schema since we got rid of GraphiQL.state.schema

@acao acao force-pushed the feat/use-context-hooks branch 2 times, most recently from 6b3e9af to 20ed39a Compare March 12, 2020 20:01
@acao acao added this to the GraphiQL-1.0.0-beta milestone Mar 13, 2020
@acao acao changed the title feat: schema provider using context/useReducer feat: schema provider and oepration session context using context/useReducer Mar 15, 2020
Comment thread packages/graphiql/src/components/ExecuteButton.tsx Outdated
@acao acao force-pushed the feat/use-context-hooks branch from 336c32c to a174c58 Compare March 16, 2020 15:41
Comment thread packages/graphiql/src/components/QueryEditor.tsx Outdated
Comment thread packages/graphiql/src/components/QueryEditor.tsx Outdated
Comment thread packages/graphiql/src/components/QueryEditor.tsx
@acao
Copy link
Copy Markdown
Member Author

acao commented Mar 17, 2020

@AlecAivazis welcome back! :) did you add yourself as a reviewer?

@acao acao changed the title feat: schema provider and oepration session context using context/useReducer feat: schema provider and operation session context using context/useReducer Mar 17, 2020
@acao acao force-pushed the feat/use-context-hooks branch from 31fc6e0 to 246c51e Compare March 17, 2020 15:58
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 20, 2020

Codecov Report

Merging #1359 into master will decrease coverage by 22.04%.
The diff coverage is 0%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master    #1359       +/-   ##
===========================================
- Coverage   58.96%   36.92%   -22.05%     
===========================================
  Files          60       64        +4     
  Lines        3022     2941       -81     
  Branches      796      731       -65     
===========================================
- Hits         1782     1086      -696     
- Misses       1194     1833      +639     
+ Partials       46       22       -24
Impacted Files Coverage Δ
...ages/graphiql/src/state/GraphiQLSchemaProvider.tsx 0% <0%> (ø)
packages/graphiql/src/components/QueryEditor.tsx 0% <0%> (-63.74%) ⬇️
packages/graphiql/src/components/GraphiQL.tsx 0% <0%> (-58.9%) ⬇️
packages/graphiql/src/components/ResultViewer.tsx 0% <0%> (-73.81%) ⬇️
packages/graphiql/src/state/common.ts 0% <0%> (ø)
packages/graphiql/src/components/ExecuteButton.tsx 0% <0%> (-42.86%) ⬇️
...ges/graphiql/src/state/GraphiQLSessionProvider.tsx 0% <0%> (ø)
...ackages/graphiql/src/components/VariableEditor.tsx 0% <0%> (-62.83%) ⬇️
packages/graphiql/src/state/useReducers.ts 0% <0%> (ø)
packages/graphiql/src/utility/CodeMirrorSizer.ts 0% <0%> (-100%) ⬇️
... and 35 more

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 cd77a51...c9d29e9. Read the comment docs.

? 'http://localhost:8080/graphql'
: 'https://swapi-graphql.netlify.com/.netlify/functions/index';

export const initialReducerState: SchemaState = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it possible to use discriminated unions so invalid states are impossible to represent?

A simplified example (Reason syntax since it's terser, but I think TS can represent this):

type schemaState = 
 | Loading
 | Error(string)
 | Loaded(schema)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is just temporary for the demo, but yes it is, via typescript. that would come in a later PR

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

lets talk about state in the plugin spec

Copy link
Copy Markdown
Collaborator

@cshaver cshaver left a comment

Choose a reason for hiding this comment

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

I haven't looked at this too closely yet, but I noticed one small thing where fetch is used:

query: introspectionQuery,
operationName: introspectionQueryName,
}),
headers: { 'Content-Type': 'application/json', credentials: 'omit' },
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
headers: { 'Content-Type': 'application/json', credentials: 'omit' },
headers: { 'Content-Type': 'application/json' },
credentials: 'omit',

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

oops good catch!

const rawResult = await fetch(schemaConfig.uri, {
method: 'post',
body: JSON.stringify(graphqlParams),
headers: { 'Content-Type': 'application/json', credentials: 'omit' },
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
headers: { 'Content-Type': 'application/json', credentials: 'omit' },
headers: { 'Content-Type': 'application/json' },
credentials: 'omit',

@acao acao added rfc Request for Comment work-in-progress not ready to merge yet, PR is available for demonstration/review purposes labels Apr 1, 2020
@acao
Copy link
Copy Markdown
Member Author

acao commented Apr 5, 2020

@cshaver if you notice anything funky with tests, its because they're disabled temporarily for the RFC puproses

@ryan-m-walker is also around to help, he helped me start this PR!

{options}
</div>
export function ExecuteButton(props: ExecuteButtonProps) {
const [optionsOpen, setOptionsOpen] = useState<boolean>(false);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the generic isn't needed here. the type can be inferred

payload: { value: variablesText, sessionId },
});
const executeOperation = async (
sessionState: SessionState,
Copy link
Copy Markdown
Contributor

@ryan-m-walker ryan-m-walker Apr 5, 2020

Choose a reason for hiding this comment

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

it feels kind of strange that you have to pass the session state back into this function. Shouldn't the outer scope provide everything this function needs or are you trying to just encapsulate it all? lines like this one just feel strange to me:

https://github.com/graphql/graphiql/pull/1359/files#diff-de4c1a6e083a392d7c4acd996d95106dR49

null,
);
const session = useSessionContext();
const operations = props.operations ?? [];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we be getting operations from the session state now instead of props?

ryan-m-walker and others added 8 commits April 5, 2020 22:36
* made errors consitent across providers
* allowed for multiple errors
* preserved error objects to allow more custom usage
* eslint-plugin-react-hooks
* Move query facts out of session state
* Rename sessionReducer
* used the return type of the action creators for the action types
@acao
Copy link
Copy Markdown
Member Author

acao commented Apr 6, 2020

closing for #1468, in favor of using a fork for access management purposes.

@acao acao closed this Apr 6, 2020
@acao acao reopened this Apr 6, 2020
@acao acao closed this Apr 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

graphiql rfc Request for Comment work-in-progress not ready to merge yet, PR is available for demonstration/review purposes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants