Module: GraphqlGrpc::Schema

Included in:
Proxy
Defined in:
lib/graphql_grpc/schema.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#gql_mutationsObject



40
41
42
# File 'lib/graphql_grpc/schema.rb', line 40

def gql_mutations
  @function_map.reject { |name_sym, rpc_desc| query?(name_sym, rpc_desc) }
end

#gql_queriesObject



44
45
46
# File 'lib/graphql_grpc/schema.rb', line 44

def gql_queries
  @function_map.select { |name_sym, rpc_desc| query?(name_sym, rpc_desc) }
end

#query?(name_sym, rpc_desc) ⇒ Boolean

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

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/graphql_grpc/schema.rb', line 30

def query?(name_sym, rpc_desc)
  name_sym.to_s.start_with?('get') ||
    name_sym.to_s.start_with?('find') ||
    rpc_desc.rpc_desc.input == Google::Protobuf::Empty
end

#streaming_response?(rpc_desc) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/graphql_grpc/schema.rb', line 36

def streaming_response?(rpc_desc)
  rpc_desc&.rpc_desc&.output.class == GRPC::RpcDesc::Stream
end

#to_function_types(ggg_function_hash) ⇒ Object



62
63
64
# File 'lib/graphql_grpc/schema.rb', line 62

def to_function_types(ggg_function_hash)
  ggg_function_hash.values.sort_by(&:name).map(&:to_query_type).join("\n  ")
end

#to_gql_schemaObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/graphql_grpc/schema.rb', line 88

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_mutationsObject



82
83
84
85
86
# File 'lib/graphql_grpc/schema.rb', line 82

def to_schema_mutations
  return '' if gql_mutations.empty?

  "type Mutation { #{to_function_types(gql_mutations)} }"
end

#to_schema_queryObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/graphql_grpc/schema.rb', line 66

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_typesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/graphql_grpc/schema.rb', line 48

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