Giter Club home page Giter Club logo

Comments (2)

erikwrede avatar erikwrede commented on June 2, 2024 2

You'll need to analyze the AST for that. You'll get the AST after parsing using parse(document). Variables are always passed to arguments or children of arguments, so it's best to have a look at all ArgumentNodes using BFS/DFS:

class ArgumentNode(Node):
__slots__ = "name", "value"
name: NameNode
value: ValueNode

The possible inputs to ArgumentNodes are the subclasses of ValueNode. If you just want to look for Variables, not constants, do another DFS search inside each ArgumentNode, expanding all the ListValueNodes and ObjectValueNodes.

You can also write a custom visitor to save the effort of manually performing the search and call it using the visit function: https://github.com/graphql-python/graphql-core/blob/main/src/graphql/language/visitor.py

Hope that helps, LMK if you have any questions. Am curious about the use case so please feel free to share more (maybe not in this issue though, so we don't add too much noise)🙂

from graphql-core.

nrbnlulu avatar nrbnlulu commented on June 2, 2024

Since I parse every operation down to field level I added an argument evaluator that will map
variable uses to field definition, looks like this:

def _evaluate_variable_uses(
    type_info: OperationTypeInfo,
    field: QtGqlFieldDefinition,
    arguments: tuple[gql_lang.ArgumentNode, ...],
) -> list[QtGqlVariableUse]:
    ret: list[QtGqlVariableUse] = []
    for arg in arguments:
        index = field.index_for_argument(arg.name.value)
        var_name = arg.value.name.value  # type: ignore[attr-defined]
        for variable in type_info.variables:
            if var_name == variable.name:
                ret.append(
                    QtGqlVariableUse(argument=(index, field.arguments[index]), variable=variable),
                )
    assert len(ret) == len(arguments), "could not find all variable uses"
    return ret


def _evaluate_variable_node_type(
    type_info: SchemaTypeInfo,
    node: graphql.TypeNode,
) -> QtGqlTypeABC:
    if nonnull := is_nonnull_node(node):
        return evaluate_graphql_type(
            type_info,
            graphql.type.GraphQLNonNull(
                type_info.schema_definition.get_type(nonnull.type.name.value),  # type: ignore
            ),
        )

    if named_type := is_named_type_node(node):
        gql_concrete = type_info.schema_definition.get_type(named_type.name.value)
        assert gql_concrete
        return evaluate_graphql_type(type_info, gql_concrete)
    raise NotImplementedError(node, "Type is not supported as a variable ATM")

Then fields that has arguments are stored in a map that is that looks like this

std::map<std::tuple<(argument types)>, (value)>

And when an operation is deserialized the value is set on that field based on the operation argument that
I already determined what they should be.

from graphql-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.