forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add more tests, handle more invalid cases
- Loading branch information
Showing
16 changed files
with
7,767 additions
and
43 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,311 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { parse } from '../../language/parser.js'; | ||
|
||
import { GraphQLObjectType } from '../../type/definition.js'; | ||
import { GraphQLString } from '../../type/scalars.js'; | ||
import { GraphQLSchema } from '../../type/schema.js'; | ||
|
||
import { executeSync } from './execute.js'; | ||
|
||
const schema = new GraphQLSchema({ | ||
query: new GraphQLObjectType({ | ||
name: 'TestType', | ||
fields: { | ||
a: { type: GraphQLString }, | ||
b: { type: GraphQLString }, | ||
}, | ||
}), | ||
}); | ||
|
||
const rootValue = { | ||
a() { | ||
return 'a'; | ||
}, | ||
b() { | ||
return 'b'; | ||
}, | ||
}; | ||
|
||
function executeTestQuery(query: string) { | ||
const document = parse(query); | ||
return executeSync({ schema, document, rootValue }); | ||
} | ||
|
||
describe('Execute: handles directives', () => { | ||
describe('works without directives', () => { | ||
it('basic query works', () => { | ||
const result = executeTestQuery('{ a, b }'); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a', b: 'b' }, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('works on scalars', () => { | ||
it('if true includes scalar', () => { | ||
const result = executeTestQuery('{ a, b @include(if: true) }'); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a', b: 'b' }, | ||
}); | ||
}); | ||
|
||
it('if false omits on scalar', () => { | ||
const result = executeTestQuery('{ a, b @include(if: false) }'); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a' }, | ||
}); | ||
}); | ||
|
||
it('unless false includes scalar', () => { | ||
const result = executeTestQuery('{ a, b @skip(if: false) }'); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a', b: 'b' }, | ||
}); | ||
}); | ||
|
||
it('unless true omits scalar', () => { | ||
const result = executeTestQuery('{ a, b @skip(if: true) }'); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a' }, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('works on fragment spreads', () => { | ||
it('if false omits fragment spread', () => { | ||
const result = executeTestQuery(` | ||
query { | ||
a | ||
...Frag @include(if: false) | ||
} | ||
fragment Frag on TestType { | ||
b | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a' }, | ||
}); | ||
}); | ||
|
||
it('if true includes fragment spread', () => { | ||
const result = executeTestQuery(` | ||
query { | ||
a | ||
...Frag @include(if: true) | ||
} | ||
fragment Frag on TestType { | ||
b | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a', b: 'b' }, | ||
}); | ||
}); | ||
|
||
it('unless false includes fragment spread', () => { | ||
const result = executeTestQuery(` | ||
query { | ||
a | ||
...Frag @skip(if: false) | ||
} | ||
fragment Frag on TestType { | ||
b | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a', b: 'b' }, | ||
}); | ||
}); | ||
|
||
it('unless true omits fragment spread', () => { | ||
const result = executeTestQuery(` | ||
query { | ||
a | ||
...Frag @skip(if: true) | ||
} | ||
fragment Frag on TestType { | ||
b | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a' }, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('works on inline fragment', () => { | ||
it('if false omits inline fragment', () => { | ||
const result = executeTestQuery(` | ||
query { | ||
a | ||
... on TestType @include(if: false) { | ||
b | ||
} | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a' }, | ||
}); | ||
}); | ||
|
||
it('if true includes inline fragment', () => { | ||
const result = executeTestQuery(` | ||
query { | ||
a | ||
... on TestType @include(if: true) { | ||
b | ||
} | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a', b: 'b' }, | ||
}); | ||
}); | ||
|
||
it('unless false includes inline fragment', () => { | ||
const result = executeTestQuery(` | ||
query { | ||
a | ||
... on TestType @skip(if: false) { | ||
b | ||
} | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a', b: 'b' }, | ||
}); | ||
}); | ||
|
||
it('unless true includes inline fragment', () => { | ||
const result = executeTestQuery(` | ||
query { | ||
a | ||
... on TestType @skip(if: true) { | ||
b | ||
} | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a' }, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('works on anonymous inline fragment', () => { | ||
it('if false omits anonymous inline fragment', () => { | ||
const result = executeTestQuery(` | ||
query { | ||
a | ||
... @include(if: false) { | ||
b | ||
} | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a' }, | ||
}); | ||
}); | ||
|
||
it('if true includes anonymous inline fragment', () => { | ||
const result = executeTestQuery(` | ||
query { | ||
a | ||
... @include(if: true) { | ||
b | ||
} | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a', b: 'b' }, | ||
}); | ||
}); | ||
|
||
it('unless false includes anonymous inline fragment', () => { | ||
const result = executeTestQuery(` | ||
query Q { | ||
a | ||
... @skip(if: false) { | ||
b | ||
} | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a', b: 'b' }, | ||
}); | ||
}); | ||
|
||
it('unless true includes anonymous inline fragment', () => { | ||
const result = executeTestQuery(` | ||
query { | ||
a | ||
... @skip(if: true) { | ||
b | ||
} | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a' }, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('works with skip and include directives', () => { | ||
it('include and no skip', () => { | ||
const result = executeTestQuery(` | ||
{ | ||
a | ||
b @include(if: true) @skip(if: false) | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a', b: 'b' }, | ||
}); | ||
}); | ||
|
||
it('include and skip', () => { | ||
const result = executeTestQuery(` | ||
{ | ||
a | ||
b @include(if: true) @skip(if: true) | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a' }, | ||
}); | ||
}); | ||
|
||
it('no include or skip', () => { | ||
const result = executeTestQuery(` | ||
{ | ||
a | ||
b @include(if: false) @skip(if: false) | ||
} | ||
`); | ||
|
||
expect(result).to.deep.equal({ | ||
data: { a: 'a' }, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { isPromise } from '../../jsutils/isPromise.js'; | ||
import type { PromiseOrValue } from '../../jsutils/PromiseOrValue.js'; | ||
|
||
import type { ExecutionArgs } from '../../execution/execute.js'; | ||
import type { ExecutionResult } from '../../execution/types.js'; | ||
|
||
import { legacyExecuteIncrementally } from '../legacyExecuteIncrementally.js'; | ||
import type { LegacyExperimentalIncrementalExecutionResults } from '../transformResult.js'; | ||
|
||
export function executeSync(args: ExecutionArgs): ExecutionResult { | ||
const result = legacyExecuteIncrementally(args); | ||
|
||
// Assert that the execution was synchronous. | ||
if (isPromise(result) || 'initialResult' in result) { | ||
throw new Error('GraphQL execution failed to complete synchronously.'); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export function execute( | ||
args: ExecutionArgs, | ||
): PromiseOrValue< | ||
ExecutionResult | LegacyExperimentalIncrementalExecutionResults | ||
> { | ||
return legacyExecuteIncrementally(args); | ||
} |
Oops, something went wrong.