-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
[Question] how to use variables #224
Comments
Hi @germsb , Thank you for your interest. You can see a mutation using variable: https://github.com/acro5piano/typed-graphqlify#basic-mutation However, using |
Can you create an example of how to create a query with variables using both methods you mention please. The mutation example is confusing as it is terse and talks about aliases. |
In general, there are two methods to add dynamic parameters in GraphQL. One is In terms of standard GraphQL server implementation, Now that TypeScript supports As you said, import { mutation, params, rawString } from 'typed-graphqlify'
mutation('updateUserMutation', {
updateUser: params(
{
input: {
name: rawString('Ben'),
slug: rawString('/ben'),
},
},
{
id: types.number,
name: types.string,
},
),
}) |
Thanks for that. I think an example for a simple query with variables would be useful for developers like myself who are not GQL experts and are mainly trying to make things work due to time pressure and have simple requirements. I had been looking for a solution for efficient typing GQL and typescript, Apollo had a code-get tool that would query the schema and create types which was handy but is now deprecated in favour of the code-gen from the guild, but that solution is too strict and prescriptive for my liking. Your approach is much more flexible and elegant however the documentation is more code comments rather than the hand-holding a new comer to your package like myself would need. I understand documentation is time consuming so code examples can fill that gap. I spent an hour trying to implement a simple query with variables because the syntax doesn't make intuitive sense to me and came up with this, it was the only combination which produced the GQL string I needed, but it's confusing as the repetition of the $ownerName and $stackFileName seems redundant, and the params are not visible to TS but it seems to be necessary? As there is no example in the docs I can't be sure. const getStack = query(
"GetStack($ownerName: String, $stackFileName: String)",
{ getStack: params({ ownerName: "$ownerName", stackFileName: "$stackFileName" }, { code: types.string }) },
);
request(clientConfig.gqlGetStack(), getStack.toString(), {
ownerName,
stackFileName,
}) which doesn't match the pattern you show above. Which still leaves me confused as to the "correctness" of what I'm doing. Again I appreciate the reply but it is pitched at a higher level when what would be more helpful to me would be an example of the recommended way to construct a simple query with parameters/variable. Could you add one to the reply to this if you have time? I'd appreciate it greatly. |
This works: const getStack = (ownerName: string, stackFilename: string) => {
return query("GetStack", {
getStack: params(
{ ownerName: rawString(ownerName), stackFileName: rawString(stackFilename) },
{ code: types.string },
),
});
};
request("myurl", getStack.toString()) |
@sirganya thanks for the code example and I'm happy you found the solution. If you sent a pull request for the documentation I'd be happy to merge it. Actually I recommend you to use graphql-codegen from the guild guy, as it provides a better type-safety and developer experience. I mostly use graphql-codegen for my serious projects and no problems so far - I sadly admit that typed-graphqlify is outdated for the current technology trend. |
Hello, thanks for your library, looks pretty cool.
The doc says:
But I did not find anything in doc or test on the use of variables.
It is possible to add an example ?
The text was updated successfully, but these errors were encountered: