Skip to content

Commit

Permalink
Read service gqlgen.yml file to find a list of related graphql schemas
Browse files Browse the repository at this point in the history
Summary:
In order to satisfy `make gqlgen`, we needed to add a shared '@Migrate'
directive that services can reference (see D62237). We then ran into
errors linting this shared file because it duplicates the `migrate`
directive we manually add as part of the linter here.

To fix, I resolved the TODO here to just read `gqlgen.yml` for the list
of schema files that each service depends on.

Test Plan:
With these changes applied in `khan-linter` and with webapp including
the changes from `D62237` `ka-lint
pkg/graphql/shared-schemas/side_by_side_directives.graphql` and saw no
errors.

I then ran `make check` within `services/content` and likewise saw no
error.

Reviewers: csilvers, marksandstrom

Reviewed By: csilvers

Subscribers: rachelmarincola, kevinb

Differential Revision: https://phabricator.khanacademy.org/D62244
  • Loading branch information
dkapadia committed Apr 10, 2020
1 parent 6c46aea commit fd2d0e4
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions linters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""Linters process files or lists of files for correctness."""

import itertools
import glob
import json
import logging
import os
Expand Down Expand Up @@ -840,11 +839,10 @@ def process(self, f, contents_of_f):
# https://github.com/cjoudrey/graphql-schema-linter/issues/210
#
# So instead we take a two-pronged approach:
# 1) We append all the other schema files in our directory.
# This matches what gqlgen does, and will not have the
# @extends problem since it's only for a single backend.
# TODO(csilvers): read gqlgen.yml to figure out what
# schema files to include, instead of glob.
# 1) We find the `gqlgen.yml` file that belongs to this service and
# parse it to find out all the other schema files the service depends
# on. This matches what gqlgen does, and will not have the @extends
# problem since it's only for a single backend.
# 2) We then try to run the linter and catch all "undefined
# type" errors. We then add fake definitions for those
# types, and re-run the linter.
Expand All @@ -861,12 +859,22 @@ def process(self, f, contents_of_f):
directive @provides(fields: _FieldSet!) on FIELD_DEFINITION
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
directive @extends on OBJECT | INTERFACE
# Khan-specific directives
directive @migrate(from: String!, state: String!) on FIELD_DEFINITION
"""
# All schema files should live at the top level of a service, so we can
# look for the current gqlgen.yml
service_root = os.path.dirname(f)
gqlgen_path = os.path.join(service_root, "gqlgen.yml")

try:
gqlgen_contents = yaml.safe_load(self._read_file(gqlgen_path))
schema_files = [
os.path.join(service_root, schema_file)
for schema_file in gqlgen_contents["schema"]]
except Exception:
# If the gqlgen.yml file doesn't exist (for shared schemas) we
# assume there are no related files we need.
schema_files = []

schema_files = glob.glob(os.path.join(os.path.dirname(f), '*.graphql'))
for other_f in schema_files:
if other_f == f:
continue
Expand Down

0 comments on commit fd2d0e4

Please sign in to comment.