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

implement TryIntoStruct for EdgeParameters #343

Closed
u9g opened this issue Jul 7, 2023 · 0 comments · Fixed by #350
Closed

implement TryIntoStruct for EdgeParameters #343

u9g opened this issue Jul 7, 2023 · 0 comments · Fixed by #350
Labels
A-adapter Area: plugging data sources into the interpreter C-feature-request Category: request for new future functionality L-rust Language: affects use cases in the Rust programming language

Comments

@u9g
Copy link
Contributor

u9g commented Jul 7, 2023

using the TryIntoStruct trait: https://github.com/obi1kenobi/trustfall/releases/tag/trustfall-v0.5.0 , we can unpack edge params into a struct like:

struct Params {
    ends_with_regex: Vec<String>
}
obi1kenobi added a commit that referenced this issue Jul 7, 2023
Edges defined in Trustfall schemas may take parameters, for example:

```graphql
type NewsWebsite {
    latest_stories(count: Int!): [Story!]!
}
```

This trait can be used to deserialize [`&EdgeParameters`](crate::ir::EdgeParameters)
into a struct specific to the parameters of that edge:
```rust
struct LatestStoriesParameters {
    count: usize
}
```

For example:
```rust
use trustfall_core::TryIntoStruct;

fn resolve_latest_stories(contexts: ContextIterator<Vertex>, parameters: &EdgeParameters) {
    let parameters: LatestStoriesParameters = parameters
        .try_into_struct()
        .expect("edge parameters did not match struct definition");
    let count = parameters.count;
    // then resolve the edge with the given count
}
```

Resolves #343.
@obi1kenobi obi1kenobi added A-adapter Area: plugging data sources into the interpreter C-feature-request Category: request for new future functionality L-rust Language: affects use cases in the Rust programming language labels Jul 7, 2023
obi1kenobi added a commit that referenced this issue Jul 7, 2023
Edges defined in Trustfall schemas may take parameters, for example:

```graphql
type NewsWebsite {
    latest_stories(count: Int!): [Story!]!
}
```

This trait can be used to deserialize [`&EdgeParameters`](crate::ir::EdgeParameters)
into a struct specific to the parameters of that edge:
```rust
struct LatestStoriesParameters {
    count: usize
}
```

For example:
```rust
use trustfall_core::TryIntoStruct;

fn resolve_latest_stories(contexts: ContextIterator<Vertex>, parameters: &EdgeParameters) {
    let parameters: LatestStoriesParameters = parameters
        .try_into_struct()
        .expect("edge parameters did not match struct definition");
    let count = parameters.count;
    // then resolve the edge with the given count
}
```

Resolves #343.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-adapter Area: plugging data sources into the interpreter C-feature-request Category: request for new future functionality L-rust Language: affects use cases in the Rust programming language
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants