-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Some enhancements for hooks #1347
Conversation
Signed-off-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
de2fb45
to
6c3819a
Compare
Signed-off-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
6c3819a
to
1f7e084
Compare
@@ -52,13 +52,13 @@ Or if you prefer: | |||
Customize from where will be `gql` imported. This is useful if you want to use eg. `graphql.macro` for inlining documents. | |||
Note that you need to write whole import line, eg. `import { gql } from 'graphql.macro'`. | |||
|
|||
#### `noHOC` (default value: `false`) | |||
#### `withHOC` (default value: `true`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like unnecessary breaking change just for switching logic around. If anything, it should still support old flags, just deprecate them and convert into these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#### `withSubscriptionHooks` (default value: `false`) | ||
|
||
This will cause the codegen to add React **Hooks** even for _Subscriptions_. | ||
You can specify alternative module that is exports `useQuery` `useMutation` and `useSubscription`. This is useful for further abstraction of some common tasks (eg. error handling). Filepath relative to generated file can be also specified. But if you provide only `true`, it will use `react-apollo-hooks` by default. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't feel right to abuse the boolean flag to also have a string value. I would keep it separate, nothing wrong about.
const config = options.data.root.config || {}; | ||
return operationType !== 'subscription' || config.withSubscriptionHooks; | ||
return config.gqlImport || "import gql from 'graphql-tag'"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not include this in the getImports
as well?
): Promise<string> => { | ||
config.withHOC = 'withHOC' in config ? config.withHOC : true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a personal preference, probably not a big different
config.withHOC = 'withHOC' in config ? config.withHOC : true; | |
config.withHOC = config.withHOC !== undefined ? config.withHOC : true; |
{{#if @root.config.withHooks}} | ||
import * as ReactApolloHooks from 'react-apollo-hooks'; | ||
{{/if}} | ||
{{{getImports}}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got burned by this as well :)
{{{getImports}}} | |
{{{getImports this}}} |
I also think there is another inconsistency between |
Yea well, if you have a better idea... I've explained in #1344 |
Btw, that's also why there is a different name |
We use |
Ok, it's an interesting approach, I suppose it will work, but it requires a better documentation as it's not the usual approach :)
Yea, this config option is not even documented, so I doubt someone was using it :) What do you think @dotansimha?
I guess it makes sense, not sure who would actually use that, but 🙈
I don't think it makes a difference to have one or two extra configs. It's a DEV tool after all. It's more about clarity so it's apparent from the first view what each config does. You shouldn't be forced look into docs every time you need to tweak something. |
I'd cast a vote for different options (like I originally implemented them). More options, with clean docs, allows us to output clear errors if plugin is configured badly. I may understand something like we accepts a list of foos or just a single foo, but switching boolean with string seems not so useful in this case. However, enough bikeshedding for today! 🎉 |
- Add gqlImport config option - Remove `withSubscriptionHooks` - Fix missing `ReactApollo` import if `noComponents` is true, but `noHOC` is false. - Fixed invalid import syntax - `gqlImport` is more consistent with other import syntaxes in codegen. `gqlImport: graphql.macro#gql`
- Add gqlImport config option - Remove `withSubscriptionHooks` - Fix missing `ReactApollo` import if `noComponents` is true, but `noHOC` is false. - Fixed invalid import syntax - `gqlImport` is more consistent with other import syntaxes in codegen. `gqlImport: graphql.macro#gql`
@ardatan @FredyC But, what if I want to use Example with import gql from "graphql-tag.macro";
import * as ReactApolloHooks from "react-apollo-hooks";
// ====================================================
// Fragments
// ====================================================
export const WitnessInfoFragmentDoc = {
kind: "Document",
definitions: [
{
kind: "FragmentDefinition",
name: { kind: "Name", value: "WitnessInfo" },
typeCondition: {
kind: "NamedType",
name: { kind: "Name", value: "Witness" }
},
directives: [],
selectionSet: {
kind: "SelectionSet",
selections: [
{
kind: "Field",
name: { kind: "Name", value: "id" },
arguments: [],
directives: []
},
{
kind: "Field",
name: { kind: "Name", value: "firstName" },
arguments: [],
directives: []
},
{
kind: "Field",
name: { kind: "Name", value: "lastName" },
arguments: [],
directives: []
},
{
kind: "Field",
name: { kind: "Name", value: "croppedImage" },
arguments: [],
directives: []
}
]
}
}
],
loc: { start: 0, end: 103 }
}; How can I stop parsing the GraphQL queries and use I am trying to understand the logic of this 23894f0#diff-518180923419f5ff9c24c6d9b7d61651R86 return config.gqlImport ? JSON.stringify(gqlTag(doc)) : 'gql`' + doc + '`'; If gqlImport is not Probably you should consider showing a warning message if |
@sandiiarov Do you realize that's what is the macro about? To inline parsed document so it doesn't need to be done on runtime :) |
@FredyC Yes, I do. Do you understand what I am trying to ask? Do you understand how |
When I use import gql from "graphql-tag.macro";
export const WitnessInfoFragmentDoc = gql`
fragment WitnessInfo on Witness {
id
firstName
lastName
}
`; but getting this import gql from "graphql-tag.macro";
export const WitnessInfoFragmentDoc = {
kind: "Document",
definitions: [
{
kind: "FragmentDefinition",
name: { kind: "Name", value: "WitnessInfo" },
typeCondition: {
kind: "NamedType",
name: { kind: "Name", value: "Witness" }
},
directives: [],
selectionSet: {
kind: "SelectionSet",
selections: [
{
kind: "Field",
name: { kind: "Name", value: "id" },
arguments: [],
directives: []
},
{
kind: "Field",
name: { kind: "Name", value: "firstName" },
arguments: [],
directives: []
},
{
kind: "Field",
name: { kind: "Name", value: "lastName" },
arguments: [],
directives: []
}
]
}
}
],
loc: { start: 0, end: 88 }
}; @FredyC Does it make sense to you? |
Ah, I see what you mean now. Looks like that @DAB0mB introduced that mistake when merging some of @ardatan changes in 859de36. Originally there was different option (which wasn't really documented). config.noGraphqlTag ? JSON.stringify(gqlTag(doc)) : 'gql`' + doc + '`'; The logic should be reversed with |
@FredyC you are right, we fixed it in the refactor :) |
withSubscriptionHooks
withHooks
option to provide custom module for hooks.ReactApollo
import ifnoComponents
is true, butnoHOC
is false.noComponents
andnoHOC
towithComponents
andwithHOC
which look more consistent withwithHooks
.gqlImport
is more consistent with other import syntaxes in codegen.gqlImport: graphql.macro#gql