Skip to content

Commit

Permalink
Merge pull request #633 from dackroyd/update-graphql-js-testdata-process
Browse files Browse the repository at this point in the history
Upgrade graphql-js Testdata Process
  • Loading branch information
pavelnikolov authored Apr 2, 2024
2 parents f9f5863 + ad43f96 commit a3d0ced
Show file tree
Hide file tree
Showing 13 changed files with 2,543 additions and 3,574 deletions.
4 changes: 2 additions & 2 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5211,8 +5211,8 @@ func TestCircularFragmentMaxDepth(t *testing.T) {
}
`,
ExpectedErrors: []*gqlerrors.QueryError{{
Message: `Cannot spread fragment "X" within itself via Y.`,
Rule: "NoFragmentCycles",
Message: `Cannot spread fragment "X" within itself via "Y".`,
Rule: "NoFragmentCyclesRule",
Locations: []gqlerrors.Location{
{Line: 7, Column: 20},
{Line: 10, Column: 20},
Expand Down
2 changes: 1 addition & 1 deletion internal/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func ResolveType(t ast.Type, resolver Resolver) (ast.Type, *errors.QueryError) {
refT := resolver(t.Name)
if refT == nil {
err := errors.Errorf("Unknown type %q.", t.Name)
err.Rule = "KnownTypeNames"
err.Rule = "KnownTypeNamesRule"
err.Locations = []errors.Location{t.Loc}
return nil, err
}
Expand Down
29 changes: 29 additions & 0 deletions internal/validation/testdata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# graphql-js testdata

Test cases are generated here by extracting them from [graphql-js] into JSON that we can use to drive Go tests.

## Usage

To update the testdata, run the following command within the `testdata` directory:

```sh
go generate .
```

## How it works

A Node.js project is used to pull in graphql-js as a dependency, and automatically patch that via `patch-package`. These
patches replace the `mocha` test functions `describe`, `it`, assertions and the test `harness`. This allows the
expectations to be captured, and written to a JSON file. These test cases in the JSON file are then used to drive the Go
tests.

## Updating patches

With changes to [graphql-js], the patches may need to be updated. To do this, update the `graphql` dependency under
`node_modules`, and sync the patches with the following command:

```sh
npm run create-patches
```

[graphql-js]: https://github.com/graphql/graphql-js
118 changes: 0 additions & 118 deletions internal/validation/testdata/export.js

This file was deleted.

46 changes: 46 additions & 0 deletions internal/validation/testdata/export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as fs from 'node:fs';
import { printSchema } from 'graphql/src/utilities/printSchema.js';
import { schemas, testCases } from 'graphql/src/validation/__tests__/harness.js';

// TODO: Fix test failures.
// require('graphql/src/validation/__tests__/ExecutableDefinitions-test');
import 'graphql/src/validation/__tests__/FieldsOnCorrectTypeRule-test.js';
import 'graphql/src/validation/__tests__/FragmentsOnCompositeTypesRule-test.js';
// TODO: Fix test failures.
// require('graphql/src/validation/__tests__/KnownArgumentNames-test');
import 'graphql/src/validation/__tests__/KnownDirectivesRule-test.js';
import 'graphql/src/validation/__tests__/KnownFragmentNamesRule-test.js';
import 'graphql/src/validation/__tests__/KnownTypeNamesRule-test.js';
import 'graphql/src/validation/__tests__/LoneAnonymousOperationRule-test.js';
import 'graphql/src/validation/__tests__/NoFragmentCyclesRule-test.js';
import 'graphql/src/validation/__tests__/NoUndefinedVariablesRule-test.js';
import 'graphql/src/validation/__tests__/NoUnusedFragmentsRule-test.js';
import 'graphql/src/validation/__tests__/NoUnusedVariablesRule-test.js';
import 'graphql/src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.js';
// TODO: Fix test failures.
// require('graphql/src/validation/__tests__/PossibleFragmentSpreads-test');
import 'graphql/src/validation/__tests__/ProvidedRequiredArgumentsRule-test.js';
import 'graphql/src/validation/__tests__/ScalarLeafsRule-test.js';
// TODO: Add support for subscriptions.
// require('graphql/src/validation/__tests__/SingleFieldSubscriptions-test.js');
import 'graphql/src/validation/__tests__/UniqueArgumentNamesRule-test.js';
import 'graphql/src/validation/__tests__/UniqueDirectivesPerLocationRule-test.js';
import 'graphql/src/validation/__tests__/UniqueFragmentNamesRule-test.js';
import 'graphql/src/validation/__tests__/UniqueInputFieldNamesRule-test.js';
import 'graphql/src/validation/__tests__/UniqueOperationNamesRule-test.js';
import 'graphql/src/validation/__tests__/UniqueVariableNamesRule-test.js';
// TODO: Fix test failures.
// require('graphql/src/validation/__tests__/ValuesofCorrectType-test');
import 'graphql/src/validation/__tests__/VariablesAreInputTypesRule-test.js';
// TODO: Fix test failures.
// require('graphql/src/validation/__tests__/VariablesDefaultValueAllowed-test');
import 'graphql/src/validation/__tests__/VariablesInAllowedPositionRule-test.js';

let output = JSON.stringify({
schemas: schemas().map(s => printSchema(s)),
tests: testCases(),
}, null, 2)
output = output.replace(/ Did you mean to use an inline fragment on [^?]*\?/g, '');
// Ignore suggested types in errors, which graphql-go does not currently produce.
output = output.replace(/ Did you mean \\"[A-Z].*\"\?/g, '');
fs.writeFileSync("tests.json", output);
4 changes: 1 addition & 3 deletions internal/validation/testdata/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
// - npm >= 5.2.0 (for use of npx)
//
// Usage:
// $ git clone https://github.com/graphql/graphql-js
// $ go generate .
package testdata

//go:generate npm install
//go:generate cp export.js graphql-js/export.js
//go:generate npx babel-node graphql-js/export.js
//go:generate npm run export
Loading

0 comments on commit a3d0ced

Please sign in to comment.