Blockchain explorer for Ethereum based network and a tool for inspecting and analyzing EVM based blockchains.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
blockscout/apps/block_scout_web/lib/block_scout_web/schema.ex

125 lines
3.6 KiB

defmodule BlockScoutWeb.Schema do
@moduledoc false
use Absinthe.Schema
GraphQL support to get transactions by address Why: * We'd like to add support for GraphQL queries to get transactions by address hash. RPC API users could use this instead of the `txlist` RPC API action. Example usage: 1. ``` query($hash: AddressHash!, $first: Int!) { address(hash: $hash) { transactions(first: $first) { edges { node { hash blockNumber } cursor } } } ``` 2. with pagination support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query($hash: AddressHash!, $first: Int!, $after: String!) { address(hash: $hash) { transactions(first: $first, after: $after) { pageInfo { hasNextPage hasPreviousPage } edges { node { hash blockNumber } cursor } } } } ``` * Issue link: n/a This change addresses the need by: * Renaming `BlockScoutWeb.Schema.Query.AddressTest` to `...AddressesTest` for the name to match the query field it is testing. * Adding `:absinthe_relay` dependency to `BlockScoutWeb` app for Absinthe support for the [Relay framework](https://facebook.github.io/relay/graphql/connections.htm). * Editing `BlockScoutWeb.Schema` to use `Absinthe.Relay.Schema`, define a node interface, and define a node field. This was necessary to support relay connections. * Adding an `address` field to `BlockScoutWeb.Schema`, to get a single address by hash. * Adding a `TransactionConnection` field to the `address` object type in `BlockScoutWeb.Schema.Types` * Configuring the `transaction` object type as a node, in `BlockScoutWeb.Schema.Types`. * Adding a resolver function in `BlockScoutWeb.Resolvers.Address`, to get a single address. * Adding a resolver function in `BlockScoutWeb.Resolvers.Transaction`, to get transactions for an address. * Creating `Explorer.GraphQL` for GraphQL API specific Ecto queries. The only function in this module now is `address_to_transactions_query/1`. This function is necessary for `Absinthe.Relay.Connection.from_query/4` to load transactions in the new transaction resolver mentioned above. * Editing RPC API docs to include reference to the GraphQL "transactions" field.
6 years ago
use Absinthe.Relay.Schema, :modern
alias Absinthe.Middleware.Dataloader, as: AbsintheMiddlewareDataloader
alias Absinthe.Plugin, as: AbsinthePlugin
GraphQL support to get internal transactions Why: * For GraphQL API users to be able to get internal transactions. RPC API users could use this instead of the `txlistinternal` RPC API action. Example usage: 1. ``` query ($hash: FullHash!, $first: Int!) { transaction(hash: $hash) { internal_transactions(first: $first) { edges { node { index value block_number transaction_index from_address_hash to_address_hash } } } } } ``` 2. with pagiantion support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query ($hash: AddressHash!, $first: Int!, $after: ID!) { transaction(hash: $hash) { internal_transactions(first: $first, after: $after) { page_info { has_next_page has_previous_page } edges { node { index transaction_hash } cursor } } } } ``` * Issue link: n/a This change addresses the need by: * Editing the node interface and node field in `BlockScoutWeb.Schema` to support internal transactions. * Adding `internal_transaction` object type to `BlockScoutWeb.Schema.Types`. * Adding an `internal_transaction` connection field to the `transaction` object type in `BlockScoutWeb.Schema.Types`. * Adding `:call_type` and `:type` enums to `BlockScoutWEb.Schema.Scalars`. Needed by `internal_transaction` GraphQL object type. * Creating `BlockScoutWeb.Resolvers.InternalTransaction` with resolver functions for getting internal transactions. * Adding `get_internal_transactions/1` and `transaction_to_internal_transactions_query/1` to `Explorer.GraphQL`. These are used by the new resolver functions mentioned above.
6 years ago
alias BlockScoutWeb.Resolvers.{
Address,
Block,
InternalTransaction,
GraphQL API token_transfers query Why: * For GraphQL API users to be able to fetch token transfer events. RPC API users could use this instead of the `tokentx` RPC API action. Example usage: 1. ``` query ($token_contract_address_hash: AddressHash!, $first: Int!) { token_transfers(token_contract_address_hash: $token_contract_address_hash, first: $first) { edges { node { amount block_number log_index token_id from_address_hash to_address_hash token_contract_address_hash transaction_hash } } } } ``` 2. with pagination support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query ($token_contract_address_hash: AddressHash!, $first: Int!, $after: ID!) { token_transfers(token_contract_address_hash: $token_contract_address_hash, first: $first, after: $after) { page_info { has_next_page has_previous_page } edges { node { amount from_address_hash to_address_hash } cursor } } } ``` * Issue link: n/a This change addresses the need by: * Editing the node interface and node field in `BlockScoutWeb.Schema` to support token transfers. * Adding `token_transfers` connection field to root query object in `BlockScoutWeb.Schema` * Making `token_transfer` object types a node type and adding a few new fields to it, in `BlockScoutWeb.Schema.Types`. * Creating `BlockScoutWeb.Resolvers.TokenTransfer` with resolver functions for getting token transfers. * Adding `get_token_transfer/1` and `list_token_transfers_query/1` to `Explorer.GraphQL`. These two functions are used by the new resolver functions mentioned above. * Editing RPC API docs to reference the new GraphQL query field added in this commit.
6 years ago
TokenTransfer,
GraphQL support to get internal transactions Why: * For GraphQL API users to be able to get internal transactions. RPC API users could use this instead of the `txlistinternal` RPC API action. Example usage: 1. ``` query ($hash: FullHash!, $first: Int!) { transaction(hash: $hash) { internal_transactions(first: $first) { edges { node { index value block_number transaction_index from_address_hash to_address_hash } } } } } ``` 2. with pagiantion support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query ($hash: AddressHash!, $first: Int!, $after: ID!) { transaction(hash: $hash) { internal_transactions(first: $first, after: $after) { page_info { has_next_page has_previous_page } edges { node { index transaction_hash } cursor } } } } ``` * Issue link: n/a This change addresses the need by: * Editing the node interface and node field in `BlockScoutWeb.Schema` to support internal transactions. * Adding `internal_transaction` object type to `BlockScoutWeb.Schema.Types`. * Adding an `internal_transaction` connection field to the `transaction` object type in `BlockScoutWeb.Schema.Types`. * Adding `:call_type` and `:type` enums to `BlockScoutWEb.Schema.Scalars`. Needed by `internal_transaction` GraphQL object type. * Creating `BlockScoutWeb.Resolvers.InternalTransaction` with resolver functions for getting internal transactions. * Adding `get_internal_transactions/1` and `transaction_to_internal_transactions_query/1` to `Explorer.GraphQL`. These are used by the new resolver functions mentioned above.
6 years ago
Transaction
}
alias Explorer.Chain
GraphQL support to get internal transactions Why: * For GraphQL API users to be able to get internal transactions. RPC API users could use this instead of the `txlistinternal` RPC API action. Example usage: 1. ``` query ($hash: FullHash!, $first: Int!) { transaction(hash: $hash) { internal_transactions(first: $first) { edges { node { index value block_number transaction_index from_address_hash to_address_hash } } } } } ``` 2. with pagiantion support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query ($hash: AddressHash!, $first: Int!, $after: ID!) { transaction(hash: $hash) { internal_transactions(first: $first, after: $after) { page_info { has_next_page has_previous_page } edges { node { index transaction_hash } cursor } } } } ``` * Issue link: n/a This change addresses the need by: * Editing the node interface and node field in `BlockScoutWeb.Schema` to support internal transactions. * Adding `internal_transaction` object type to `BlockScoutWeb.Schema.Types`. * Adding an `internal_transaction` connection field to the `transaction` object type in `BlockScoutWeb.Schema.Types`. * Adding `:call_type` and `:type` enums to `BlockScoutWEb.Schema.Scalars`. Needed by `internal_transaction` GraphQL object type. * Creating `BlockScoutWeb.Resolvers.InternalTransaction` with resolver functions for getting internal transactions. * Adding `get_internal_transactions/1` and `transaction_to_internal_transactions_query/1` to `Explorer.GraphQL`. These are used by the new resolver functions mentioned above.
6 years ago
alias Explorer.Chain.InternalTransaction, as: ExplorerChainInternalTransaction
GraphQL API token_transfers query Why: * For GraphQL API users to be able to fetch token transfer events. RPC API users could use this instead of the `tokentx` RPC API action. Example usage: 1. ``` query ($token_contract_address_hash: AddressHash!, $first: Int!) { token_transfers(token_contract_address_hash: $token_contract_address_hash, first: $first) { edges { node { amount block_number log_index token_id from_address_hash to_address_hash token_contract_address_hash transaction_hash } } } } ``` 2. with pagination support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query ($token_contract_address_hash: AddressHash!, $first: Int!, $after: ID!) { token_transfers(token_contract_address_hash: $token_contract_address_hash, first: $first, after: $after) { page_info { has_next_page has_previous_page } edges { node { amount from_address_hash to_address_hash } cursor } } } ``` * Issue link: n/a This change addresses the need by: * Editing the node interface and node field in `BlockScoutWeb.Schema` to support token transfers. * Adding `token_transfers` connection field to root query object in `BlockScoutWeb.Schema` * Making `token_transfer` object types a node type and adding a few new fields to it, in `BlockScoutWeb.Schema.Types`. * Creating `BlockScoutWeb.Resolvers.TokenTransfer` with resolver functions for getting token transfers. * Adding `get_token_transfer/1` and `list_token_transfers_query/1` to `Explorer.GraphQL`. These two functions are used by the new resolver functions mentioned above. * Editing RPC API docs to reference the new GraphQL query field added in this commit.
6 years ago
alias Explorer.Chain.TokenTransfer, as: ExplorerChainTokenTransfer
GraphQL support to get transactions by address Why: * We'd like to add support for GraphQL queries to get transactions by address hash. RPC API users could use this instead of the `txlist` RPC API action. Example usage: 1. ``` query($hash: AddressHash!, $first: Int!) { address(hash: $hash) { transactions(first: $first) { edges { node { hash blockNumber } cursor } } } ``` 2. with pagination support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query($hash: AddressHash!, $first: Int!, $after: String!) { address(hash: $hash) { transactions(first: $first, after: $after) { pageInfo { hasNextPage hasPreviousPage } edges { node { hash blockNumber } cursor } } } } ``` * Issue link: n/a This change addresses the need by: * Renaming `BlockScoutWeb.Schema.Query.AddressTest` to `...AddressesTest` for the name to match the query field it is testing. * Adding `:absinthe_relay` dependency to `BlockScoutWeb` app for Absinthe support for the [Relay framework](https://facebook.github.io/relay/graphql/connections.htm). * Editing `BlockScoutWeb.Schema` to use `Absinthe.Relay.Schema`, define a node interface, and define a node field. This was necessary to support relay connections. * Adding an `address` field to `BlockScoutWeb.Schema`, to get a single address by hash. * Adding a `TransactionConnection` field to the `address` object type in `BlockScoutWeb.Schema.Types` * Configuring the `transaction` object type as a node, in `BlockScoutWeb.Schema.Types`. * Adding a resolver function in `BlockScoutWeb.Resolvers.Address`, to get a single address. * Adding a resolver function in `BlockScoutWeb.Resolvers.Transaction`, to get transactions for an address. * Creating `Explorer.GraphQL` for GraphQL API specific Ecto queries. The only function in this module now is `address_to_transactions_query/1`. This function is necessary for `Absinthe.Relay.Connection.from_query/4` to load transactions in the new transaction resolver mentioned above. * Editing RPC API docs to include reference to the GraphQL "transactions" field.
6 years ago
alias Explorer.Chain.Transaction, as: ExplorerChainTransaction
import_types(BlockScoutWeb.Schema.Types)
GraphQL support to get transactions by address Why: * We'd like to add support for GraphQL queries to get transactions by address hash. RPC API users could use this instead of the `txlist` RPC API action. Example usage: 1. ``` query($hash: AddressHash!, $first: Int!) { address(hash: $hash) { transactions(first: $first) { edges { node { hash blockNumber } cursor } } } ``` 2. with pagination support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query($hash: AddressHash!, $first: Int!, $after: String!) { address(hash: $hash) { transactions(first: $first, after: $after) { pageInfo { hasNextPage hasPreviousPage } edges { node { hash blockNumber } cursor } } } } ``` * Issue link: n/a This change addresses the need by: * Renaming `BlockScoutWeb.Schema.Query.AddressTest` to `...AddressesTest` for the name to match the query field it is testing. * Adding `:absinthe_relay` dependency to `BlockScoutWeb` app for Absinthe support for the [Relay framework](https://facebook.github.io/relay/graphql/connections.htm). * Editing `BlockScoutWeb.Schema` to use `Absinthe.Relay.Schema`, define a node interface, and define a node field. This was necessary to support relay connections. * Adding an `address` field to `BlockScoutWeb.Schema`, to get a single address by hash. * Adding a `TransactionConnection` field to the `address` object type in `BlockScoutWeb.Schema.Types` * Configuring the `transaction` object type as a node, in `BlockScoutWeb.Schema.Types`. * Adding a resolver function in `BlockScoutWeb.Resolvers.Address`, to get a single address. * Adding a resolver function in `BlockScoutWeb.Resolvers.Transaction`, to get transactions for an address. * Creating `Explorer.GraphQL` for GraphQL API specific Ecto queries. The only function in this module now is `address_to_transactions_query/1`. This function is necessary for `Absinthe.Relay.Connection.from_query/4` to load transactions in the new transaction resolver mentioned above. * Editing RPC API docs to include reference to the GraphQL "transactions" field.
6 years ago
node interface do
resolve_type(fn
GraphQL support to get internal transactions Why: * For GraphQL API users to be able to get internal transactions. RPC API users could use this instead of the `txlistinternal` RPC API action. Example usage: 1. ``` query ($hash: FullHash!, $first: Int!) { transaction(hash: $hash) { internal_transactions(first: $first) { edges { node { index value block_number transaction_index from_address_hash to_address_hash } } } } } ``` 2. with pagiantion support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query ($hash: AddressHash!, $first: Int!, $after: ID!) { transaction(hash: $hash) { internal_transactions(first: $first, after: $after) { page_info { has_next_page has_previous_page } edges { node { index transaction_hash } cursor } } } } ``` * Issue link: n/a This change addresses the need by: * Editing the node interface and node field in `BlockScoutWeb.Schema` to support internal transactions. * Adding `internal_transaction` object type to `BlockScoutWeb.Schema.Types`. * Adding an `internal_transaction` connection field to the `transaction` object type in `BlockScoutWeb.Schema.Types`. * Adding `:call_type` and `:type` enums to `BlockScoutWEb.Schema.Scalars`. Needed by `internal_transaction` GraphQL object type. * Creating `BlockScoutWeb.Resolvers.InternalTransaction` with resolver functions for getting internal transactions. * Adding `get_internal_transactions/1` and `transaction_to_internal_transactions_query/1` to `Explorer.GraphQL`. These are used by the new resolver functions mentioned above.
6 years ago
%ExplorerChainInternalTransaction{}, _ ->
:internal_transaction
GraphQL API token_transfers query Why: * For GraphQL API users to be able to fetch token transfer events. RPC API users could use this instead of the `tokentx` RPC API action. Example usage: 1. ``` query ($token_contract_address_hash: AddressHash!, $first: Int!) { token_transfers(token_contract_address_hash: $token_contract_address_hash, first: $first) { edges { node { amount block_number log_index token_id from_address_hash to_address_hash token_contract_address_hash transaction_hash } } } } ``` 2. with pagination support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query ($token_contract_address_hash: AddressHash!, $first: Int!, $after: ID!) { token_transfers(token_contract_address_hash: $token_contract_address_hash, first: $first, after: $after) { page_info { has_next_page has_previous_page } edges { node { amount from_address_hash to_address_hash } cursor } } } ``` * Issue link: n/a This change addresses the need by: * Editing the node interface and node field in `BlockScoutWeb.Schema` to support token transfers. * Adding `token_transfers` connection field to root query object in `BlockScoutWeb.Schema` * Making `token_transfer` object types a node type and adding a few new fields to it, in `BlockScoutWeb.Schema.Types`. * Creating `BlockScoutWeb.Resolvers.TokenTransfer` with resolver functions for getting token transfers. * Adding `get_token_transfer/1` and `list_token_transfers_query/1` to `Explorer.GraphQL`. These two functions are used by the new resolver functions mentioned above. * Editing RPC API docs to reference the new GraphQL query field added in this commit.
6 years ago
%ExplorerChainTokenTransfer{}, _ ->
:token_transfer
%ExplorerChainTransaction{}, _ ->
:transaction
GraphQL support to get transactions by address Why: * We'd like to add support for GraphQL queries to get transactions by address hash. RPC API users could use this instead of the `txlist` RPC API action. Example usage: 1. ``` query($hash: AddressHash!, $first: Int!) { address(hash: $hash) { transactions(first: $first) { edges { node { hash blockNumber } cursor } } } ``` 2. with pagination support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query($hash: AddressHash!, $first: Int!, $after: String!) { address(hash: $hash) { transactions(first: $first, after: $after) { pageInfo { hasNextPage hasPreviousPage } edges { node { hash blockNumber } cursor } } } } ``` * Issue link: n/a This change addresses the need by: * Renaming `BlockScoutWeb.Schema.Query.AddressTest` to `...AddressesTest` for the name to match the query field it is testing. * Adding `:absinthe_relay` dependency to `BlockScoutWeb` app for Absinthe support for the [Relay framework](https://facebook.github.io/relay/graphql/connections.htm). * Editing `BlockScoutWeb.Schema` to use `Absinthe.Relay.Schema`, define a node interface, and define a node field. This was necessary to support relay connections. * Adding an `address` field to `BlockScoutWeb.Schema`, to get a single address by hash. * Adding a `TransactionConnection` field to the `address` object type in `BlockScoutWeb.Schema.Types` * Configuring the `transaction` object type as a node, in `BlockScoutWeb.Schema.Types`. * Adding a resolver function in `BlockScoutWeb.Resolvers.Address`, to get a single address. * Adding a resolver function in `BlockScoutWeb.Resolvers.Transaction`, to get transactions for an address. * Creating `Explorer.GraphQL` for GraphQL API specific Ecto queries. The only function in this module now is `address_to_transactions_query/1`. This function is necessary for `Absinthe.Relay.Connection.from_query/4` to load transactions in the new transaction resolver mentioned above. * Editing RPC API docs to include reference to the GraphQL "transactions" field.
6 years ago
_, _ ->
nil
end)
end
query do
GraphQL support to get transactions by address Why: * We'd like to add support for GraphQL queries to get transactions by address hash. RPC API users could use this instead of the `txlist` RPC API action. Example usage: 1. ``` query($hash: AddressHash!, $first: Int!) { address(hash: $hash) { transactions(first: $first) { edges { node { hash blockNumber } cursor } } } ``` 2. with pagination support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query($hash: AddressHash!, $first: Int!, $after: String!) { address(hash: $hash) { transactions(first: $first, after: $after) { pageInfo { hasNextPage hasPreviousPage } edges { node { hash blockNumber } cursor } } } } ``` * Issue link: n/a This change addresses the need by: * Renaming `BlockScoutWeb.Schema.Query.AddressTest` to `...AddressesTest` for the name to match the query field it is testing. * Adding `:absinthe_relay` dependency to `BlockScoutWeb` app for Absinthe support for the [Relay framework](https://facebook.github.io/relay/graphql/connections.htm). * Editing `BlockScoutWeb.Schema` to use `Absinthe.Relay.Schema`, define a node interface, and define a node field. This was necessary to support relay connections. * Adding an `address` field to `BlockScoutWeb.Schema`, to get a single address by hash. * Adding a `TransactionConnection` field to the `address` object type in `BlockScoutWeb.Schema.Types` * Configuring the `transaction` object type as a node, in `BlockScoutWeb.Schema.Types`. * Adding a resolver function in `BlockScoutWeb.Resolvers.Address`, to get a single address. * Adding a resolver function in `BlockScoutWeb.Resolvers.Transaction`, to get transactions for an address. * Creating `Explorer.GraphQL` for GraphQL API specific Ecto queries. The only function in this module now is `address_to_transactions_query/1`. This function is necessary for `Absinthe.Relay.Connection.from_query/4` to load transactions in the new transaction resolver mentioned above. * Editing RPC API docs to include reference to the GraphQL "transactions" field.
6 years ago
node field do
resolve(fn
GraphQL support to get internal transactions Why: * For GraphQL API users to be able to get internal transactions. RPC API users could use this instead of the `txlistinternal` RPC API action. Example usage: 1. ``` query ($hash: FullHash!, $first: Int!) { transaction(hash: $hash) { internal_transactions(first: $first) { edges { node { index value block_number transaction_index from_address_hash to_address_hash } } } } } ``` 2. with pagiantion support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query ($hash: AddressHash!, $first: Int!, $after: ID!) { transaction(hash: $hash) { internal_transactions(first: $first, after: $after) { page_info { has_next_page has_previous_page } edges { node { index transaction_hash } cursor } } } } ``` * Issue link: n/a This change addresses the need by: * Editing the node interface and node field in `BlockScoutWeb.Schema` to support internal transactions. * Adding `internal_transaction` object type to `BlockScoutWeb.Schema.Types`. * Adding an `internal_transaction` connection field to the `transaction` object type in `BlockScoutWeb.Schema.Types`. * Adding `:call_type` and `:type` enums to `BlockScoutWEb.Schema.Scalars`. Needed by `internal_transaction` GraphQL object type. * Creating `BlockScoutWeb.Resolvers.InternalTransaction` with resolver functions for getting internal transactions. * Adding `get_internal_transactions/1` and `transaction_to_internal_transactions_query/1` to `Explorer.GraphQL`. These are used by the new resolver functions mentioned above.
6 years ago
%{type: :internal_transaction, id: id}, _ ->
%{"transaction_hash" => transaction_hash_string, "index" => index} = Jason.decode!(id)
{:ok, transaction_hash} = Chain.string_to_transaction_hash(transaction_hash_string)
InternalTransaction.get_by(%{transaction_hash: transaction_hash, index: index})
GraphQL API token_transfers query Why: * For GraphQL API users to be able to fetch token transfer events. RPC API users could use this instead of the `tokentx` RPC API action. Example usage: 1. ``` query ($token_contract_address_hash: AddressHash!, $first: Int!) { token_transfers(token_contract_address_hash: $token_contract_address_hash, first: $first) { edges { node { amount block_number log_index token_id from_address_hash to_address_hash token_contract_address_hash transaction_hash } } } } ``` 2. with pagination support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query ($token_contract_address_hash: AddressHash!, $first: Int!, $after: ID!) { token_transfers(token_contract_address_hash: $token_contract_address_hash, first: $first, after: $after) { page_info { has_next_page has_previous_page } edges { node { amount from_address_hash to_address_hash } cursor } } } ``` * Issue link: n/a This change addresses the need by: * Editing the node interface and node field in `BlockScoutWeb.Schema` to support token transfers. * Adding `token_transfers` connection field to root query object in `BlockScoutWeb.Schema` * Making `token_transfer` object types a node type and adding a few new fields to it, in `BlockScoutWeb.Schema.Types`. * Creating `BlockScoutWeb.Resolvers.TokenTransfer` with resolver functions for getting token transfers. * Adding `get_token_transfer/1` and `list_token_transfers_query/1` to `Explorer.GraphQL`. These two functions are used by the new resolver functions mentioned above. * Editing RPC API docs to reference the new GraphQL query field added in this commit.
6 years ago
%{type: :token_transfer, id: id}, _ ->
%{"transaction_hash" => transaction_hash_string, "log_index" => log_index} = Jason.decode!(id)
{:ok, transaction_hash} = Chain.string_to_transaction_hash(transaction_hash_string)
TokenTransfer.get_by(%{transaction_hash: transaction_hash, log_index: log_index})
%{type: :transaction, id: transaction_hash_string}, _ ->
{:ok, hash} = Chain.string_to_transaction_hash(transaction_hash_string)
Transaction.get_by(%{}, %{hash: hash}, %{})
GraphQL support to get transactions by address Why: * We'd like to add support for GraphQL queries to get transactions by address hash. RPC API users could use this instead of the `txlist` RPC API action. Example usage: 1. ``` query($hash: AddressHash!, $first: Int!) { address(hash: $hash) { transactions(first: $first) { edges { node { hash blockNumber } cursor } } } ``` 2. with pagination support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query($hash: AddressHash!, $first: Int!, $after: String!) { address(hash: $hash) { transactions(first: $first, after: $after) { pageInfo { hasNextPage hasPreviousPage } edges { node { hash blockNumber } cursor } } } } ``` * Issue link: n/a This change addresses the need by: * Renaming `BlockScoutWeb.Schema.Query.AddressTest` to `...AddressesTest` for the name to match the query field it is testing. * Adding `:absinthe_relay` dependency to `BlockScoutWeb` app for Absinthe support for the [Relay framework](https://facebook.github.io/relay/graphql/connections.htm). * Editing `BlockScoutWeb.Schema` to use `Absinthe.Relay.Schema`, define a node interface, and define a node field. This was necessary to support relay connections. * Adding an `address` field to `BlockScoutWeb.Schema`, to get a single address by hash. * Adding a `TransactionConnection` field to the `address` object type in `BlockScoutWeb.Schema.Types` * Configuring the `transaction` object type as a node, in `BlockScoutWeb.Schema.Types`. * Adding a resolver function in `BlockScoutWeb.Resolvers.Address`, to get a single address. * Adding a resolver function in `BlockScoutWeb.Resolvers.Transaction`, to get transactions for an address. * Creating `Explorer.GraphQL` for GraphQL API specific Ecto queries. The only function in this module now is `address_to_transactions_query/1`. This function is necessary for `Absinthe.Relay.Connection.from_query/4` to load transactions in the new transaction resolver mentioned above. * Editing RPC API docs to include reference to the GraphQL "transactions" field.
6 years ago
_, _ ->
{:error, "Unknown node"}
end)
end
@desc "Gets an address by hash."
field :address, :address do
arg(:hash, non_null(:address_hash))
resolve(&Address.get_by/3)
end
@desc "Gets addresses by address hash."
field :addresses, list_of(:address) do
arg(:hashes, non_null(list_of(non_null(:address_hash))))
resolve(&Address.get_by/3)
complexity(fn %{hashes: hashes}, child_complexity -> length(hashes) * child_complexity end)
end
@desc "Gets a block by number."
field :block, :block do
arg(:number, non_null(:integer))
resolve(&Block.get_by/3)
end
GraphQL API token_transfers query Why: * For GraphQL API users to be able to fetch token transfer events. RPC API users could use this instead of the `tokentx` RPC API action. Example usage: 1. ``` query ($token_contract_address_hash: AddressHash!, $first: Int!) { token_transfers(token_contract_address_hash: $token_contract_address_hash, first: $first) { edges { node { amount block_number log_index token_id from_address_hash to_address_hash token_contract_address_hash transaction_hash } } } } ``` 2. with pagination support via [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) ``` query ($token_contract_address_hash: AddressHash!, $first: Int!, $after: ID!) { token_transfers(token_contract_address_hash: $token_contract_address_hash, first: $first, after: $after) { page_info { has_next_page has_previous_page } edges { node { amount from_address_hash to_address_hash } cursor } } } ``` * Issue link: n/a This change addresses the need by: * Editing the node interface and node field in `BlockScoutWeb.Schema` to support token transfers. * Adding `token_transfers` connection field to root query object in `BlockScoutWeb.Schema` * Making `token_transfer` object types a node type and adding a few new fields to it, in `BlockScoutWeb.Schema.Types`. * Creating `BlockScoutWeb.Resolvers.TokenTransfer` with resolver functions for getting token transfers. * Adding `get_token_transfer/1` and `list_token_transfers_query/1` to `Explorer.GraphQL`. These two functions are used by the new resolver functions mentioned above. * Editing RPC API docs to reference the new GraphQL query field added in this commit.
6 years ago
@desc "Gets token transfers by token contract address hash."
connection field(:token_transfers, node_type: :token_transfer) do
arg(:token_contract_address_hash, non_null(:address_hash))
arg(:count, :integer)
resolve(&TokenTransfer.get_by/3)
complexity(fn
%{first: first}, child_complexity ->
first * child_complexity
%{last: last}, child_complexity ->
last * child_complexity
end)
end
@desc "Gets a transaction by hash."
field :transaction, :transaction do
arg(:hash, non_null(:full_hash))
resolve(&Transaction.get_by/3)
end
end
subscription do
field :token_transfers, list_of(:token_transfer) do
arg(:token_contract_address_hash, non_null(:address_hash))
config(fn args, _info ->
{:ok, topic: to_string(args.token_contract_address_hash)}
end)
end
end
def context(context) do
loader = Dataloader.add_source(Dataloader.new(), :db, Chain.data())
Map.put(context, :loader, loader)
end
def plugins do
[AbsintheMiddlewareDataloader] ++ AbsinthePlugin.defaults()
end
end