Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade graphql-js Testdata Process #633

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading