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

Issue on website code snippet #16

Closed
schettn opened this issue Sep 9, 2024 · 10 comments
Closed

Issue on website code snippet #16

schettn opened this issue Sep 9, 2024 · 10 comments

Comments

@schettn
Copy link

schettn commented Sep 9, 2024

I just saw this on your website and haven't tested it but I think the ...fields (autocompletion support) cannot work as fields isn't defined anywhere.

import spacex from "spacex";

const { company } = await spacex((op) =>
    op.query((q) => ({
        company: q.company(
            ({
                name,
                ceo,
                founded,
                ...fields // 👈 Autocompletion support
            }) => ({
                name,
                ceo,
                founded, //
            })
        ),
    }))
);
@schettn
Copy link
Author

schettn commented Sep 9, 2024

Ah.. while writing this issue I realised that ..fields is simply a placeholder and you want to communicate that the user can type any field via autocomplete.

This is maybe a bit confusing.

@liontariai
Copy link
Owner

Yep. Good point... that was the intention. At the same time, '...fields' is a 'Rest' operator and so at least the 'fields' object/var now contains the rest of the fields, i.e. all fields except 'name', 'ceo' and 'founded'.

Maybe I'll come up with a better illustration, thanks for the heads-up!

@schettn
Copy link
Author

schettn commented Sep 9, 2024

I thought in term of gqty.. But there is a major difference. You use callbacks for nested queries (I missed that). Qqty uses return objects which are then analysed via a Proxy.

const {company} = await resolve(({Query}) => {
  const company = Query.company(...args)

  return {
    name: company.name,
    ceo: company.ceo,
    founded: company.founded
  }
})

Do you support args?

@schettn
Copy link
Author

schettn commented Sep 9, 2024

Nvm. Just found it:

image

@schettn
Copy link
Author

schettn commented Sep 9, 2024

You should really link the Playground on you website.

@liontariai
Copy link
Owner

Yes, you can use args everywhere and they are hoisted into the variables of the graphql operation.
You can even use the same field (which needs args) multiple times via an alias, and the args/vars will be matched to the right instance. Even works if you use args in fragments :)

See #6

grafik

@liontariai
Copy link
Owner

You should really link the Playground on you website.

yea the playground website was my first try... the liontari.ai website is my try to look more 'professional' :D
I should kinda merge both somehow, you're right. Or at least link it, true.. I'll do that first I guess.

@liontariai
Copy link
Owner

Hey @schettn , i went all-in on the playground. Would really appreciate your feedback :)

check https://liontari.ai ... it works on mobile too, but it takes a while to load typescript and rollup things in the cache.
grafik

@schettn
Copy link
Author

schettn commented Sep 26, 2024

Looks nice!

But I dont get the snippet. what is code: ""?

Do yo have some documentation on how the syntax works? I personally find it quite hard to understand when just reading the code.

@liontariai
Copy link
Owner

Hmm, thanks for asking, the code: "" is sub-optimal right now.

When using the .$lazy helper to lazify an operation you can pass default arguments which are used later, when actually calling the function, and you can override the arguments later if you want.
See the 3 different calls with code: "GB", code: "FR" and code: "DE".

The problem is that code is a required argument in the Query.country( code: String! ) field. So the typescript type also requires this to be set, when constructing your query. I haven't yet found a solution to modify the type based on whether the .$lazy property is accessed or not, and I don't think it's possible.

My idea would be, to introduce a placeholder variable like _ which may be exported from the sdk so that you can do

import sdk, { _ } from "sdk";

const { getCountry } = await sdk(op => op.query(({ country }) => ({
    getCountry: country({ code: _ })(s => ({ // see the use of _ here
        name: s.name({lang: "en"}),
        capital: s.capital
    })).$lazy
})));

this would have the type any, so it satisfies the argument types, but will throw an error if not overwritten on execution.
What do you think?

I have some docs on for the .$lazy operator here: https://github.com/liontariai/samarium/blob/main/docs/lazy-execution.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants