stigg-api-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,99 @@
1
+ import * as gql from 'graphql';
2
+ import fs from 'fs';
3
+
4
+ const schema = fs.readFileSync(
5
+ '../api-client-schema/src/schema.graphql',
6
+ 'utf8'
7
+ );
8
+
9
+ const operations = fs.readFileSync('operations.graphql', 'utf8');
10
+
11
+ const gqlSchema = gql.buildSchema(schema);
12
+
13
+ const gqlOperations = gql.parse(operations);
14
+
15
+ function generateOperationsModule() {
16
+ const typeInfo = new gql.TypeInfo(gqlSchema);
17
+ const outputProgram = gql.visit(
18
+ gqlOperations,
19
+ gql.visitWithTypeInfo(typeInfo, visitor(gqlSchema, typeInfo))
20
+ );
21
+
22
+ const modules = ['Fragment', 'Mutation', 'Query']
23
+ .map(
24
+ (moduleName) =>
25
+ `module ${moduleName}\n${outputProgram
26
+ .filter((x) => x.moduleName === moduleName)
27
+ .map((x) => x.element)
28
+ .join('\n\n')}\nend`
29
+ )
30
+ .join('\n');
31
+
32
+ return `# frozen_string_literal: true\n\nmodule Stigg\n${modules}\nend`;
33
+ }
34
+
35
+ function findFragments(node, results = new Set()) {
36
+ if (node.kind === 'FragmentSpread') {
37
+ results.add(node.name.value);
38
+ gqlOperations.definitions.find((def) => {
39
+ if (
40
+ def.kind === 'FragmentDefinition' &&
41
+ def.name.value === node.name.value
42
+ ) {
43
+ findFragments(def, results);
44
+ }
45
+ });
46
+ }
47
+ node.selectionSet?.selections?.forEach((node) =>
48
+ findFragments(node, results)
49
+ );
50
+ return results;
51
+ }
52
+
53
+ function visitor(schema, typeInfo) {
54
+ return {
55
+ enter(node) {
56
+ typeInfo.enter(node);
57
+ },
58
+ leave(node) {
59
+ typeInfo.leave(node);
60
+ switch (node.kind) {
61
+ case 'Document':
62
+ return node.definitions;
63
+ case 'OperationDefinition': {
64
+ const results = findFragments(node);
65
+ const rawQuery = node.loc.source.body.substring(
66
+ node.loc.start,
67
+ node.loc.end
68
+ );
69
+ const namelessQuery = rawQuery.replace(
70
+ `${node.operation} ${node.name.value}`,
71
+ `${node.operation} `
72
+ );
73
+ const includedFragments = [...results]
74
+ .map((x) => `#{Fragment::${x}}\n`)
75
+ .join('');
76
+ const constantName =
77
+ node.name.value[0].toUpperCase() + node.name.value.slice(1);
78
+ return {
79
+ moduleName: node.operation === 'mutation' ? 'Mutation' : 'Query',
80
+ element: `${constantName} = <<-GRAPHQL\n${namelessQuery}\n${includedFragments}GRAPHQL`,
81
+ };
82
+ }
83
+ case 'FragmentDefinition': {
84
+ const querySource = node.loc.source.body.substring(
85
+ node.loc.start,
86
+ node.loc.end
87
+ );
88
+ return {
89
+ moduleName: 'Fragment',
90
+ element: `${node.name.value} = <<-GRAPHQL\n${querySource}\nGRAPHQL`,
91
+ };
92
+ }
93
+ }
94
+ },
95
+ };
96
+ }
97
+
98
+ const module = generateOperationsModule();
99
+ fs.writeFileSync('lib/stigg/generated/operations.rb', module);
@@ -0,0 +1,14 @@
1
+ # stitch the operation files together
2
+ cat ../api-client-schema/src/operations/fragments.graphql > operations.graphql
3
+ cat ../api-client-schema/src/operations/queries.graphql >> operations.graphql
4
+ cat ../api-client-schema/src/operations/mutations.graphql >> operations.graphql
5
+
6
+ cp ../api-client-schema/src/schema.json schema.json
7
+
8
+ cat <<-EOF > schema.json
9
+ $(echo { \"data\": ) $(cat schema.json) $(echo })
10
+ EOF
11
+
12
+ node scripts/generate-operations.mjs
13
+
14
+ rubocop -a lib/stigg/generated/operations.rb
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stigg-api-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stigg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: graphlient
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.0
27
+ description: Stigg API Client for Ruby
28
+ email:
29
+ - support@stigg.io
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - lib/stigg.rb
42
+ - lib/stigg/client.rb
43
+ - lib/stigg/generated/operations.rb
44
+ - lib/stigg/version.rb
45
+ - package.json
46
+ - project.json
47
+ - schema.json
48
+ - scripts/generate-operations.mjs
49
+ - scripts/generate.sh
50
+ homepage: https://stigg.io
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://stigg.io
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 3.0.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.2.3
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Stigg API Client
74
+ test_files: []