Skip to content

Commit

Permalink
DEVPROD-7582 Add documentation for running Graphql tests (#7924)
Browse files Browse the repository at this point in the history
  • Loading branch information
khelif96 committed May 24, 2024
1 parent a8d4c90 commit 3395b66
Showing 1 changed file with 62 additions and 15 deletions.
77 changes: 62 additions & 15 deletions graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@

#### Fields

Add fields to .graphql files in the `schema/` folder. When you run `make gqlgen`, these changes will be processed and a resolver will be generated.
Add fields to .graphql files in the `schema/` folder. When you run
`make gqlgen`, these changes will be processed and a resolver will be generated.

You can also add models in `gqlgen.yml`. If a GraphQL object has a corresponding model definition in `gqlgen.yml`, then a resolver will not be generated. Instead you may have to edit the API models which are located in the `rest/model/` folder.
You can also add models in `gqlgen.yml`. If a GraphQL object has a corresponding
model definition in `gqlgen.yml`, then a resolver will not be generated. Instead
you may have to edit the API models which are located in the `rest/model/`
folder.

#### Directives

Directives control access to certain mutations and queries. They are defined in `schema/directives.graphql` but their corresponding functions are not generated through `make gqlgen`. You will have to manually add or edit the directive functions in `resolver.go`.
Directives control access to certain mutations and queries. They are defined in
`schema/directives.graphql` but their corresponding functions are not generated
through `make gqlgen`. You will have to manually add or edit the directive
functions in `resolver.go`.

### Best Practices for GraphQL

#### Designing Mutations

When designing mutations, the input and payload should be objects. We often have to add new fields, and it is much easier to add backwards compatible changes if the existing fields are nested within an object.
When designing mutations, the input and payload should be objects. We often have
to add new fields, and it is much easier to add backwards compatible changes if
the existing fields are nested within an object.

In practice, this means you should prefer

Expand All @@ -35,28 +44,41 @@ In practice, this means you should prefer
over

```graphql
abortTask(taskId: String!): Task
abortTask(taskId: String!): Task
```

See the Apollo GraphQL [blogpost](https://www.apollographql.com/blog/designing-graphql-mutations) from which this was referenced.
See the Apollo GraphQL
[blogpost](https://www.apollographql.com/blog/designing-graphql-mutations) from
which this was referenced.

Note that this guideline only applies to mutations, not queries.

#### Nullability

Nullability is controlled via the exclamation mark (`!`). If you put an exclamation mark on a field, it means that the field cannot be null.
Nullability is controlled via the exclamation mark (`!`). If you put an
exclamation mark on a field, it means that the field cannot be null.

In general, you can reference this [guide](https://yelp.github.io/graphql-guidelines/nullability.html#summary) for nullability. Some callouts from this guide:
In general, you can reference this
[guide](https://yelp.github.io/graphql-guidelines/nullability.html#summary) for
nullability. Some callouts from this guide:

- Complex objects should be nullable due to the "bubbling up" effect.
- Lists should be non-nullable, and items contained within lists should be non-nullable.
- Booleans should be non-nullable. If you have a third state to represent, consider using an enum. You may also want to consider if the boolean field has the potential to evolve into an enum, such as in the example described [here](https://www.teamten.com/lawrence/programming/prefer-enums-over-booleans.html).

These principles apply generally, but you may encounter situations where you'll want to deviate from these rules. Think carefully about marking fields as non-nullable, because if we query for a non-nullable field and get null as a response it will break parts of the application.
- Lists should be non-nullable, and items contained within lists should be
non-nullable.
- Booleans should be non-nullable. If you have a third state to represent,
consider using an enum. You may also want to consider if the boolean field has
the potential to evolve into an enum, such as in the example described
[here](https://www.teamten.com/lawrence/programming/prefer-enums-over-booleans.html).

These principles apply generally, but you may encounter situations where you'll
want to deviate from these rules. Think carefully about marking fields as
non-nullable, because if we query for a non-nullable field and get null as a
response it will break parts of the application.

### Writing GraphQL tests

You can add tests to the `tests/` directory. The folder is structured as the following:
You can add tests to the `tests/` directory. The folder is structured as the
following:

```
.
Expand All @@ -69,8 +91,33 @@ You can add tests to the `tests/` directory. The folder is structured as the fol
└── ...
```

The tests run via the test runner defined in `integration_atomic_test_util.go`. If you see some behavior in your tests that can't be explained by what you've added, it's a good idea to check the setup functions defined in this file.
The tests run via the test runner defined in `integration_atomic_test_util.go`.
If you see some behavior in your tests that can't be explained by what you've
added, it's a good idea to check the setup functions defined in this file.

Note: Do not add anything to the `testdata/` directory. These tests will eventually be deprecated. They run via the test runner defined in `integration_test_util.go`.
Note: Do not add anything to the `testdata/` directory. These tests will
eventually be deprecated. They run via the test runner defined in
`integration_test_util.go`.

Note: Tests for directives are located in `directive_test.go`.

### Running GraphQL tests

Before running any tests, ensure you have a creds.yml file set up. If you don't
have it, you can create one by running the following command:

```bash
bash scripts/setup-credentials.sh
```

To run all of the tests, you can use the following command:

```bash
SETTINGS_OVERRIDE=creds.yml make test-service-graphql
```

To run a specific test, you can use the following command:

```bash
SETTINGS_OVERRIDE=creds.yml make test-service-graphql RUN_TEST=TestAtomicGQLQueries/<TestName>
```

0 comments on commit 3395b66

Please sign in to comment.