-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Support for Node #32
Comments
@smeijer Yep. You can simply follow the instructions over at https://gqless.netlify.com and skip the react bit You should access the data twice, the first time to fetch it - the second time to get its value It may be possible to make it work synchronously, but currently it's async |
@samdenty any chance to optionally have a client where you can simply await queries? |
@adrianhunter yep that's the goal function test() {
console.log(query.name)
}
await preload(test)
test() // => Bob |
cool, also just await query.foo ? |
Released a new You can pass a GraphQL accessor: const title = await resolved(query.hn!.topStories[0]!.title)!;
console.log(title); Passing a callback will keep executing the callback until everything's been fetched, and then resolve the final return value: const data = await resolved(() => ({
title: query.hn!.topStories[0]!.title,
id: query.hn!.topStories[0]!.id
}))!;
console.log(data); See a demo here: |
Documenting this would be huge. We’re looking at gqless because honestly Apollo is sort of appalling how verbose it is. Being able to run queries independently is super important for global state stores and non React usage obviously. Beyond that optimistic updates that “just work” would be a huge killer feature. I’d be happy to jump in and help with either feature. |
@natew Started updating the docs over the weekend. Should be done sometime next week #74 End-goal is to automatically turn your GraphQL client into a smart SDK. Reactive SDK's like discord.js are waaaay easier than making rest calls / GraphQL requests. But they just cost too much to implement to that degree - plus they have overfetching. Stuff which GraphQL can solve
Yep totally agree, gonna be essentially mobx - but the data is bidirectionally synced with your GraphQL server. |
This is awesome! I'm having some trouble pulling out array items from the resolver function though. For example if I want to get all the top stories' titles: const stories = await resolved(query.hn!.topStories.map(story => story.title)); This is not working. Any ideas on how to make it work?
|
@rhlsthrm turn that argument into a callback const stories = await resolved(() =>
query.hn!.topStories.map(story => story.title)
); |
@samdenty that worked perfectly! Thank you for the assistance. |
You can also do this, albeit looks worse: await resolved(() => {
// Access the data we want to resolve
query.hn!.topStories.forEach(story => story.title);
});
// Data we requested is now resolved
const stories = query.hn!.topStories.map(story => story.title);
console.log(stories); |
Another q @samdenty (sorry if this issue isn't the best place). How would I use this same type of approach to run a const stories = await resolved(() =>
query.hn!.topStories
.map(story => {
return { title: story.title, date: story.date };
})
.filter(story => story.date > YESTERDAY)
); Edit, this actually works perfectly but I'm wondering if this is the "right" way to do it. Some more examples along these lines would be super helpful. Loving this tool though! |
@rhlsthrm first you have to understand how the await resolved(() => {
// Access the data we want to resolve
query.hn!.topStories.forEach(story => story.title); // <- why forEach here?
}); The answer is that he had to inform Then in the next line after await resolved(() => {
query.hn!.topStories.forEach(story => {
story.title;
story.date;
});
});
const stories = query.hn!.topStories;
stories.filter(story => story.date > YESTERDAY); Maybe you would also add |
@lukejagodzinski thanks for that. This helped my understanding about "informing const stories = await resolved(() =>
query.hn!.topStories
.where({
read: false
})
.map(story => {
return {
title: story.title,
read: story.read
};
})
); Now I am informing the resolved query what my filter params need to be before mapping. Let me know what you think about this. |
@rhlsthrm what is the await resolved(() => {
query.hn!.topStories.forEach(story => {
story.title;
story.read;
});
});
const stories = query.hn!.topStories
.filter(story => story.read === false)
.map(story => {
return {
title: story.title,
read: story.read
};
}); |
The subgraph I am interacting with is using For example, if we look at this subgraph: https://lucasconstantino.github.io/graphiql-online/ And I want to run a query like this: query {
allUsers(filter: { name_starts_with: "A" }) {
id
name
}
} I would think I would query by writing: const stories = await resolved(() =>
query.allUsers
.filter({
name_starts_with: "A"
})
.map(user => {
return {
id: user.id,
name: user.name
};
})
); To me this seems "right", and the filter fields actually autocomplete from TypeScript from the generated code from the schema. |
@rhlsthrm await resolved(() => query.users); You start building query, so until that point your query will look like: query {
users {
__typename
}
} Now you want to specify what user properties you want to fetch: await resolved(() => query.users.forEach(user => {
user.firstName;
user.lastName;
user.posts;
})); Just by "touching" (accessing) some properties you add it to the query. So now your query looks like: query {
users {
__typename
firstName
lastName
posts {
__typename
}
}
} You can move on and add more nested properties: await resolved(() => query.users.forEach(user => {
user.firstName;
user.lastName;
user.posts.forEach(post => {
post.title;
post.content;
});
})); Now, your query looks like: query {
users {
__typename
firstName
lastName
posts {
__typename
title
content
}
}
} In this process, you're just telling |
Actually if I understand it correctly you don't even need to do forEach, you could just do: await resolved(() => {
const user = query.users[0];
user.firstName;
user.lastName;
const post = user.posts[0];
post.title;
post.content;
}); |
@lukejagodzinski thanks for all the help, I'm starting to understand more and more. The one thing your examples are leaving out is how to add the This is the query I want to generate: query {
allUsers(filter: { name_starts_with: "A" }) {
id
name
}
} I don't want to just fetch all the users, I want to fetch users based on a condition.
I don't understand how this could work. I want more than the [0] indexed user, I want all the users based on a condition. |
I just checked the generated query using what I posted earlier: const stories = await resolved(() =>
query.allUsers
.filter({
name_starts_with: "A"
})
.map(user => {
return {
id: user.id,
name: user.name
};
})
); The above code generates the following query: query {
allUsers(filter: { name_starts_with: "a" }) {
id
name
}
} This is exactly what I need, so I will go ahead and proceed with this. Thanks again for the help @lukejagodzinski. |
@rhlsthrm, are you sure? Shouldn't the filter be a function argument to get that query?: const stories = await resolved(() =>
query
.allUsers({
filter: { name_starts_with: 'A' },
})
.map(user => ({
id: user.id,
name: user.name,
}))
); @samdenty, I guess we can close this issue now that we have |
@smeijer you're totally right, this was a bad copy/paste on my part. Your syntax is perfect. |
Oh filter is an argument of query Hehe so that's why all this confusion :-). And about forEach, yes you don't need to run it. It's just about visiting at least one field from the object. Even if you visit element |
Oh this is interesting! I will experiment with this. |
I've closed this one, as |
Is there any easy way to extract all fields w/o touching or an easy way to touch them all - preferably built into GQless, without having to build anything into |
|
Can this also be used for command-line apps? Or does it depend on react?
It looks quite awesome for fast prototyping and quering a graphql endpoint in a node repl.
The text was updated successfully, but these errors were encountered: