module GraphqlGrpc::Schema

Constants

QUERY_PREFIX_ENV_VAR

Public Instance Methods

gql_mutations() click to toggle source
# File lib/graphql_grpc/schema.rb, line 41
def gql_mutations
  @function_map.reject { |name_sym, rpc_desc| query?(name_sym, rpc_desc) }
end
gql_queries() click to toggle source
# File lib/graphql_grpc/schema.rb, line 45
def gql_queries
  @function_map.select { |name_sym, rpc_desc| query?(name_sym, rpc_desc) }
end
query?(name_sym, rpc_desc) click to toggle source

TODO: Find better way to detect queries Currently look for methods named 'get', 'find' or with no args

# File lib/graphql_grpc/schema.rb, line 31
def query?(name_sym, rpc_desc)
  name_str = name_sym.to_s
  prefixes.each { |prefix| return true if name_str.start_with?(prefix) }
  rpc_desc.rpc_desc.input == Google::Protobuf::Empty
end
streaming_response?(rpc_desc) click to toggle source
# File lib/graphql_grpc/schema.rb, line 37
def streaming_response?(rpc_desc)
  rpc_desc&.rpc_desc&.output.class == GRPC::RpcDesc::Stream
end
to_function_types(ggg_function_hash) click to toggle source
# File lib/graphql_grpc/schema.rb, line 63
def to_function_types(ggg_function_hash)
  ggg_function_hash.values.sort_by(&:name).map(&:to_query_type).join("\n  ")
end
to_gql_schema() click to toggle source
# File lib/graphql_grpc/schema.rb, line 89
    def to_gql_schema
      <<GRAPHQL_SCHEMA
  #{to_schema_types}
  #{to_schema_query}
  #{to_schema_mutations}
  schema {
  query: Query
  #{gql_mutations.empty? ? '' : 'mutation: Mutation'}
  }
GRAPHQL_SCHEMA
    end
to_schema_mutations() click to toggle source
# File lib/graphql_grpc/schema.rb, line 83
def to_schema_mutations
  return '' if gql_mutations.empty?

  "type Mutation { #{to_function_types(gql_mutations)} }"
end
to_schema_query() click to toggle source
# File lib/graphql_grpc/schema.rb, line 67
    def to_schema_query
      if gql_queries.empty?
        'type Query {'\
             '  # """This gRPC stub does not contain any methods that are mapped to '\
             'GraphQL queries; this placeholder query field keeps the Query type from '\
             'being empty which can break tools (GraphiQL) which expect Query to contain '\
             'at least one field."""'\
             '
'\
             '  grpcPlacholder: Url'\
             '}'
      else
        "type Query { #{to_function_types(gql_queries)} }"
      end
    end
to_schema_types() click to toggle source
# File lib/graphql_grpc/schema.rb, line 49
def to_schema_types
  function_output_types = @function_map.values.map do |function|
    function.rpc_desc.output.is_a?(
      GRPC::RpcDesc::Stream
    ) ? function.rpc_desc.output.type : function.rpc_desc.output
  end.flatten.uniq
  output_types = TypeLibrary.new(function_output_types)
  function_input_types = @function_map.values.map do |function|
    function.rpc_desc.input
  end.flatten.uniq
  input_types = InputTypeLibrary.new(function_input_types)
  input_types.to_schema_types + "\nscalar Url\n" + output_types.to_schema_types
end

Private Instance Methods

prefixes() click to toggle source
# File lib/graphql_grpc/schema.rb, line 103
def prefixes
  @prefixes ||= (ENV[QUERY_PREFIX_ENV_VAR]&.strip&.split(',') || ['get', 'find'])
end