def build_attrs(): edge = edge_factory(sqla_model) return { "edges": Field(NonNull(List(NonNull(edge))), resolve=default_resolver), "pageInfo": Field(NonNull(PageInfo), resolve=default_resolver), "totalCount": Field(NonNull(Int), resolve=default_resolver), }
def update_payload_factory(sqla_model: TableProtocol) -> InputObjectType: """UpdateAccountPayload""" from nebulo.gql.convert.table import table_factory relevant_type_name = Config.table_type_name_mapper(sqla_model) relevant_attr_name = Config.table_name_mapper(sqla_model) result_name = f"Update{relevant_type_name}Payload" attrs = { "clientMutationId": Field(String, resolve=default_resolver), "nodeId": ID, relevant_attr_name: Field( table_factory(sqla_model), resolve=default_resolver, description= f"The {relevant_type_name} that was created by this mutation.", ), } return UpdatePayloadType( result_name, attrs, description=f"The output of our update {relevant_type_name} mutation", sqla_model=sqla_model)
def jwt_function_payload_factory(sql_function: SQLFunction, jwt_secret: str) -> FunctionPayloadType: """CreateAccountPayload""" function_name = Config.function_type_name_mapper(sql_function) result_name = f"{function_name}Payload" function_return_type = ScalarType( "JWT", serialize=lambda result: jwt.encode({k: v for k, v in result.items()}, jwt_secret, algorithm="HS256").decode("utf-8"), ) attrs = { "clientMutationId": Field(String, resolve=default_resolver), "result": Field( function_return_type, description=f"The {result_name} that was created by this mutation.", resolve=default_resolver, ), } payload = FunctionPayloadType( result_name, attrs, description=f"The output of our create {function_name} mutation") payload.sql_function = sql_function return payload
def build_attrs(): attrs = {} # Override id to relay standard attrs["nodeId"] = Field(NonNull(ID), resolve=default_resolver) for column in get_columns(sqla_model): if not Config.exclude_read(column): key = Config.column_name_mapper(column) attrs[key] = convert_column(column) for relationship in get_relationships(sqla_model): direction = relationship.direction to_sqla_model = relationship.mapper.class_ relationship_is_nullable = is_nullable(relationship) # Name of the attribute on the model attr_key = Config.relationship_name_mapper(relationship) # If this model has 1 counterpart, do not use a list if direction == interfaces.MANYTOONE: _type = table_factory(to_sqla_model) _type = NonNull(_type) if not relationship_is_nullable else _type attrs[attr_key] = Field(_type, resolve=default_resolver) # Otherwise, set it up as a connection elif direction in (interfaces.ONETOMANY, interfaces.MANYTOMANY): connection_field = connection_field_factory( to_sqla_model, resolver=default_resolver, not_null=relationship_is_nullable ) attrs[attr_key] = connection_field return attrs
def function_payload_factory(sql_function: SQLFunction) -> FunctionPayloadType: """CreateAccountPayload""" function_name = Config.function_type_name_mapper(sql_function) result_name = f"{function_name}Payload" # TODO(OR): handle functions with no return function_return_type = convert_type(sql_function.return_sqla_type) function_return_type.sql_function = sql_function attrs = { "clientMutationId": Field(String, resolve=default_resolver), "result": Field( function_return_type, description=f"The {result_name} that was created by this mutation.", resolve=default_resolver, ), } payload = FunctionPayloadType( result_name, attrs, description=f"The output of our create {function_name} mutation") payload.sql_function = sql_function return payload
def mutable_function_entrypoint_factory( sql_function: SQLFunction, resolver: typing.Callable, jwt_secret: typing.Optional[str] = None) -> typing.Dict[str, Field]: """authenticate""" # TODO(OR): need seperate mapper function_name = Config.function_name_mapper(sql_function) if sql_function.is_immutable: raise Exception( f"SQLFunction {sql_function.name} is immutable, use immutable_function_entrypoint" ) args = {"input": NonNull(function_input_type_factory(sql_function))} if jwt_secret is not None: payload = jwt_function_payload_factory(sql_function, jwt_secret) else: payload = function_payload_factory(sql_function) return { function_name: Field(payload, args=args, resolve=resolver, description=f"Call the function {function_name}.") }
def immutable_function_entrypoint_factory( sql_function: SQLFunction, resolver: typing.Callable) -> typing.Dict[str, Field]: """authenticate""" # TODO(OR): need seperate mapper function_name = Config.function_name_mapper(sql_function) if not sql_function.is_immutable: raise Exception( f"SQLFunction {sql_function.name} is not immutable, use mutable_function_entrypoint" ) gql_args = {(arg_name if arg_name else f"param{ix}"): Argument(NonNull(convert_type(arg_sqla_type))) for ix, (arg_name, arg_sqla_type) in enumerate( zip(sql_function.arg_names, sql_function.arg_sqla_types))} return_type = convert_type(sql_function.return_sqla_type) return_type.sql_function = sql_function return_field = Field(return_type, args=gql_args, resolve=resolver, description="") return {function_name: return_field}
def table_field_factory(sqla_model: TableProtocol, resolver) -> Field: relevant_type_name = Config.table_type_name_mapper(sqla_model) node = table_factory(sqla_model) return Field( node, args={"nodeId": Argument(NonNull(ID))}, resolve=resolver, description=f"Reads a single {relevant_type_name} using its globally unique ID", )
def create_entrypoint_factory(sqla_model: TableProtocol, resolver) -> Field: """createAccount""" relevant_type_name = Config.table_type_name_mapper(sqla_model) name = f"create{relevant_type_name}" args = {"input": NonNull(create_input_type_factory(sqla_model))} payload = create_payload_factory(sqla_model) return { name: Field(payload, args=args, resolve=resolver, description=f"Creates a single {relevant_type_name}.") }
def update_entrypoint_factory(sqla_model: TableProtocol, resolver) -> t.Dict[str, Field]: """updateAccount""" relevant_type_name = Config.table_type_name_mapper(sqla_model) name = f"update{relevant_type_name}" args = {"input": NonNull(update_input_type_factory(sqla_model))} payload = update_payload_factory(sqla_model) return { name: Field( payload, args=args, resolve=resolver, description=f"Updates a single {relevant_type_name} using its globally unique id and a patch.", ) }
def delete_payload_factory(sqla_model: TableProtocol) -> InputObjectType: """DeleteAccountPayload""" relevant_type_name = Config.table_type_name_mapper(sqla_model) result_name = f"Delete{relevant_type_name}Payload" attrs = { "clientMutationId": Field(String, resolve=default_resolver), "nodeId": ID } return DeletePayloadType( result_name, attrs, description=f"The output of our delete {relevant_type_name} mutation", sqla_model=sqla_model)
def connection_field_factory(sqla_model: TableProtocol, resolver, not_null=False) -> Field: relevant_type_name = Config.table_type_name_mapper(sqla_model) connection = connection_factory(sqla_model) condition = condition_factory(sqla_model) args = { "first": Argument(Int, description="", out_name=None), "last": Argument(Int), "before": Argument(Cursor), "after": Argument(Cursor), "condition": Argument(condition), } return Field( NonNull(connection) if not_null else connection, args=args, resolve=resolver, description=f"Reads and enables pagination through a set of {relevant_type_name}", )
def build_attrs(): return {"cursor": Field(Cursor), "node": Field(table_factory(sqla_model))}
vals.extend([literal_string(col_name), query_elem.c[col_name]]) return func.jsonb_build_object( literal_string("table_name"), literal_string(table_name), literal_string("values"), func.jsonb_build_object(*vals), ) ID = ScalarType( "ID", description="Unique ID for node", serialize=serialize, parse_value=NodeIdStructure.deserialize, parse_literal=lambda x: NodeIdStructure.deserialize(x.value), ) NodeInterface = InterfaceType( "NodeInterface", description="An object with a nodeId", fields={ "nodeId": Field(NonNull(ID), description="The global id of the object.", resolve=None) }, # Maybe not necessary resolve_type=lambda *args, **kwargs: None, )
def convert_column(column: Column) -> Field: """Converts a sqlalchemy column into a graphql field or input field""" gql_type = convert_type(column.type) notnull = not column.nullable return_type = NonNull(gql_type) if notnull else gql_type return Field(return_type, resolve=default_resolver)
from nebulo.gql.alias import Boolean, Field, NonNull, ObjectType from nebulo.gql.relay.cursor import Cursor PageInfo = ObjectType( name="PageInfo", fields={ "hasNextPage": Field(NonNull(Boolean)), "hasPreviousPage": Field(NonNull(Boolean)), "startCursor": Field(Cursor), "endCursor": Field(Cursor), }, description="", )