Skip to content

Commit 7f58e28

Browse files
committed
minor fixes
1 parent 862af27 commit 7f58e28

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

graphql-api-generator/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
# pgschema-to-apischema
1+
# graphql-api-generator
22
The tool in this repository extends a schema for Property Graphs into a GraphQL schema for a GraphQL API, where [the Property Graph schema is assumed to be represented using the GraphQL Schema Definition Language (SDL)](http://blog.liu.se/olafhartig/documents/graphql-schemas-for-property-graphs/). The actual approach to extend the Property Graph schemas into GraphQL API schemas (as implemented by this tool) is documented in the [wiki of this repo](https://github.com/LiUGraphQL/pgschema-to-apischema/wiki).
33

44
## Prerequisites
55
```bash
66
$ pip3 install graphql-core-next
7+
$ pip3 install PyYAML
78
```
89

910
## Example
1011
```bash
1112
$ python3 generator.py \
12-
--input resources/schema.graphql \
13+
--input schema.graphql \
1314
--output api-schema.graphql \
1415
--config resources/config.yml
1516
```
@@ -22,7 +23,7 @@ usage: generator.py [-h] --input INPUT [--output OUTPUT] [--config CONFIG]
2223
optional arguments:
2324
-h, --help show this help message and exit
2425
--input INPUT Input schema files (separated by commas)
25-
--output OUTPUT Output schema file (default stdout)
26+
--output OUTPUT Output schema file (optional)
2627
--config CONFIG Path to configuration file
2728
```
2829

@@ -41,7 +42,7 @@ generation:
4142
# add id field to all schema types
4243
field_for_id: true
4344
# add creation date and last update date field(s) to all schema types
44-
generate_datetime: false
45+
generate_datetime: true
4546
field_for_creation_date: true
4647
field_for_last_update_date: true
4748
# add reverse edges for traversal

graphql-api-generator/generator.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from io import UnsupportedOperation
55

66
import yaml
7-
from utils.utils import *
7+
from modules.utils import *
88

99
string_transforms = {
1010
'uppercase': uppercase,
@@ -35,6 +35,7 @@ def cmd(args):
3535
for file in files:
3636
with open(file, 'r') as f:
3737
schema_string += f.read() + '\n'
38+
3839
schema = build_schema(schema_string)
3940

4041
# run
@@ -49,10 +50,6 @@ def cmd(args):
4950

5051

5152
def run(schema: GraphQLSchema, config: dict):
52-
# check if DateTime exists, or should be added
53-
if config.get('generation').get('generate_datetime') or config.get('generation').get('field_for_creation_date') or config.get('generation').get('field_for_last_update_date'):
54-
datetime_control(schema)
55-
5653
# validate
5754
if config.get('validate'):
5855
validate_names(schema, config.get('validate'))
@@ -63,36 +60,45 @@ def run(schema: GraphQLSchema, config: dict):
6360

6461
# API generation
6562
if config.get('generation'):
63+
# check if DateTime exists or should be added
64+
if config.get('generation').get('field_for_creation_date'):
65+
datetime_control(schema)
66+
67+
# add query type
6668
if config.get('generation').get('add_query_type'):
6769
schema = add_query_type(schema)
70+
71+
# add mutation type
6872
if config.get('generation').get('add_mutation_type'):
6973
schema = add_mutation_type(schema)
7074

7175
# add id
7276
if config.get('generation').get('field_for_id'):
7377
schema = add_id_to_types(schema)
7478

75-
# add creationDate
76-
if config.get('generation').get('field_for_creation_date'):
77-
schema = add_creation_date_to_types(schema)
78-
79-
# add lastUpdateDate
80-
if config.get('generation').get('field_for_last_update_date'):
81-
schema = add_last_update_date_to_types(schema)
82-
8379
# add reverse edges for traversal
8480
if config.get('generation').get('reverse_edges'):
8581
schema = add_reverse_edges(schema)
8682

8783
# add edge types
8884
if config.get('generation').get('edge_types') or config.get('generation').get('create_edge_objects'):
89-
schema = add_edge_objects(schema, config.get('generation').get('field_for_creation_date'), config.get('generation').get('field_for_last_update_date'))
85+
schema = add_edge_objects(schema)
9086
if config.get('generation').get('fields_for_edge_types'):
9187
raise UnsupportedOperation('{0} is currently not supported'.format('fields_for_edge_types'))
9288

89+
# add creation date
90+
if config.get('generation').get('field_for_creation_date'):
91+
schema = add_creation_date_to_types(schema)
92+
93+
# add last update date
94+
if config.get('generation').get('field_for_last_update_date'):
95+
schema = add_last_update_date_to_types(schema)
96+
9397
# add queries
9498
if config.get('generation').get('query_by_id'):
9599
schema = add_get_queries(schema)
100+
101+
# add filters
96102
if config.get('generation').get('query_type_filter') or config.get('generation').get('query_list_of'):
97103
schema = add_enum_filters(schema)
98104
schema = add_scalar_filters(schema)
@@ -144,7 +150,6 @@ def run(schema: GraphQLSchema, config: dict):
144150

145151

146152
def validate_names(schema: GraphQLSchema, validate):
147-
148153
# types and interfaces
149154
if validate.get('type_names'):
150155
# type names
@@ -186,7 +191,6 @@ def validate_names(schema: GraphQLSchema, validate):
186191

187192

188193
def transform_names(schema: GraphQLSchema, transform):
189-
190194
# types and interfaces
191195
if transform.get('type_names'):
192196
if transform.get('type_names') in string_transforms:
@@ -268,7 +272,7 @@ def datetime_control(schema):
268272
if not is_scalar_type(schema.type_map['DateTime']):
269273
raise Exception('DateTime exists but is not scalar type: ' + schema.type_map['DateTime'])
270274
else:
271-
schema.type_map['DateTime'] = GraphQLScalarType('DateTime')
275+
schema.type_map['DateTime'] = GraphQLScalarType('DateTime', serialize=lambda x: str(x))
272276
if not is_scalar_type(schema.type_map['DateTime']):
273277
raise Exception('DateTime could not be added as scalar!')
274278

@@ -278,10 +282,9 @@ def datetime_control(schema):
278282
parser.add_argument('--input', type=str, required=True,
279283
help='GraphQL DB schema files (separated by commas), or a path to a schema directory')
280284
parser.add_argument('--output', type=str,
281-
help='Output schema file (default stdout)')
285+
help='Output schema file (optional)')
282286
parser.add_argument('--config', type=str,
283287
help='Path to configuration file')
284-
285288
cmd(parser.parse_args())
286289

287290

graphql-api-generator/modules/utils.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def add_creation_date_to_types(schema: GraphQLSchema):
108108
"""
109109
make = ''
110110
for _type in schema.type_map.values():
111-
if not is_schema_defined_type(_type):
111+
if not is_object_type(_type) or _type.name == 'Mutation' or _type.name == 'Query':
112112
continue
113113
if is_interface_type(_type):
114114
make += f'extend interface {_type.name} {{ _creationDate: DateTime! }} '
@@ -125,12 +125,12 @@ def add_last_update_date_to_types(schema: GraphQLSchema):
125125
"""
126126
make = ''
127127
for _type in schema.type_map.values():
128-
if not is_schema_defined_type(_type):
128+
if not is_object_type(_type) or _type.name == 'Mutation' or _type.name == 'Query':
129129
continue
130130
if is_interface_type(_type):
131-
make += f'extend interface {_type.name} {{ _lastUpdateDate: DateTime }} '
131+
make += f'extend interface {_type.name} {{ _lastUpdateDate: DateTime! }} '
132132
else:
133-
make += f'extend type {_type.name} {{ _lastUpdateDate: DateTime }} '
133+
make += f'extend type {_type.name} {{ _lastUpdateDate: DateTime! }} '
134134
return add_to_schema(schema, make)
135135

136136

@@ -477,13 +477,19 @@ def add_scalar_filters(schema: GraphQLSchema):
477477
for scalar_name, scalar in schema.type_map.items():
478478
if not is_scalar_type(scalar) or scalar_name in ['Int', 'Float', 'String', 'Boolean', 'ID']:
479479
continue
480-
481480
make += f'input _{scalar_name}Filter {{' \
482481
f' _eq: {scalar_name} ' \
483482
f' _neq: {scalar_name} ' \
484483
f' _in: [{scalar_name}] ' \
485484
f' _nin: [{scalar_name}] ' \
486485
f'}} '
486+
if scalar_name == 'DateTime':
487+
make += f'extend input _{scalar_name}Filter {{' \
488+
' _gt: String ' \
489+
' _egt: String ' \
490+
' _lt: String ' \
491+
' _elt: String ' \
492+
f'}} '
487493

488494
schema = add_to_schema(schema, make)
489495

@@ -562,14 +568,8 @@ def get_field_annotations(field: GraphQLField):
562568
return " ".join(annotation_fields)
563569

564570

565-
def add_edge_objects(schema: GraphQLSchema, field_for_creation_date, field_for_last_update_date):
571+
def add_edge_objects(schema: GraphQLSchema):
566572
make = ''
567-
creation_date_string = ''
568-
last_update_date_string = ''
569-
if field_for_creation_date:
570-
creation_date_string = '_creationDate: Date!'
571-
if field_for_last_update_date:
572-
last_update_date_string = '_lastUpdateDate: Date'
573573
for _type in schema.type_map.values():
574574
if not is_schema_defined_type(_type) or is_interface_type(_type):
575575
continue
@@ -581,7 +581,7 @@ def add_edge_objects(schema: GraphQLSchema, field_for_creation_date, field_for_l
581581
for t in connected_types:
582582
edge_from = f'{capitalize(field_name)}EdgeFrom{t.name}'
583583
annotations = get_field_annotations(field)
584-
make += f'type _{edge_from} {{id:ID! source: {t.name}! target: {inner_field_type}! {creation_date_string} {last_update_date_string} {annotations}}}\n'
584+
make += f'type _{edge_from} {{id:ID! source: {t.name}! target: {inner_field_type}! {annotations}}}\n'
585585

586586
schema = add_to_schema(schema, make)
587587
return schema

graphql-api-generator/test/all_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest import TestCase
22
from graphql import build_schema
33
import generator
4-
from utils import compare
4+
from modules.utils import compare
55

66
# TODO Many tests...
77

0 commit comments

Comments
 (0)