Skip to content

Commit

Permalink
feat: 🎸 disable unique directive per location rule (#31)
Browse files Browse the repository at this point in the history
* feat: 🎸 disable unique directive per location rule

* test: πŸ’ add missing test

* chore: πŸ€– bump publish node version from 12 to 14 on CircleCI

* fix: πŸ› remove error throw on success test
  • Loading branch information
marioviana authored Dec 7, 2021
1 parent 9efd984 commit 43e935c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ workflows:
version: '14'
- publish:
<<: *only_master
version: '12'
version: '14'
context: common-env-vars
requires:
- node-10
Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/valid-federated-with-multiple-primary-keys.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type Sale @key(fields: "id") {
id: ID!
name: String
seller: User
}

extend type User @key(fields: "id") @key(fields: "netsuiteInternalId") {
id: ID! @external
netsuiteInternalId: Int
sales: [Sale]
numberOfSales: Int
}

extend type Query {
sales: [Sale]
}
9 changes: 9 additions & 0 deletions src/__tests__/validateSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ function expectError(filename: string, isFederated: boolean) {
throw new Error('Expected validateSchema to throw');
}

function expectSuccess(filename: string, isFederated: boolean) {
validateSchema(filename, isFederated);
return expect(true);
}

test('non-existant.graphql', () => {
expectError(__dirname + '/non-existant.graphql', false).toMatchInlineSnapshot(
`"Could not find the schema at src/__tests__/non-existant.graphql"`,
Expand Down Expand Up @@ -92,3 +97,7 @@ test('invalid-federated-schema-2.graphql', () => {
"
`);
});

test('valid-federated-with-multiple-primary-keys.graphql', () => {
expectSuccess(__dirname + '/valid-federated-with-multiple-primary-keys.graphql', true).toBeTruthy();
});
8 changes: 7 additions & 1 deletion src/validateSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {relative} from 'path';
import {GraphQLError} from 'graphql/error';
import {parse, DocumentNode} from 'graphql/language';
import {validateSDL} from 'graphql/validation/validate';
import {specifiedSDLRules} from 'graphql/validation/specifiedRules';
import {UniqueDirectivesPerLocationRule} from 'graphql';
import {codeFrameColumns} from '@babel/code-frame';
import ExpectedError from './ExpectedError';
import chalk from 'chalk';
Expand All @@ -13,6 +15,9 @@ directive @requires(fields: _FieldSet!) on FIELD_DEFINITION
directive @provides(fields: _FieldSet!) on FIELD_DEFINITION
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
`;

const OMITTED_VALIDATION_RULES = [UniqueDirectivesPerLocationRule.name];

export default function validateSchema(filename: string, isFederated: boolean): {source: string} {
let schemaString: string;
try {
Expand Down Expand Up @@ -87,7 +92,8 @@ export default function validateSchema(filename: string, isFederated: boolean):
});
}

const errors = validateSDL(parsedSchema);
const rules = specifiedSDLRules.filter((rule) => !OMITTED_VALIDATION_RULES.includes(rule.name));
const errors = validateSDL(parsedSchema, undefined, rules);
if (errors.length) {
throw new ExpectedError(
`${chalk.red(`GraphQL schema ${errors.length > 1 ? 'errors' : 'error'} in`)} ${chalk.cyan(
Expand Down

0 comments on commit 43e935c

Please sign in to comment.