Class: GraphQL::IDLParser

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql-idl-parser.rb,
ext/graphql-idl-parser/graphql-idl-parser.c

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(filename: nil, schema: nil) ⇒ IDLParser

Returns a new instance of IDLParser.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graphql-idl-parser.rb', line 6

def initialize(filename: nil, schema: nil)
  if !filename.nil? && !schema.nil?
    raise ArgumentError, 'Pass in `filename` or `schema`, but not both!'
  end

  if filename.nil? && schema.nil?
    raise ArgumentError, 'Pass in either `filename` or `schema`'
  end

  if filename
    unless filename.is_a?(String)
      raise TypeError, "Expected `String`, got `#{filename.class}`"
    end

    unless File.exist?(filename)
      raise ArgumentError, "#{filename} does not exist!"
    end

    @schema = File.read(filename)
  else
    unless schema.is_a?(String)
      raise TypeError, "Expected `String`, got `#{schema.class}`"
    end

    @schema = schema
  end
end

Instance Method Details

#processObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'ext/graphql-idl-parser/graphql-idl-parser.c', line 155

static VALUE GRAPHQLIDLPARSERPROCESS_process(VALUE self)
{
  VALUE rb_schema = rb_iv_get(self, "@schema");
  VALUE rb_result = rb_obj_alloc(rb_cResult);
  VALUE rb_types;
  VALUE rb_hash;
  GraphQLTypes* types = NULL;
  size_t types_len = 0;
  uint8_t err;
  const char *schema = StringValueCStr(rb_schema);

  err = gqlidl_parse_schema(schema, &types, &types_len);

  if (err > 0) {
    printf("Error: Return code %d", err);
    exit(err);
  }

  rb_types = rb_ary_new();

  /* attr_reader :types */
  rb_define_attr(rb_cResult, "types", 1, 0);

  for (size_t i = 0; i < types_len; i++) {
    rb_hash = rb_hash_new();

    if (strcmp(types[i].typename, "scalar") == 0) {
      rb_hash_aset(rb_hash, CSTR2SYM("typename"), convert_string(types[i].typename));
      rb_hash_aset(rb_hash, CSTR2SYM("name"), convert_string(types[i].scalar_type.name));
      rb_hash_aset(rb_hash, CSTR2SYM("description"), convert_string(types[i].scalar_type.description));
    } else if (strcmp(types[i].typename, "object") == 0) {
      rb_hash_aset(rb_hash, CSTR2SYM("typename"), convert_string(types[i].typename));
      rb_hash_aset(rb_hash, CSTR2SYM("description"), convert_string(types[i].object_type.description));
      rb_hash_aset(rb_hash, CSTR2SYM("name"), convert_string(types[i].object_type.name));
      rb_hash_aset(rb_hash, CSTR2SYM("implements"), convert_array_of_strings(types[i].object_type.implements));
      rb_hash_aset(rb_hash, CSTR2SYM("directives"), convert_array_of_directives(types[i].object_type.directives));
      rb_hash_aset(rb_hash, CSTR2SYM("fields"), convert_array_of_fields(types[i].object_type.fields));
    } else if (strcmp(types[i].typename, "enum") == 0) {
      rb_hash_aset(rb_hash, CSTR2SYM("typename"), convert_string(types[i].typename));
      rb_hash_aset(rb_hash, CSTR2SYM("description"), convert_string(types[i].enum_type.description));
      rb_hash_aset(rb_hash, CSTR2SYM("name"), convert_string(types[i].enum_type.name));
      rb_hash_aset(rb_hash, CSTR2SYM("directives"), convert_array_of_directives(types[i].enum_type.directives));
      rb_hash_aset(rb_hash, CSTR2SYM("values"), convert_array_of_values(types[i].enum_type.values));
    } else if (strcmp(types[i].typename, "interface") == 0) {
      rb_hash_aset(rb_hash, CSTR2SYM("typename"), convert_string(types[i].typename));
      rb_hash_aset(rb_hash, CSTR2SYM("description"), convert_string(types[i].interface_type.description));
      rb_hash_aset(rb_hash, CSTR2SYM("name"), convert_string(types[i].interface_type.name));
      rb_hash_aset(rb_hash, CSTR2SYM("directives"), convert_array_of_directives(types[i].interface_type.directives));
      rb_hash_aset(rb_hash, CSTR2SYM("fields"), convert_array_of_fields(types[i].interface_type.fields));
    } else if (strcmp(types[i].typename, "union") == 0) {
      rb_hash_aset(rb_hash, CSTR2SYM("typename"), convert_string(types[i].typename));
      rb_hash_aset(rb_hash, CSTR2SYM("description"), convert_string(types[i].union_type.description));
      rb_hash_aset(rb_hash, CSTR2SYM("name"), convert_string(types[i].union_type.name));
      rb_hash_aset(rb_hash, CSTR2SYM("directives"), convert_array_of_directives(types[i].union_type.directives));
      rb_hash_aset(rb_hash, CSTR2SYM("values"), convert_array_of_strings(types[i].union_type.values));
    } else if (strcmp(types[i].typename, "input_object") == 0) {
      rb_hash_aset(rb_hash, CSTR2SYM("typename"), convert_string(types[i].typename));
      rb_hash_aset(rb_hash, CSTR2SYM("description"), convert_string(types[i].input_object_type.description));
      rb_hash_aset(rb_hash, CSTR2SYM("name"), convert_string(types[i].input_object_type.name));
      rb_hash_aset(rb_hash, CSTR2SYM("directives"), convert_array_of_directives(types[i].input_object_type.directives));
      rb_hash_aset(rb_hash, CSTR2SYM("fields"), convert_array_of_fields(types[i].input_object_type.fields));
    } else {
      printf("\nError: Unknown type %s\n", types[i].typename);
      exit(1);
    }

    rb_ary_push(rb_types, rb_hash);
  }

  rb_iv_set(rb_result, "@types", rb_types);
  return rb_result;
}